
By Mickey Petersen Altering the key bindings in Emacs should not, on the face of it, be a difficult task. But there’s a reason why the Emacs manual has dedicated 30-odd pages to describing, in great detail, all the subtleties and nuances of how to bind keys. To save you the time of reading all of that, I’ve written a guide that covers what you need to know to bind keys to even complex commands, and a set of templates you can use in your own code. This guide will assume that you have little or no knowledge of elisp What makes up a key bind? Emacs is “self-hosting” and, depending on who you ask, close to achieving sentience. Almost all of Emacs is written in Emacs-Lisp (weighing in at roughly 1.2 million lines of code!), and most of the complex elisp functions you use are in turn built with simpler building blocks and so on, right down to the core C source code layer.
Emacs’s key engine is very similar, because Emacs lets you control almost every facet of self-insertion and other key binds. For instance, when you press “f” it runs self-insert-command, a special command that repeats the last typed key N times. So if you type C-u 10 f you will see ffffffffff printed on your screen. As you can see, every keypress – even elemental ones like typing a character on the screen – has an associated key binding that you can modify or even remove. Keymaps A keymap is an internal data structure used by Emacs to store keys and their associated actions.
Keymaps are rarely modified directly, but through a set of commands that manipulate the data structure for you. Most Emacs users will never interact with keymaps aside from indirectly assigning keys to them.
Drag the Emacs Application to your Applications folder. Do shell script '/Applications/Emacs.app/Contents/MacOS/Emacs --daemon' end tell. Created 6 years ago, I was dubious of it's accuracy, but it's still working for me today 2018-02-23!
Every buffer and most major and minor modes have a keymap, and that keymap defines what the keys do when key sequences are sent to that buffer. Keys can be divided into three categories: undefined, prefix key, or complete key. Undefined is self-explanatory: it does no operation when it is invoked.
Prefix keys are keys like C-x and C-c; they are parts of a complete key, and each constituent part of a prefix key is made up of its own keymap. A complete key is a command that, when input, executes its associated command. It is possible for Emacs to enumerate all the active minor and major mode key bindings in a buffer by typing C-h m.
This command is very useful if you want to learn more about what a major or minor mode can do. Likewise, you can type out parts of a complete key (say M-s) and then type C-h to get a list of all keys that belong to that prefix. Key Bind Commands There are several ways you can define (or undefine) keys, as the table below shows, but in reality there are dozens of ways you can hack the keymaps. (define-key KEYMAP KEY DEF) Defines a key against a keyboard map. Use this if you want to change a keymap that isn’t the current buffer map. (local-set-key KEY COMMAND) Binds a key to the local keymap used by the active buffer, unlike define-key which takes an explicit keymap to bind a key against.

(global-set-key KEY COMMAND) Binds a key to the global keymap, making it available in all buffers (with a caveat – see below.) (global-unset-key KEY) Removes KEY from the global keymap (local-unset-key KEY) Removes KEY from the active, local keymap. Representing Keys in Code In order to actually bind a key you must first tell Emacs what key you intend to use. Unfortunately there’s more than one way of representing keys in Emacs: as a string, or as a vector. We won’t concern ourselves with either, as we will use a handy macro built in to Emacs called kbd. The kbd macro translates a human-readable key into a format Emacs can understand.
Emacs App (v 23 For Mac)
One important point to note is that you must surround function and navigation keys with. Those keys include F-keys, arrow keys and home row keys, like so:,.
Download Emacs For Mac
But if you want represent the key C-c p then write (kbd 'C-c p'). Remapping Commands You can tell Emacs that you want to replace all keys pointing to a certain command with one of your own choosing by using the remap event; this should be done instead of passing a key to the key bind function you are using. This is arguably the best way of replacing existing commands with your own as Emacs will automagically handle the key reassignment in the background. Example: (define-key (current-global-map) [remap kill-line] 'my-homemade-kill-line) Here I globally remap all key binds that point to kill-line to my-homemade-kill-line. For some more hands-on examples read my article Fixing the mark commands in transient mark mode. Reserved Keys You can pick any keyboard combination you desire – even if that key bind is already taken, so be careful – but Emacs has set aside certain keys for use by users. Generally, all keys prefixed with C-c?