Wednesday, December 29, 2010

Finding command names and customizing key-bindings in Emacs

Of the zillion things that make Emacs a great editor, one is the ability to access all its functionality using the keyboard. However, you may find that a particular key combination is not convenient for you. For example, I find it inconvenient to use M-/ (pressing the ALT key and the / key together) for word completion. No problem! Of those zillion things, another one is the ability to modify the key bidings to your liking. I explain below how to assign word completion to S-SPC (that is, getting word completion by pressing Shift and Space keys together).

1.   The first step in customizing key bindings is finding the name of the command for a given key sequence. This is done using:

    C-h c key-sequence    

Note that you should not type the key-sequence, you have to press it the way you would for invoking the command. For example, to find the name of the command that is executed when M-/ is pressed, you cannot type letter M followed by a /, you have to press ALT and / together. This will return:

     M-/ runs the command dabbrev-expand     

in the minibuffer. There, you know the name of the command.

2.   The second step is to bind your custom key-sequence to this command. Suppose you want dabbrev-expand command to be executed when Shift and Space keys are pressed together. Do the following: open ~/.emacs (or, whichever is your Emacs init file) and add the following line to that file:

     (global-set-key (kbd "S-SPC") 'dabbrev-expand)     

3.   Close Emacs and restart. You can now use your custom key-sequence for the command you assigned to it.

If you are thinking, "okay, from your example I know that I have to use
S for Shift key and SPC for the Space key, but what do I use for some other keys, say the function keys. You can find useful information regarding this and a lot more about custom key bindings at

No comments:

Post a Comment