Rebinding C-c to C-c - emacs

I'm using Viper, and I want to change its C-c and C-g to the original emacs functions. I can rebind C-g with (define-key viper-vi-global-user-map "C-g" 'keyboard-quit), but how can I rebind C-c, since it's a prefix key?
Thanks!

It may make sense for you to run M-x viper-set-expert-level with an argument of 2 ("Master"). As the viper-mode documentation explains:
2 -- MASTER: C-c now has its standard
Emacs meaning in Vi command state, so
most Emacs commands can be used when
Viper is in Vi state.
As you master viper-mode, you're meant to increase your expert-level setting gradually over time, making more Emacs features available to you (or, as the Viper documentation puts it, "To use Emacs productively, you must reach level 3 or higher").

The original binding for C-c can be set with the following:
(define-key viper-vi-global-user-map (kbd "C-c") 'mode-specific-command-prefix)
The info page for this is Prefix Keys.

Related

How do I reset Emacs keybindings to default?

I was unsetting the C-z quit shortcut in my Emacs. I accidentally made a typo, and somehow made it so that I couldn't capitalize the letter C. In an effort to try to restore this, I accidentally unset C-y, and then accidentally set it back to "undo" instead of "paste". Then I accidentally unset the letter "c" completely.
My Emacs is in a mess right now. How can I just reset these keys to their default commands?
For reference, I am running on EC2 with AMI 5.10. I also could not find an existing Emacs init file in any of the standard locations.
There's no "reset all the keys" command (and for numerous reasons it wouldn't make any sense to include one), but if you don't want to restart your existing instance, then do this:
Start a new, separate instance.
In that, ask Emacs what the key is bound to, using C-hk
Back in the original instance, rebind the key to that command (in the same keymap)
For the global keymap you can use M-x global-set-key
More generally, evaluate the code (define-key KEYMAP (kbd "KEY SEQUENCE") #'COMMAND) for the KEYMAP name indicated in the *Help* buffer. E.g. if I saw:
C-y runs the command yank (found in global-map)
Then (because it's the global keymap) I could use either of these:
(define-key global-map (kbd "C-y") #'yank)
M-x global-set-key RET C-y yank RET

Emacs: How to bind key only in regular buffers and not in the minibuffer?

I have written a fancy function, which I would like to bind to TAB. The functionality is only meaningful in any non-read-only text buffer. Currently, I bind it either like that:
(global-set-key (kbd "<tab>") 'my-indent-region)
or
(define-key global-map (kbd "<tab>") 'my-indent-region)
The problem with this binding is that now tab-completion does no longer work in the minibuffer, which is an essential feature (e.g. for buffer/file names, or M-x).
Is it possible to bind TAB only for regular modes? I know that I can use define-key some-major-mode-map, but since I want it in all modes except for the minibuffer, this would be annoying to maintain. Thus, I'm probably looking for something like a define-key any-mode-except-minibuffer ....
If such a functionality does not exist: Is there a workaround to get the tab-completion working in the minibuffer again? Maybe I can re-set the original minibuffer tab binding after changing the global binding? I couldn't figure out though which function I actually have to bind to make it work.
After some more research I found a workaround/solution to the problem in this answer.
Apparently, my problem was that I was binding to (kbd "<tab>"). If I understand it correctly, my problem was in fact not that I overwrote the actual keymap of the minibuffer -- I guess they are correctly loaded when entering the minibuffer minor modes. However, there seems to be a precedence of a binding to (kbd "<tab>") over a binding to just "\t". According to the above answer, the minibuffer bindings just use "\t", so binding to (kbd "<tab>") shadows them. I'm now using the following binding instead:
(global-set-key "\t" 'my-indent-region)
Now everything seems to be working fine.
Do you see this behavior when you start Emacs without your init file (emacs -Q)? I doubt it. If not, then recursively bisect your init file to find out what is causing the problem.
The minibuffer uses its own keymaps, which are local and which therefore take precedence over global keymap bindings.
However, any minor-mode keymaps take precedence over local keymaps. So if, for example, you have a (global) minor mode turned on that binds <tab> then that will override any binding for that key in the minibuffer keymaps.
Another thing you can do is simply bind whatever command you want to <tab> in the minibuffer keymaps. But again, you should not need to do that, if you want the usual <tab> behavior for the minibuffer.
[Another possible confusion: Some things, such as Isearch, which you might think use the minibuffer do not use it. Isearch uses its own keymap, isearch-mode-map.]
UPDATE after your comment:
Assigning a key in the global map, as you have done, should not affect what that key does in the minibuffer, provided it has a different binding in the minibuffer keymaps. TAB is typically bound in all of the minibuffer completion keymaps (but not in the non-completion minibuffer keymaps).
See the Elisp manual, nodes Completion Commands and Text from Minibuffer for information about the minibuffer keymaps.
To see what the current bindings are for a keymap that is associated with a variable (such as minibuffer-local-completion-map), load library help-fns+.el and use C-h M-k followed by the keymap variable's name. (See Help+ for more information about the library.)
If you do not want TAB to use your global command binding in the non-completion minibuffer maps (minibuffer-local-map, minibuffer-local-ns-map), then just bind it in those maps to whatever command you like. But for the completion maps you should not need to do anything - TAB should already be bound there.
Did you try emacs -Q, to see if something in your init file is interfering? If not, do that first.

disable package key-binding override in Emacs

This seems like some basic Emacs configuration issue that I do not understand.
I have Emacs 24.3 preinstalled on site. In a Verilog file, pressing M-s is bound to p4-current-file "edit" (Perforce operation), likewise M-e is bound to p4-current-file "sync". All is well.
The problem is in a VHDL file, M-s does the same, but M-e is bound to "vhdl-end-of-statement". I would like it to do "p4 edit" like in Verilog.
Seems that vhdl-mode is overriding the M-e key function, and I cannot not find how to change this permanently. If I do local-unset-key and unset M-e, I get the desired result, M-e does p4 edit, but when I restart Emacs M-e does "vhdl-end-of-statement" again.
How can I change M-e to do "p4 edit" permanently? I'm not fluent in Lisp, but will be happy to get instructions how to change this.
Thanks.
To remove an over-riding binding from a mode keymap, so that Emacs will fall back to what it would usually use, you would normally do something like this:
(eval-after-load "vhdl-mode"
'(progn
(define-key vhdl-mode-map (kbd "M-e") nil) ;; unbind M-e
(define-key vhdl-mode-map (kbd "M-a") nil))) ;; unbind M-a
As MrBones comments, though, these are not very desirable custom bindings. Both M-s and M-e have standard uses in Emacs (the former is normally a prefix binding which a bunch of useful things hang off), and if possible you should consider moving those bindings to other keys.
(My impression is that the "preinstalled on site" version had these bindings already in place, though? In which case that's more a note for whoever maintains that...)

redefined keys doesn't work correctly

I've redefined key bindings for some basic movement functions in my init.el file:
(global-set-key "\C-j" 'backward-char)
(global-set-key "\C-k" 'next-line)
(global-set-key "\C-l" 'forward-char)
(keyboard-translate ?\C-i ?\H-i)
(global-set-key [?\H-i] 'previous-line)
(global-set-key "\M-j" 'backward-word)
(global-set-key "\M-l" 'forward-word)
And in general (text editing) it perfectly works, but in some modes it executes multiple commands, e.g. in Buffer mode when I press C-k aside from moving the cursor down Emacs marks the listed buffer for deletion. Also, when I call helm-prelude with C-c p h and press one of these key bindings Emacs either doesn't react at all or, in case of C-k, clears the search bar. I thought that the purpose of global-set-key was to bind commands to specific keys everywhere, am I wrong?
Local (e.g., major-mode) keymap bindings trump global keymap (global-map) bindings. And minor-mode keymap bindings trump both of these.
There is a hierarchy of several keymap types that determines which maps take precedence. See the Elisp manual, node Controlling Active Maps (and nearby nodes about keymaps). The full hierarchy is a bit complicated, but most of the time what you need to be aware of is what I stated in the preceding paragraph.
Yes, the global keymap is only used when there is no binding for the key being pressed in a local keymap. For example, the buffer menu mode uses Buffer-menu-mode-map, where C-k is bound to Buffer-menu-delete.
You may have better luck using keyboard-translate to translate these keys to the "normal" Emacs bindings for those commands, i.e. C-p, C-n etc.

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