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...)
Related
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
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.
Cannot get to bind Enter to newline-and-indent in Emacs !!! Very annoying.
I already tried everything on the following thread by changing 'mode' to ruby and still nothing:
How do I make Emacs auto-indent my C code?
I know that the problem is the RETURN key, since if I bind to something else, works fine.
I tried [enter], (kbd "enter"), (read-kbd-macro "enter"), (kbd "RET")
Follow-up 1.
This is what I get from C-hkRET
RET runs the command newline, which is an interactive compiled Lisp
function.
It is bound to RET.
(newline &optional ARG)
Insert a newline, and move to left margin of the new line if it's blank.
If use-hard-newlines' is non-nil, the newline is marked with the
text-propertyhard'.
With ARG, insert that many newlines.
Call auto-fill-function' if the current column number is greater
than the value offill-column' and ARG is nil.
I dont know what to make of it or how to figure out if it's a global
or local binding that gets in the way. trying to remap C-j
also doesnt work.
As a previous comment says, use C-h k (describe-key) to see what the key is bound to at the point when it's not doing what you want. The (kbd "foo") syntax will be correct for whichever foo describe-key refers to it as.
Chances are that you are simply not defining that key in the appropriate keymap.
Note that major and minor mode keymaps take precedence over the global keymap, so you shouldn't necessarily be surprised if a global binding is overridden.
edit:
Myself, I have a hook function for common behaviours for all the programming modes I use, and it includes the sort of remapping you're after. The relevant part looks like this:
(defun my-coding-config ()
(local-set-key (kbd "RET") (key-binding (kbd "M-j")))
(local-set-key (kbd "<S-return>") 'newline)
)
(mapc
(lambda (language-mode-hook)
(add-hook language-mode-hook 'my-coding-config))
'(cperl-mode-hook
css-mode-hook
emacs-lisp-mode-hook
;; etc...
))
See Daimrod's answer for the explanation of why I'm re-binding RET to the current binding of M-j -- although I'm using comment-indent-new-line (or similar) instead of newline-and-indent (or similar), which does what I want in both comments and non-comments.
In Emacs 24, programming modes seem to derive from prog-mode, so you could probably (un-tested) reduce that list to prog-mode-hook plus any exceptions for third-party modes which don't yet do that.
As said earlier, use C-hkC-j because
C-j is the standard key to do newline-and-indent.
If you open a new file, activate ruby-mode and try the previous
command you will see why it doesn't work. Because ruby-mode doesn't
have newline-and-indent but rather
reindent-then-newline-and-indent. Yes that's stupid but you can either ask
to the maintener to change it, or accept it.
However I suggest you to use C-j to do it because
ruby-mode is not the only mode to do so, like paredit-mode which
uses paredit-newline.
I'm trying to setup multi-term for emacs 23, but the
(setq term-unbind-key-list '("C-z" "C-x" "C-c" "C-h" "C-y"))
line all the websites I've seen recommend for getting rid of key binding clashes doesn't work- I still can't C-z out of man pages for example.
Did something change in emacs 23? How do I unbind them properly?
The variable term-unbind-key-list only affects bindings in the key map term-raw-map. You can find this out by looking at the documentation for the function multi-term-keystroke-setup (no idea why this information isn't available for the variable itself...)
Keystroke setup of `term-char-mode'.
By default, the key bindings of term-char-mode' conflict with user's
keystroke. So this function unbinds some keys withterm-raw-map', and
binds some keystroke with `term-raw-map'.
So... likely the C-z is still bound to suspend-frame b/c that's what Emacs does by default.
If you want C-z to be bound to what it is normally in a terminal (suspend-job), you can do this:
(require 'multi-term)
(add-to-list 'term-bind-key-alist '("C-z" . term-stop-subjob))
Which makes the binding do what (I'm guessing) you want.
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.