My Simple Setup to to Avoid RSI in Emacs

This is an introduction to my setup to avoid Repeat Strain Injury (RSI) in Emacs. It's very simple.

Here is a list describing how I use Emacs:

  1. I'm familiar with and would like to use Emacs key bindings. That is, Evil is not the preferred way to navigate in Emacs.
  2. Never use ESC.
  3. I maintain my own .emacs.d but I also regularly check some popular .emacs.d repos on GitHub. For example, Steve Purcell's emacs.d, Prelude, and of course, the very popular Spacemacs.

I mainly use Emacs on Arch Linux and I have a US layout laptop PC keyboard, which is probably not possible to be replaced by other more ctrl-friendly keyboards.

Only two steps are needed!

1 Step 1: Use Space As Ctrl

This can be achieved by different tools on differnt platforms. I use xcape in Arch Linux. Just install it from AUR, and use the following script(found in xcape's README) to use Space as both SPC and Ctrl:

# Map an unused modifier's keysym to the spacebar's keycode and make it a
# control modifier. It needs to be an existing key so that emacs won't
# spazz out when you press it. Hyper_L is a good candidate.
spare_modifier="Hyper_L"
xmodmap -e "keycode 65 = $spare_modifier"
xmodmap -e "remove mod4 = $spare_modifier" # hyper_l is mod4 by default
xmodmap -e "add Control = $spare_modifier"

# Map space to an unused keycode (to keep it around for xcape to
# use).
xmodmap -e "keycode any = space"

# Finally use xcape to cause the space bar to generate a space when tapped.
xcape -e "$spare_modifier=space"

Now you can now hold down the Space key to use it as the Ctrl key. You can reach the Space key by your thumb without any problem. On a regular PC, Alt keys are beside the Space key, so you can easily press either Ctrl (remember it's Space now!) or Meta. To produce a real space, just press the Space key and release it, without holding it.

2 Step 2: Swap Escape and Caps Lock

I Never use Escape in Emacs, then why on earth do I ever want to swap Caps Lock with Escape? Actually, Escape key in Emacs is kind of special. You don't need to hold down the Escape key while using the key bindings starting with ESC, which is very different from Ctrl and Meta. I don't know how Escape key event is generated in Emacs or in X11, but from a user point of view, it is quite like other non-modifier keys. You press it down and release it, one key is generated. This is really nice since we can then use it as the "leader" key for the Emacs just like the Space key in Spacemacs.

So first, let's swap Escape and Caps Lock so we can easily reach Escape by our left pinky. I use setxkbmap:

setxkbmap -option caps:swapescape

Then remap ESC to C-c since it is the standard prefix key reserved for users:

(define-key key-translation-map (kbd "ESC") (kbd "C-c"))

So why is this useful? If you like the way Spacemacs organizes its key bindings, you can replace its key bindings with C-c as the prefix instead of Space, since all the single letter key bindings starting with C-c are reserved for users. Now let's assume that you bind C-c b b to behave like SPC b b, which is helm-mini in the case of Spacemacs. You can press the key bindings in following ways(note ESC has been remapped to C-c and swapped with Caps Lock so you can easily press it by your left pinky):

  1. ESC, b, b, just like SPC, b, b, one keystroke at a time.
  2. Holding down ESC and b at the same time and then release ESC and press b, just like how you would press C-x b.
  3. Holding down ESC and press b, b, just like how you would press C-x C-x.

The latter two are valid because ESC key event is only generated once, but I would just use the first one because in that case the pinky doesn't have to be in a stressed state by holding down some key. Just press it and release it. No extra effort needed. What's more, the Escape key, after swapped with Caps Lock, is almost as easy to be pressed as the Space key! And there is one addtional benifit: it is more robust since if you accidentally press your keyboard in the second or the third way, you can still generate the key sequence you want! You can now imagine you use Escapemacs because Escape is taking the role of Space in Spacemacs.

3 Addtional Step

Sorry, I lied. Actually we need an additional step:-). Why? Because we use Space as both Space and Ctrl. We should carefully think about how to deal with key bindings that contain Ctrl and Space. Of course, you can use the physical Ctrl key in this case, then you don't need any addition step. For me, I rebind those commands to C-c SPC, and combined with other packages such as hydra and expand-region.

(defhydra hydra-mark (:body-pre (call-interactively 'set-mark-command)
                                :exit t)
  "hydra for mark commands"
  ("SPC" er/expand-region)
  ("P" er/mark-inside-pairs)
  ("Q" er/mark-inside-quotes)
  ("p" er/mark-outside-pairs)
  ("q" er/mark-outside-quotes)
  ("d" er/mark-defun)
  ("c" er/mark-comment)
  ("." er/mark-text-sentence)
  ("h" er/mark-text-paragraph)
  ("w" er/mark-word)
  ("u" er/mark-url)
  ("m" er/mark-email)
  ("s" er/mark-symbol)
  ("j" (funcall 'set-mark-command t) :exit nil))

4 Some Other Tip: key-chord-mode, avy and hydra

key-chord-mode is great. You can press two key strokes quickly to invoke a command. I have defined many key chords, quite similar as what you can find on other people's blog on key-chord-mode, such as xf to open a file, xs to save a buffer and bb to switch buffers. I bind jj to change to a simple VI mode which is defined by hydra, and jk to avy-goto-word-1. avy is a really awesome package. It helps you quickly navigate in the visual area. I can't live in Emacs without it. Thanks to abo-abo for creating so many useful packages(hydra, avy, ace-window, lispy, ace-link, etc.)!

I know someone would always recommend me to use Evil to avoid RSI in Emacs. Evil is awesome, but I thought for me, it is too heavy-weight. I used to use Evil very often, and I used Emacs key bindings in insert mode. However, I found that I stayed in insert mode too long because of my Emacs muscle memory: all the C-a, C-e, M-f, M-b kind of commands to navigate the buffers and of course I also heavily use avy. Although sometimes I do enjoy using the VI's hjkl and other key bindings, I thought I don't really need the power of Evil. So after hydra comes out, I used it to define a very tiny and simple VI mode for myself, and that's just enough for me.

5 Final Words

Just don't rebind Caps Lock to Ctrl. It harms your pinky.

[ UPDATE 2016-02-25 ] Note: This article describes only part of my settings because I think my own full key remapping settings might be too opinionated(and thus not that helpful). Here describes a little more.

Junpeng Qiu 22 February 2016
blog comments powered by Disqus