Sunday 28 July 2024

Devil mode in Emacs on the console
==================================

Emacs on the console comes with some challenges because several key
bindings can't be used, like `C-h k'.

Recently, "But she is a girl" posted a interesting article on their
blog [1]. It is about using `devil mode' on Emacs [2], written by
Susam.

This mode allows you to use the comma key in Emacs commands to replace
the control key. The mode is clever enough to let you type a comma
when needed, so nothing is lost.

Emacs being Emacs, alternatives for often used key bindings can be set,
like:

    (keymap-global-set "C-c <SPC>" 'mark-sexp)

but perhaps `devil mode' offers a more general solution. Therefor,
I decided to give a try.

Installed Devil mode
--------------------
I manage my Emacs packages with `use-package'. This is how I added
devil mode.

    (use-package devil
      :ensure t
      :init
      (global-devil-mode)
      (global-set-key (kbd "C-,") 'global-devil-mode)
      (setopt devil-all-keys-repeatable t))

This activates devil mode globally, and with `C-,' you can toggle the
activation of devil mode when needed.

The option `devil-all-keys-repeatable' make all devil commands
repeatable.

General usage
-------------
In the Emacs commands, replace Ctrl with `,' and Meta with `,m'.
Replace Ctrl-Meta with `,mm'.

Some examples:

,x.f : visit a file
,mx  : interactively issue a command
,m:  ; evaluate an sexp
,mm<space> : mark a sexp

The option `devil-all-keys-repeatable' is very handy when using
`.mm<space>': just enter more spaces to expand the selection
with more sexps.

The function 'select sexp' is not only useful when editing lisp code,
it can also be used to select one or more consecutive words in text.

Addition, not replacement
-------------------------
Devil mode acts as an addition, and not as a replacement. You can
mix the usage of Ctrl and Meta with the use of the comma method of
Devil mode.

Not only for the console
------------------------
Devil mode makes Emacs on the console or in a terminal just as
usable as Emacs in a graphical environment.

It helps to overcome the challenges from the combined use of laptops
with a PC keyboard layout and MacBooks with a Mac keyboard layout and
ends the struggling with the Meta and the Command key on Mac
keyboards.

No new bindings
---------------
"But she is a girl" mentions:

    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.

This is true, but still one has to gain some new muscle memory. And
using Devil mode is often less efficient as the vanilla Emacs key
bindings. It can help to avoid the so-called `Emacs pinky'.

Because Devil mode is an addition, and not a replacement, it doesn't
hurt to install it, The only `price' you have to pay, is that entering
a comma into your text works a bit different. In practice, when
writing text, I always add a space after a comma, and this is enough
for devil mode to actually enter a comma and not start a command
sequence. Only in non-text edits, like code, entering a comma requires
typing the comma twice, which IMHO is a very small price to pay.

For me, devil-mode is a keeper, and I am grateful to "But she is a
girl" for having pointed it out. Without the blog post, I probably
would never have discovered it.


[1]: https://www.rousette.org.uk/archives/devilish-fun-with-a-modeless-modal-editor/
[2]: https://susam.github.io/devil/


Last edited: $Date: 2024/07/28 08:07:06 $