I started using Emacs (currently for org mode only). I don't use priorities in my TODOs, hence I'd like to disable S-UP and S-DOWN key bindings (which loop through the priorities). How can I do this?
#lawlist gave you the recipe in his comment. Here's how to find this out for yourself.
See if there is a keymap variable for the mode in question - typically there is one. In this case, try C-h v org-mode-map. If you find no such variable, fish around a little, using the apropos commands - for example, M-x apropos-variable org-mode.
Bind the key(s) in question to nil in that keymap:
(define-key org-mode-map (kbd "S-<up>") nil)
C-h m gives you info about the current mode. Sometimes it lists the important key bindings for the mode. And C-h b (anywhere) lists lots of key bindings for the current context.
If you want to see all of the key bindings that belong to a given keymap variable (in human-readable form), then load library help-fns+.el and then use C-h M-k followed by the keymap variable name (e.g. org-mode-map). See Help+.
Related
Is there a way to filter the list of available bindings (C-h b), so it will not show all the possible bindings but just the relevant ones for the used mode?
For example on org-mode, I get all the general C-x list plus all the bindings of the modes that have general key bindings plus all the major mode bindings, followed by all the org mode ones, followed by global bindings... 1369 lines in total... I'm using this view for learning my way around, perhaps there is a way to filter so as to find my way around.
I think you are looking for C-h m which runs the command describe-mode.
You will find more goodies in C-h C-h which runs the command help-for-help.
C-h m shows the doc for the current major mode, as well as currently enabled minor modes.
Often C-h m lists some of the more important local key bindings, that is, some of the bindings made for the current major mode. But not always, and typically it does not list all of the local bindings.
If you use library help-fns+.el then you can use command describe-keymap to list all of the local key bindings:
M-: (describe-keymap (current-local-map))
If you know the name of the local keymap variable (e.g. emacs-lisp-mode) then you can invoke describe-keymap interactively using C-h M-k, providing the map name at the prompt.
If you use Icicles then you can see all of the currently available key bindings using S-TAB (key completion). By default, the local bindings (i.e, those for the current major mode) are shown first, and are highlighted specially. (You can use C-, to sort the candidate bindings in other ways (by key name, prefix keys first; by command name).
After C-h b switch into Help-buffer and call M-xoccurRETorgRET which will display all lines containing "org".
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.
To help me learn to work with various emacs modes, I would like to have a second monitor with a little HTML page that is used for showing me what sorts of things I can type or key-chord on whatever I'm currently looking at in emacs.
So how can I get a list of all the commands or key-chords available to me in my current mode?
Someone else will no doubt tell you how to get a cheatsheet such as you request (well, here is info about that too).
But if you want something that tells you dynamically what keys are available in the current context, no matter what it is, then this is what I have to offer:
C-h m tells you about the current (major) mode. C-h b tells you about currently available keys.
The Icicles feature key completion gives you access to all of the currently available key sequences, via key S-TAB. If you use a prefix key first, then S-TAB, then you see all the completions of that prefix key. You can move up and down the key hierarchy, including even menu items, to see all possible keys. You can use C-M-RET to get help (info about) any given key that is available. Here is some more about this feature of showing you all currently possible key bindings.
I would very much like to know good answer to this question myself! At present I am using this simple function to display key bindings for the current major mode in *Help on keys* buffer:
(defun describe-current-bindings (mode)
"Show key bindings for the current major mode in *Help on keys* buffer."
(interactive)
(with-current-buffer (get-buffer-create "*Help on keys*")
(erase-buffer)
(insert (documentation mode))))
And then use defadvice to call the function automatically whenever I switch buffers or windows:
(defadvice switch-to-buffer (after display-keys-buffer activate)
(describe-current-bindings major-mode))
(defadvice select-window (after display-keys-window activate)
(describe-current-bindings major-mode))
Now I can open *Help on keys* buffer in another frame and move that frame to my second monitor.
If you use other functions to switch windows (from windmove package, etc) you may need to add defadvice for them as well.
Try the pacakge help-fns+.el, there are some useful functions: describe-mode - "Display documentation of current major mode and minor modes.", describe-keymap - "Describe bindings in KEYMAP, a variable whose value is a keymap.", etc. For example,
(describe-keymap 'global-map) ;; global bindings
(describe-keymap 'emacs-lisp-mode-map) ;; major mode bindings
(describe-keymap 'smartparens-mode-map) ;; minor mode bindings
key-binding probes keys in the currently active keymaps. For those keymaps, such as minibuffer ones, or isearch-mode-map which are restrictive and become inactive as soon as the user presses a key outside of a limited set of defined keys, I am not able to invoke key-binding without deactivating those keymaps.
How do I:
Determine which keymaps come into effect after invoking certain commands (for example, isearch-mode-map is set as the overriding-local-map by isearch-forward-regexp) in a way that does not involve analyzing source code. Is there a hook that I can use to track/log the state of a variable?
Probe keys in those keymaps. For example, to what is RET bound in isearch-mode-map?
My closest solution has been to bind this function:
(defun probe_keybinding ()
(interactive)
(message (prin1-to-string (key-binding (read-key-sequence-vector "Enter key to probe"))))
)
to an uncommon key like 'S-f9' and invoke it when the keymaps in which I am interested are active (eg in the middle of a find-file in the minibuffer or a eval-expression). This does not always work, for example, isearch-forward-regexp exits as soon as a non-recognized key is entered.
There's no easy way to determine which keymaps will come into use in response to particular commands. Any function can call (use-local-map ...) whenever it likes, so the only way to be sure is to read the source code.
Having said that, the Emacs code does follow conventions that make it possible to find the answer in many cases. If foo-mode has a keymap, then the keymap will usually be named foo-mode-map. You can see a list of variables with names ending in -map by running M-x apropos RET -map$ RET.
You can look up a key in a keymap with the function lookup-key. See "Functions for Key Lookup" in the Emacs Lisp manual. So to find out what RET is bound to in isearch-mode-map, evaluate:
(lookup-key isearch-mode-map (kbd "RET"))
===> isearch-exit
Another element of the answer is to look at individual keymaps. Unfortunately, if you just do C-h v isearch-mode-map (or any other keymap variable) you will see a Lisp expression that is not very readable.
Instead, you can use describe-keymap, from library help-fns+.el. It is bound to C-h M-k, and it shows you all of the (non-menu) keys bound in a keymap, in a human-readable way. More description here.