Emacs, Lisp
Tags

Make Your Own Keymap

Posted September 17, 2007

Yesterday I was playing around with my .emacs. Spurred by a sudden need to use IRC, I was setting up ERC at the suggestion of Will Farrington.

One thing I noticed after starting up ERC a few times is that M-x erc requires a lot of repetitive user interaction. It asks for the IRC server, IRC port, and your nickname, none of which ever change1. So, being a good Emacser, I decided to make a custom function that filled it in for me:

(defun ??? ()
  "Open an ERC client with my credentials" 
  (interactive)
  (let ((passwd (read-passwd "Password: ")))
    (erc :server "irc.freenode.net" :port "6667" :nick "nex3" :password passwd :full-name "Nathan Weizenbaum")))

I wasn’t sure of the name, though. I wanted it to be something short, easy to type via M-x, and descriptive. erc was taken. my-erc was non-descriptive and took too long to type.

Then I realized: I didn’t have to call it via M-x. I could give it a keybinding.

It didn’t seem quite worth a top-level keybinding, though. I feel like those should be reserved for actual editing. This should at least be hidden behind a prefix character.

I could have gone with one of the pre-existing ones, like C-x or M-g, but it seemed like all of those has problems. I couldn’t find an unused binding for C-x. M-g is a pain to type. C-c is supposed to be reserved for mode-specific bindings.

Then it hit me: I could make my own prefix character.

Now, Emacs’ keyboard layout is very well optimized. Somewhere between creating new features and merging in those from other emacsen, every available key ended up mapped to something or other. How could I co-opt one to use as my prefix?

Luckily for me, the set of available keys for the default map was smaller than my own set. In the interest of hardware- and backwards-compatibility, as well as just tradition, Emacs by defaultdoesn’t use the arrow keys for navigation. Instead, it uses normal letter-based bindings: C-f to move forward a character, C-p to move backward a line, and so forth.

I, on the other hand, do use the arrow keys. Part of the reason is just because that’s what I was used to when I started using Emacs. I do think it makes more sense to have all the navigation keys in one place, though. In any event, it freed me up to take one of the keys I now wasn’t using for navigation and put it to work as a prefix key.

For a mixture of ergonomic and egotistical reasons, I chose C-n, usually used to move forward a line. Making it a prefix key was delightfully easy:

(define-prefix-command 'nex3 'nex3-map)
(global-set-key (kbd "C-n") nex3-map)

So far I’ve only added a couple bindings to my keymap. I’ve defined C-n e as my ERC function, which I’ve called nex3-erc, and C-n . opens up my .emacs file:

(defun .emacs ()
  "Open up the .emacs configuration file." 
  (interactive)
  (find-file "~/.emacs" t))
(global-set-key (kbd "C-n .") '.emacs)
(global-set-key (kbd "C-n e") 'nex3-erc)

Even with just these bindings, using my keymap has been very useful. I’m sure I’ll add plenty more as I more and more get fed up with M-x’ing. And I encourage all you Emacs-users to create your own personal keymap.

1 This isn’t quite true; one might want to connect to several IRC servers. But it makes much more sense to create a couple configurations than to specify everything each time.

Edward O'Connor said September 17, 2007:

Note that keystrokes of the form C-c <letter> are reserved for the user, as well are f5 through f9. So C-c n . and C-c n e don’t clobber anything.

Make your comments snazzy with Textile!