How do I rebind a key in Emacs globally? - emacs

I have this in my .emacsrc
(define-key global-map "\C-h" 'backward-delete-char) ;;previously help
however, when I do an I-search and hit C-h to delete a char, emacs uses the default binding and tries to open the help, when I cancel the search with C-g, it then executes the backward-delete-char in the document I started the I-search from.
EDIT: This makes me wonder why C-h is again mapped to help inside the minibuffer. What is overriding my global-map definition and why?

You can use this:
(keyboard-translate ?\C-h ?\C-?)
This will translate C-h to backspace everywhere.
When you do isearch or ido-find-file, they
override some keys in the minibuffer. You could re-override
these keys for each mode, but you'd have to really do it for each
new mode that you use. That's why I've suggested the code above:
modes will not typically rebind backspace to something that doesn't behave
like a backspace. The point is that the logical C-h is still
bound for help inside minibuffer, but with keyboard-translate you don't
have C-h anymore - you just have another backspace. And if you
want to bind some command to it, you can't bind to C-h -
you have to bind to ?\C-?.

My guess is that you don't really want to delete a char when you hit "control and h", but instead that for some reason Emacs receives C-h when you press the backspace key, right?
If so, the best solution is to figure out how to change your terminal emulator so that it doesn't send C-h to Emacs when you press backspace (e.g. it could send C-? as do most other terminal emulators nowadays).

Related

can't remap C-v in emacs

I want to remap Control-v for it to use my custom function, but it looks like I can't do that. I'm running emacs in terminal.
(global-unset-key "\C-v")
(global-set-key "\C-v" 'my-cut-or-paste)
If I try to unset first, C-h k just not showing any binding for this shortcut, and C-v just do nothing. If not unsetting shortcut, its not get remapped... The strange thing is how C-h k showing different keys for such shortcuts. For example - one time it can show
r runs the command self-insert-command, which is an interactive
other time (after emacs reload)
i runs the command self-insert-command, which is an interactive
If I try to set my function to such keys (i or r), C-v gets remapped, but those keys (i or r) are mapped to my function too, so I cant type.
Is this kind of 'terminal-related' key sequences? Is it able to do at all? thx.
My guess would be that your terminal is intercepting C-v as a paste-from-clipboard command.
If your clipboard has something that starts with i, you see:
i runs the command self-insert-command, which is an interactive
Similar for "r" or other letters. Emacs is blind to the fact that the "letters" come from a external paste, not from your keyboard. You should check the configuration options of your terminal, to see if you can unset that key.

Modifying Emacs isearch key bindings to yank

I want to be able to use M-v hotkey in the emacs search mode to paste text. I know I can add the binding to the isearch-mode-map but when I try to bind yank as a method, it yanks the text in the current buffer, not the search input. How can I find which command is invoked when C-y is pressed in the search mode?
Use isearch-yank-kill instead of yank. Try (lookup-key isearch-mode-map (kbd "C-y")). I use minibuffer-inactive-mode-map, minibuffer-local-map, minibuffer-local-completion-map. You can get exhaustive list of maps by C-hv-mapTAB. Function (current-local-map) can help. See also http://www.gnu.org/software/emacs/manual/html_node/elisp/Controlling-Active-Maps.html
Upd.: Name of current local keymap, definition of function keymap-symbol, see https://stackoverflow.com/a/14490054/1937596
If you use
(setq enable-recursive-minibuffers t)
you can, while in minibuffer, call (eval-expression) by hotkey and execute (current-local-map) or (keymap-symbol (current-local-map))
Typing C-sC-hkC-y will tell you:
C-y runs the command isearch-yank-kill.
More generally, type C-hk whilst isearching, followed by the key sequence you want to know about. Analogous to C-hk when you're not searching, of course.
Typing C-hb whilst isearching displays all of the isearch bindings, which is likewise analogous to the output for C-hb when you're not searching.
The other isearch help bindings are C-hm to show you the mode help, and C-hC-h which gives you a menu to all of the above.

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).

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

How to bind ESC to keyboard-escape-quit in Emacs?

Normally keyboard-escape-quit is bound to EscEscEsc. Is it possible to rebind it to a single Esc? I never use Escape as a prefix key.
I'm running Emacs 23.0.60.1 on Windows XP.
Rehashing other's answer, I have
(global-set-key (kbd "<escape>") 'keyboard-escape-quit)
in my .emacs file, and it works on my emacs 22 on WinXP. I also hate typing 3 ESC in a row; and from years of (windows) habits my finger goes so naturally to the escape key for getting out of anything unpleasant.
Not to say this is right for you, but when I had this problem I taught myself to press Ctrl-g instead, which is also bound to keyboard-escape-quit by default. For me, this has the advantage of keeping my left hand pretty close to the home position, as well as leaving my Esc prefix intact.
Edit: After reading through the linked page, it's not bound to exactly the same function, and on Windows Ctrl-g can't forcibly interrupt a running command, but Ctrl-g covers 99% of what I would use Esc Esc Esc for --- aborting a command that I screwed up entering.
You can do it, but at the expense of killing the Esc prefix key map
The code to do this is
(global-set-key "" 'keyboard-escape-quit)
where the funny char is is escape (use ^Q esc to type it in)
it will map esc for you but the rest of the keymap is gone
after that