I am using python with elpy mode which work fine, however I am really annoyed by the default which runs executes the command under the cursor whenever I press CTRL+ENTER since I keep pressing it accidentally.
How do I disable this behavior? I tried
(global-set-key (kbd "<C-return>") nil)
but that does not seem to have an effect. Any help is much appreciated.
It's probably not set in the global key map, but in the major mode's map or some minor mode's map. In a buffer with the key bound, do C-h k C-<return> to see the binding; it should show the key map that it's in. Then use define-key to change it. E.g. if foo-mode-map contains the binding, do
(define-key foo-mode-map (kbd "C-<return>") nil)
You will probably want to add that code to either a hook or wrap it in with-eval-after-load, so foo-mode-map is defined when it runs.
Related
S-RIGHT is the default keybinding for (org-property-next-allowed-values) and
S-LEFT for (org-property-previous-allowed-value) according to https://orgmode.org/manual/Property-Syntax.html. These keybindings are used to cycle through the TODO keywords as well as schedule dates.
I want to change the keybinding to C-RIGHT and C-LEFT, respectively so that they don't interfere with my normal editing. (I use S-LEFT, S-RIGHT for text selection).
I tried to add the following lines to my init.el:
(define-key org-mode-map (kbd "<S-left>") nil)
(define-key org-mode-map (kbd "<S-right>") nil)
The above 2 lines successfully disabled the 2 keybindings.
Then I added the following 2 lines to remap the keybindings:
(define-key org-mode-map (kbd "<C-left>") 'org-property-previous-allowed-value)
(define-key org-mode-map (kbd "<C-right>") 'org-property-next-allowed-values)
However, emacs (I use AquaEmacs on Mac) doesn't recognize the "org-property-previous-allowed-value" and "org-property-next-allowed-values".
I wonder what did I do wrong here?
Thanks
<S-right> is bound to org-shiftright by default, which does different things depending on context. It only does org-property-next-allowed-value if you cursor is on a property.
I don't know why you are having problems with the rebinding, hence my comment asking for clarification. But I thing that you probably don't want to redefine keys at all. Try adding
(setq org-support-shift-select 'always)
to your init file instead. The doc string for org-support-shift-select says (among other things - you should do C-h v org-support-shift-select RET and read the whole thing if you decide to go this way):
In Emacs 23, when ‘shift-select-mode’ is on, shifted cursor keys
start selecting a region, or enlarge regions started in this way.
In Org mode, in special contexts, these same keys are used for
other purposes, important enough to compete with shift selection.
Org tries to balance these needs by supporting ‘shift-select-mode’
outside these special contexts, under control of this variable.
...
If you set this variable to the symbol ‘always’, then the keys
will not be special in headlines, property lines, item lines, and
table cells, to make shift selection work there as well. If this is
what you want, you can use the following alternative commands:
‘C-c C-t’ and ‘C-c ,’ to change TODO state and priority,
‘C-u C-u C-c C-t’ can be used to switch TODO sets,
‘C-c -’ to cycle item bullet types,
and properties can be edited by hand or in column view.
However, when the cursor is on a timestamp, shift-cursor commands
will still edit the time stamp - this is just too good to give up.
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.
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.
Specifically, I'd like to change the key bindings of Emacs-Helm. When I run helm-find-files, if I hit C-z on a directory, you can jump into the selected directory. I'd like to change this behavior to Tab. I know the action bound to C-z is helm-execute-persistanet-action. I can achieve this by doing (global-set-key (kbd "<tab>") 'helm-execute-persistanet-action) but then that will capture all other tab actions. I only want tab to run helm-execute-persistanet-action when I'm in helm-find-files
I think what you are looking for is define-key. The expression should look like:
(define-key helm-mode-map [tab] 'a-command)
You could try to advice around the helm-find-files function to declare a variable in-helm-find-files then bind the tab key in the helm keymap using define-key. If the in-helm-find is set then you can call the function you want otherwise use keymap look up to call the function in the global map.
Advicing
http://www.gnu.org/software/emacs/manual/html_node/elisp/Around_002dAdvice.html#Around_002dAdvice
Helm Keymap
https://github.com/emacs-helm/helm/blob/master/helm.el#L101
Keymap lookup
Given an emacs command name, how would you find key-bindings ? (and vice versa)
First of all, find out what major mode is active in the buffer in which you want to change the key binding. You can do that with C-h v major-mode, or look in your mode-line.
Then, use local-set-key to create the binding for that major mode only by putting some code in the mode hook. I'm not familiar with helm, but let's say the major mode is called helm-mode, and it has a hook helm-mode-hook and the command you want to bind is called helm-do-something:
(add-hook 'helm-mode-hook
(lambda () (local-set-key [tab] 'helm-do-something)))
How do I clear a binding or edit a binding an emacs package provide?
For instance I had keybindings M-c to capitalize a word.
After I install some 3rd party emacs package, it is changed to calc-dispatch.
I'd like to use M-c as capitalize as before, and set calc-dispatch to something else.
How can I do this in general?
The keybind maps are loaded by order. The keybind map which loaded later will have higher priority. This is why the local key map will override the global keymap, because the global key map is loaded before the local key map(the mode key map). Something is wrong here. Look phils's comment.
What I solve this problem is add a hook to that specify mode to disable that key bind and rebind it to other key in that key map.
First, you need to find the key-map name which defines the M-c bind to calc-dispatch.
It is usually the combination of mode name and mode-map.
For example, the name of python mode key map is py-mode-map.
Second, remove the M-c bind in that mode and rebind to other key using hook.
For example, in python mode, I want to remove the bind C-j (py-newline-and-indent). And rebind it to C-i. Because globally I bind C-j to linum-ace-jump. This is the similar case with yours.
(add-hook 'python-mode-hook
#'(lambda ()
(define-key py-mode-map "\C-j" nil)
(define-key py-mode-map "\C-i" 'py-newline-and-indent)))
What you ask for is:
(global-set-key (kbd "M-c") 'capitalize-word)
This is in general the way to set words globally.
Maybe if you want to substite the two, you can try this:
(substitute-key-definition
'capitalize-word 'calc-dispatch (current-global-map))
(define-key KEYMAPNAME (kbd "KEYCOMBO") 'FUNCNAME)
Is for specific mode. For example: (define-key emacs-lisp-mode (kbd "M-c) 'capitalize-word).
(global-set-key (kbd "M-c") nil)
Is to generally unbind a key (globally).
You can easily find more on this by just googling.