Devilish fun with a modeless modal editing mode

5 min read Original article ↗

As I write this, I am imagining one of those slightly cheesy voiceovers for a TV show, reminding viewers of the previous episode: “Last time on ‘BSAG Gets Nerdy About Emacs’…”. You may remember that I was on the Emacs from Scratch cycle again, and enjoying the process of learning the default Emacs keybindings, instead of installing evil-mode like I usually do. Well, I am still doing that, but a random meander around the Emacs-y corners of the internet turned up susam’s devil-mode, a sort of modeless modal editing mode. A what now?

I know, I had trouble wrapping my head around that myself initially. Devil mode very cleverly combines elements of god-mode (letting you enter standard Emacs chords sequentially rather than simultaneously) with aspects of evil-mode navigation, but using standard Emacs shortcuts rather than vim-specific ones.

The clever trick that devil-mode pulls off is that — like standard Emacs keyboard shortcuts — you don’t need to enter or exit a special mode in order to issue a command. It does this by using the comma key as a kind of leader that substitutes for the Ctrl modifier in commands. I know this sounds like a crazy plan. Commas are common in programming and normal text, so how does that work? I should say that this is configurable, so you can choose another key if you like. However, using the comma works out remarkably well, because there are built-in translators that substitute two successive commas into a literal comma, and a comma at the end of a word followed by a space into a comma and space. The latter works well in almost all real world cases, so you rarely have to use the double comma trick.

In devil-mode, a comma represents Ctrl. So issuing the command for find-file becomes ,x,f entered successively, rather than C-x C-f. Commands with a chord followed by a single key (like C-x k for kill-buffer) would use ,xk. Chords using M- or C-M- are also catered for using ,m and ,mm respectively. Finally, since comma space (by default, you can change it) translates to a literal comma space, it becomes impossible to issue C-SPC to set the mark using devil-mode. However, another sequence of keys acts as an alternative way to represent Ctrl so you can enter ,z SPC instead. As I said, this is configurable, so if you would rather always enter two commas for a literal comma, you can reverse this behaviour.

Of course, the beauty of devil-mode is that you can very freely mix and match standard Emacs keystrokes with the devilish versions of them, and because you never need to think about what mode you are in, this feels easy and fluid. Some devilish versions require more keystrokes than the Emacs version (e.g. M-x vs. ,mx), so I find that I tend to use the Emacs versions for those. However, there are times when holding down chords feels tiring, so you can use the devil version instead. The best part is — if you are familiar with vanilla Emacs commands — you don’t need to learn anything new. The mapping between Emacs and devil-mode commands is extremely straightforward.

Another benefit is that the mode cleverly works similarly to Emacs’ repeat-mode to chain together commands that belong in the same sequence, so that you can use single keys (not even having to use the comma activation after the first instance) to navigate around semantic units for example. So you can set things up so that after you enter ,n (for example) to move to the next line, you can enter just p to go back up a line, a to go to the start of the line, e to the end and so on. Entering any other key or C-g gets out of the repeat mode. I have set up a similar chain for semantic movement around text by word or sentence which is very handy for quickly bopping around text while editing. devil-mode uses its own syntax to define these keys, which I personally find much easier to use than the Emacs repeat-mode syntax. However, if you prefer the latter, you can disable devil-mode repeatable keys and use the Emacs configuration instead.

I’ve used devil-mode for a few weeks now, and really like it for its flexibility and transparency. I have found very few downsides to its use. One is that — by default — which-key doesn’t recognise the comma activator and so does not pop up the relevant available commands. This is a known issue, and I have used wesnel’s fork of which-key to solve this. The second very minor issue is that when you are in the only properly modal bit of devil-mode (repeatable keys) you don’t see any visual indication that you are in that mode. This is also a requested feature which would be nice to have, but in practice, C-g is an easy escape hatch, and since I tend to use the feature to quickly hop around then exit the repeatable keys mode, it isn’t much of an issue in practice.

I get the impression that not many people know about devil-mode, which is a great shame, as I think it is the perfect solution when you want to use vanilla Emacs, but you also get a bit tired of holding chords down. I should mention that this is especially handy if you use a split keyboard with homerow mods mirrored on each side of the keyboard (as I do), as there can be a bit of hand-switching needed for some Emacs chords. Anyway, give devil-mode a try — you might find you love it.