Make a key behave as another key - emacs

I want certain keys and key combinations to behave as other keys or key combinations in Emacs. For example, I want F5 to behave as a substitute for C-c for every possible combination involving it, or C-S- as C-. Is it possible to do that without manually rebinding all such key combinations?

The keys you are referring to are known as 'prefix keys'. A prefix key has its own keymap, so to make another key behave the same, you need to assign it to the same keymap. For control-c, you use the mode-specific-map:
(global-set-key (kbd "<f5>") mode-specific-map)
Control on its own isn't a prefix key, or really a key at all, since it doesn't send a keypress to Emacs[1] until you hit another key. I'm not sure how to remap C-S- to C- within Emacs. You could do it system-wide with xmodmap, but that's probably not what you want.
[1] the control key (and shift, alt) do send a keypress to the operating system, but Emacs doesn't 'see' this unless there's another key pressed at the same time

I prefer
(define-key key-translation-map [f5] (kbd "\C-c"))
Here is a good resource.
To summarize the link given above: The disadvantage of global-set-key is that, when you define a key combination to enter a symbol, it doesn't work in isearch.
key-translation-map also has a problem. Imagine you defined a symbol | to execute a command and C-| to enter the symbol |, pressing C-| will execute the command.

Related

In Spacemacs (or Emacs), is it possible to bind keys to other keys?

Here is one thing that has been hard to find out. In Spacemacs (or Emacs), is it possible to have a given key combination set to execute whatever another key combination is supposed to do?
In other words, is it possible, for example, to have the F5 key always execute whatever the combination C-c C-c is supposed to do? Something like:
(global-set-key [remap (kbd "F5")] (kbd "C-c C-c"))
Let me give a concrete example for clarity. The key combination C-c C-c is often used to send the current buffer's entire code to interpreter/console, be it to send the buffer's code to the Python interpreter in case the code resides in a .pyfile, or to send it to a R console with package ESS in a .R file.
Now, suppose that one wants to have the F5 key set to do that for these two languages and maybe even more. One obvious solution would be to simply implement key bindings that are different for each package. However, it would be much easier if it was possible to simply say: whenever F5 is pressed, it should result in whatever command associated to `C-c C-c being called.
Is it possible to have such a thing? I am particularly interested in doing that for Spacemacs, but of course a more generic answer for Emacs is acceptable.
Try
(define-key key-translation-map [f5] (kbd "C-c C-c"))
It should solve your problem.

Emacs - Can't map C-[

I'm trying to map C-[ in Emacs to do the same as C-g. I tried this:
(global-set-key "\C-[" 'keyboard-escape-quit)
But Emacs behaves strangely after remapping C-[. For example, M-x stops working and if I try to remap M-x I get the following error:
error: Key sequence M-x starts with non-prefix key ESC
Why does this happen? Is there a workaround?
C-[ is the same as ESC, the Escape key. You probably do not want to rebind ESC, since it is used in many, many, many keybindings, as a prefix key. It implements the Meta key modifier in many cases, which is probably why you say that "Emacs behaves strangely" after you rebound it (removing its prefix-key behavior). See the Emacs manual, node User Input.
As to "Is there a workaround?" -- pick another key (leave ESC alone).
And wrt ESC and C-g: See the Emacs manual, node Quitting (also node Menu Bar).

Emacs: Use a function key (F19) as meta

I’m on a Mac and would like to use the function key F19 as meta.
(There’s a good reason, although it's a bit of a hack: My built (the otherwise excellent port by Yamamoto Mitsuharu) doesn’t support using only the left alt key as meta while preserving the native behavior (inserting special characters) of the right alt key.. So I remapped the left alt key to an unused key - F19 - on the system level with PCKeyboardHack (xmodmap is sorely missed) and would like to tell Emacs to use that as meta.)
So, how do I: use a function key (F19 in my case) as meta key in Emacs?
(I’m fairly new to Emacs and, after some googling, tried out something like
(define-key global-map [f19] \M)
but that, of course, doesn’t do the trick (Symbol’s value as variable is void: M)
Well, with
(setq x-alt-keysym symbol)
you can tell emacs what key is to be understood as meta, but afaik it only accepts 'meta, 'alt, 'super and 'hyper as symbol. Maybe try it with f19.
An alternative option that will work but which will require adaption to a new way of working would be to use F19 as a prefix key (like you use C-h or F1 to invoke help commands):
(define-prefix-command 'f19-map)
(global-set-key (kbd "<f19>") 'f19-map)
(global-set-key (kbd "<f19> x") 'execute-extended-command)
(global-set-key (kbd "<f19> u") 'upcase-word)
...
Writing the configuration shouldn't be too hard. Just press C-h b to get all existing keybindings and edit the help buffer. Get rid of all lines that don't start with M-, then use rectangles to replace all occurrences of ^M- by (global-set-key (kbd ", and so on.
Yet another option would be to bind the key to Esc instead of F19, as long as that's supported by your system, and use the esc prefix instead of the f19 prefix. That way you don't have to change the emacs configuration at all.

emacs create key modifier

I am using emacs on mac os.
I would like to map a modifier (Meta, Control,...) to a simple key.
Basically this is what I need : (global-set-key (kbd "a") 'hyper)
Here a is just the a key, no Control-a or whatever, just "a" alone whithout modifier.
I want "a" to become hyper for example but I can't find a solution for this.
Someone have a clue ?
This approach won't make a modifier key in the sense that you press another key with the modifier held down, but it lets you use hyper bindings nevertheless.
By default you can utilise the additional modifier keys via the C-x# prefix. See C-x#C-h for the full list.
These bindings are in function-key-map, and you can use the same method to get your own OS-independent bindings. e.g.:
(define-key function-key-map (kbd "C-c H") 'event-apply-hyper-modifier)
function-key-map is the parent for all local-function-key-map instances. Note carefully:
Entries in `local-function-key-map' are ignored if they conflict with bindings made in the minor mode, local, or global keymaps. I.e. the remapping only applies if the original key sequence would otherwise not have any binding.
C-hig (elisp) Translation Keymaps RET
So ensure that you use a key sequence with no existing bindings. (In theory that's quite limiting, but YMMV.)
Edit: To clarify, this gives you a way to access any existing 'hyper' bindings when you are using a machine without a hyper modifier key, but does not actually create a new modifier key. If you have no existing bindings to access, this technique has no advantages over a regular prefix binding.
I believe it's the case that, as Peter commented, creating a genuine modifier key is an OS-level task. AFAIK when you press a modifier key on its own, Emacs does not receive any input, and when you press a non-modifier with a modifier, Emacs receives the (modified) input. Conversely when you press/hold a non-modifier key, Emacs receives input immediately, and has no way to combine that input with some other input and treat it all as a single (modified) event.
IIRC, xmodmap would be the typical mechanism for achieving this in Unix systems, so that may well be the case for OSX. The following links may help:
http://computing.fnal.gov/docs/products/xemacs/v21_1/xemacs.info,.SuperandHyperKeys.html
http://www.emacswiki.org/emacs/XModMap
http://www.emacswiki.org/emacs/MetaKeyProblems

writing lisp emacs key binding and cannot specify the <delete> character

For some reason I got the default M-del key binding for backward-kill-word mapped to a scan for matching brackets and resetting is not working, so I am trying to set the global key binding in lisp. So I wrote in ~/.emacs.d/init.el the lisp commands:
(global-set-key (kbd "M-h") 'backward-kill-word)
(global-set-key (kbd "M-<\delete>") ‘backward-kill-word)
I tried them with C-x C-e and they both give the 'backward-kill-word output but only the first key-binding works "M-h", the other is ignored and M-del still trying the strange scanning action. The delete key works in emacs elsewhere, so it seems like "delete" is not being mapped to the physical key in lisp (and the backslash is there to show in this text only as the word was being commented out). Any idea what keyword to use or special character?
Best.
(I looked for libraries that may have overrided this command but I cannot find them)
On some systems, the delete key is defined as an alias to C-d. This is done through function-key-map on GNU Emacs <23 and local-function-key-map on GNU Emacs 23. (I've observed this behavior on Debian and Ubuntu 10.04 under X.) The purpose of such translations is to isolate people who code modes from the terminal intricacies: a mode that wants to shadow the delete command only needs to rebind C-d and not wonder if it should rebind delete (is that a delete left or delete right?) or deletechar or something else.
If there is a global or local binding for delete, it shadows this translation to C-d. However, if you press ESC delete, if there is no global or local binding for ESC delete, the second key is translated to C-d. This translation has precedence over the interpretation of ESC delete as M-delete. So ESC delete becomes equivalent to C-M-d.
This is arguably a bug in Emacs: the effect of ESC delete should be the same as M-delete, and there is no reason why ESC delete would run down-list which has nothing to do with deletion.
There are several possible fixes; I don't know which is best. One that should work with any version of Emacs is
(global-set-key [?\e delete] 'backward-kill-word)
The really nice thing about kbd is that what you type there is the same string that Emacs displays. So, try the following
C-h k M-<\delete> (to use your syntax)
or
M-x describe-key M-<\delete>
Emacs (for me) responds with:
M-DEL (translated from <M-delete>)
runs the command backward-kill-word,
which is an interactive compiled Lisp
function in `simple.el'.
It is bound to , M-DEL.
(backward-kill-word arg)
....
Which you can see shows that the representation for the key you want is M-DEL or M-delete.
Which is a long way of getting to the point that what you want is
(global-set-key (kbd "M-delete") 'backward-kill-word)
Of course, if you have something in your .emacs that overrides it, the above won't help. You'll need to find that included library and stop using it (or customize its behavior).
You might want to call global-set-key interactively to see how it interprets meta-delete. Also try local-set-key to ensure the strange binding is not mode-specific.
After not being able to find the library holding the conflict I found this webpage
http://www.cs.cmu.edu/cgi-bin/info2www?%28emacs%29Rebinding
Changing Key Bindings Interactively...
`M-x global-set-key KEY CMD '
Define KEY globally to run CMD....
Normally, C-z' is bound to the function
suspend-emacs' (when not using the X Window System), but you can
change C-z' to invoke an interactive subshell within Emacs, by binding
it toshell' as follows:
M-x global-set-key <RET> C-z shell <RET>
`global-set-key' reads the command name after the key. After you
press the key, a message like this appears so that you can confirm that
you are binding the key you want:
Set key C-z to command:...
And now the standard default is returned to by doing
M-x global-set-key M-del ...
backward-kill-word
But this is transient and must be done on each reload, any way to make this permanent?
Putting a command into the init.el is not overriding the other effect