Custom Global Emacs Bindings with Key Chord and the Semi-Colon Key

Incessant tweaking of my Emacs setup is easily my nerdiest hobby these days. Sure, there are plenty of modern text editors that come out of the box basically fully-featured, but what’s the fun in that? Despite the learning curve, I think learning Emacs and molding it with lisp is a great creative exercise that can yield some powerful efficiencies.

One of my newly favorite lisp packages is key-chord-mode. With it, you can bind commands to combinations of key-strokes. For example, I’ve set the chord of the two angle bracket keys <> to execute sgml-close-tag, which creates a closing tag for whatever unclosed HTML/XML tag you are inside. Similarly, I’ve set the curly brackets keys {} to execute open-brackets-newline-and-indent which is a custom defun for opening a CSS definition, JavaScript function, or Ruby block and setting the cursor inside it.

My favorite way to abuse key-chord though is to treat the semi-colon as my own prefix for custom global bindings. Here is what my semi-colon chords currently look like:

(key-chord-define-global ";b" 'ido-switch-buffer)
(key-chord-define-global ";r" 'recentf-ido-find-file)
(key-chord-define-global ";f" 'ido-find-file)
(key-chord-define-global ";g" 'magit-status)
(key-chord-define-global ";m" 'ace-jump-mode)
(key-chord-define-global ";c" 'hemacs-todo-capture)
(key-chord-define-global ";t" 'hemacs-goto-todos)
(key-chord-define-global ";o" 'magit-in-perspective)
(key-chord-define-global ";s" 'persp-switch)
(key-chord-define-global ";l" 'persp-shell)
(key-chord-define-global ";d" 'dired-jump-other-window)
(key-chord-define-global ";a" 'ack-and-a-half)

Since this is basically an empty binding namespace, you can use the most memorable mnemonic letters (“o”pen project, switch “b”uffer, etc). You don’t have muck around with overriding or conflicting with command prefixes between lisp packages or memorizing multi-command. You also don’t have to rely on bindings involving the super key (⌘ in OSX), which may conflict with system-level bindings. That means they will work inside Apple Terminal or iTerm as well as Carbon Emacs.

The biggest consideration to make when defining chords in general is to stray from key combinations you might accidentally fire when typing away. The semi-colon approach works for me because I’m not writing much JavaScript in these wonderful CoffeeScript days, and when I do I’m not typically ending my lines in semi-colons, since extraneous whitespace on lines is deleted in my before-save-hook. My CSS bindings spit out the semi-colon for me as well, so I haven’t had any conflicts so far. The runner-up character candidate I was thinking of using for this chord namespace was the ` key, but it’s way up in the top-left of the keyboard and the semi-colon is ergonomically placed straight up downtown in the home row keys.

You can browse my entire Emacs setup here.