We’re rewarding the question askers & reputations are being recalculated! Read more.

Hot answers tagged

61

Emacs 25 As mentioned by @YoungFrog in the comments, starting with Emacs 25.1, the good-old C-h k method of describing key-binds will also tell you which keymap the key was found in. Before Emacs 25 There’s some code here on this, but it’s incomplete as it does not cover everything. Below is an improved version of it. Keys can be bound in 9(!) ways. ...


41

C-h b will list all the bindings available in a buffer. This is a mnemonic for help (C-h) bindings (b). You can also get a list of keybindings via C-h m, which is help for the major and minor modes for the buffer. The formatting of this is a little clearer, but includes additional information about the modes beyond just listing the keybindings.


39

There is a "shortcut" approach too for the same solution if you don't want to define your own minor mode (that I talk about in my first answer). You can install the use-package package available from Melpa and make use of bind-key* or bind-keys* macro that's part of the bind-key package that ships with use-package. From the documentation of bind-key.el: ;;...


25

You can define your own minor mode and its key map and have that override all other modes (minor + major). That's exactly why I chose to write my own minor mode. Steps to have your key bindings override all bindings: Defining your own minor mode and key map as shown below. Activate your minor mode globally (define-key my-mode-map (kbd "C-j") #'newline-and-...


23

I use the command helm-descbinds, which is available via melpa. I mapped it to C-h b because it is basically a drop-in replacement for describe-bindings. The benefit is that it is easier to navigate and search than the output of describe-bindings because you can easily search for keywords and the helm buffer will narrow to show all of the matches, and ...


23

I tested with the following script, (key-description (car (sort (let (keyseqs) (mapatoms (lambda (sym) (when (commandp sym) (let ((where (where-is-internal sym nil nil t))) (when where (setq keyseqs (append keyseqs where))))))) keyseqs) (lambda (a b) (< (length b) (length a)))))) And ...


21

Modify company-active-map accordingly: (with-eval-after-load 'company (define-key company-active-map (kbd "M-n") nil) (define-key company-active-map (kbd "M-p") nil) (define-key company-active-map (kbd "C-n") #'company-select-next) (define-key company-active-map (kbd "C-p") #'company-select-previous))


21

The future is long gone, and the stone age of computing is just about to come. All text terminals I know still send the exact same byte-sequence to Emacs for C-i as for TAB, so the original need to "unify" them is still very much with us. The input-decode-map (à la (define-key input-decode-map "\C-i" [C-i])) is probably about as good as it gets right now. ...


20

You can use with-eval-after-load to defer the key binding until after a certain module has been loaded (and thus defined the keymap): (with-eval-after-load "helm" (bind-key "M-Y" #'helm-end-of-buffer helm-map)) Use C-h v helm-map to find which module the keymap is defined in, and thus what to put in the string on the first line. with-eval-after-load was ...


16

Taken from the Emacs Lisp Manual (see Controlling Active Maps): (use-global-map (make-sparse-keymap)) To quote the manual: "It is very unusual to change the global keymap.". Safety note: Make sure you can save buffers and cleanly exit Emacs afterwards with a different keymap, otherwise you'll have to kill it without saved changes.


15

Evaluate the following using eval-expression (M-:) or in a scratch buffer: (describe-key (kbd "C-<whatever>"))


14

For closure, here is an answer that expands upon my comment. I define a minor mode with the required keybinding and toggle it in the required buffer using file local variables. (define-minor-mode my-org-buffer-local-mode "Minor mode to simulate buffer local keybindings." :init-value nil) (define-key my-org-buffer-local-mode-map (kbd "<f10>...


14

I don't think that this can be achieved from a terminal, but in GUI mode you could try this: (define-key input-decode-map [?\C-i] [C-i]) (global-set-key (kbd "<C-i>") 'indent-region) I do the same thing with C-m so that it can be distinguished from RET EDIT: The following should work whether you are in GUI or TTY mode: ;; Unbind <C-i> from ...


14

help-key-description is used to display a humanly-readable key in the documentation when you invoke describe-key (C-hk). (help-key-description [22] nil) ;; --> "C-v" (help-key-description [67108903] nil) ;; --> "C-'" or (string= (help-key-description (kbd "C-g") nil) "C-g") ;; --> t


14

Motion state is an Evil-specific thing, intended for modes where you don't edit text, but still want Vim-style movement available, with all other keys of that mode passing through. Help buffers are an example of such a case, the package.el listing is another one. Typically you want commands moving point in evil-motion-state-map and everything else in evil-...


13

GUI frames In GUI frames (whether X11, Windows, OSX, …), Emacs reads the Tab key as the tab function key. However, because the Tab key on terminals traditionally sends the ^I (Control+I) character, Emacs translates the tab function key into the Control+I character (character 9), which is displayed as TAB. This translation is done via function-key-map. A ...


13

There is no easy way to know exactly what a single key press will do. If you see additional behavior always check the common hooks. See the list here: http://www.gnu.org/software/emacs/manual/html_node/elisp/Standard-Hooks.html In most cases the important ones are: after-change-functions before-change-functions first-change-hook post-command-hook pre-...


12

You can do this by wrapping a call to org-agenda into a custom command and binding that to a key: (defun org-agenda-show-unscheduled (&optional arg) (interactive "P") (org-agenda arg "u")) (define-key org-mode-map (kbd "<f8>") 'org-agenda-show-unscheduled)


12

This turned out to be simpler then expected. As suggested in the comments here and on the question: (with-eval-after-load 'erc (require 'markdown-mode) (require 'cl-lib) (setq erc-mode-map (make-composed-keymap (cl-copy-list erc-mode-map) markdown-mode-map))) This will create a keymap which is a copy of erc-mode-...


12

Without remapping, you can already get by pretty well without arrow keys. C-c C-x C-h in an Org buffer: C-c C-x D org-shiftmetadown C-c C-x L org-shiftmetaleft C-c C-x R org-shiftmetaright C-c C-x U org-shiftmetaup C-c C-x d org-metadown C-c C-x l org-metaleft C-c C-x m org-meta-return C-c C-x r org-metaright ...


10

I've got it working now, thanks to your answers: (defun my-jump-to-tag () (interactive) (evil-emacs-state) (call-interactively (key-binding (kbd "M-."))) (evil-change-to-previous-state (other-buffer)) (evil-change-to-previous-state (current-buffer))) (define-key evil-normal-state-map (kbd "C-]") 'my-jump-to-tag) This will set evil-state to "...


10

Although not the canonical emacs way of doing things, I quite like using discover-my-major for that purpose because it just feels more effective. It is available on melpa and is powered by the makey library, which is responsible for those nice menus magit is known for. I'd suggest you check out the github link for a screenshot demonstrating the ...


9

If you can't use a particular key combination because of your terminal, you can often fake it by manually simulating the key modifier. The following combinations work exactly as though you had used the corresponding modifier key: C-x @ a alt C-x @ m meta C-x @ c control C-x @ h hyper C-x @ s super (lowercase s) C-x @ S shift (...


9

Here is my super hacky way to simulate key down/up event binding by taking advantage of timers. Overall I would suggest going by Sigma's answer, but you asked for a way to close the preview by letting go so I'm obliged to try. Basically what you can do is bind some function that will be your "keydown" function to a keybinding and inside that action, start ...


9

You can use funcall and key-binding to do this: (funcall (key-binding (kbd "C-M-j"))) will execute the function currently bound to C-M-j. funcall lets you pass in arguments as well, if needed. Alternatively, you can use call-interactively to call a command bound to a key like this: (call-interactively (key-binding (kbd "C-M-j")))


9

Solution In order to execute stuff after a given package is loaded, you need to put that after :config in use-package. Here's an example using the snippet in your question: Snippet # 1 (use-package helm :config (progn (bind-key "M-Y" #'helm-end-of-buffer helm-map) (bind-key "M-k" #'helm-next-line helm-find-files-map))) (use-package gnus :...


8

As pointed in comments, functions are bound to keys, not events. But to take a step back, I'm not sure I understand why it's important for you to keep the key down while you're (presumably) reading the content of the file. It would also be incompatible with basic (and reasonable) actions like scrolling to get more of it. Not to mention the fact that if it ...


8

calendar-mode-map is void since it's not loaded yet: (eval-after-load "calendar" `(progn (define-key calendar-mode-map (kbd "M-]") 'calendar-forward-month) (define-key calendar-mode-map (kbd "M-[") 'calendar-backward-month)))


8

The input translation maps (input-decode-map, local-function-key-map, function-key-map, key-translation-map) apply only within a key sequence. While you can specify multiple keys in the output of the translation, they'll only be counted towards binding. For example, consider: (define-key key-translation-map [f12] [?\C-x ?\C-f ?a]) (define-key key-...


8

Adapted from my own config: (define-key input-decode-map [?\C-\[] (kbd "<C-[>")) (global-set-key (kbd "<C-[>") 'butterfly) This will obviously only work in the GUI. edit: Note that input-decode-map is terminal-local which means modifying it won't work if you're using emacsclient, but will do if you're using emacs. I've fixed the issue in my ...


Only top voted, non community-wiki answers of a minimum length are eligible