Vim without Esc

As I have mentioned in a previous post, I consider the Esc key on my keyboard to be not only small but also poorly positioned. For a frequent user of the Vim editor, this is a major issue, as Esc is one of the most used keys. It is through Esc that we access the command mode (normal mode).

Even on more common keyboards, the long path to press this key does not seem natural. And indeed, it is not!

When Bill Joy created vi, he used an ADM-3A. Just by looking at the keyboard of this computer, we can understand the reason behind some key choices: Movement arrows were printed on the HJKL keys, and Esc was located where we now have the Tab key.

As always, there is more than one way to work around this issue. Here, I will show 5 alternatives to the Esc key.

Ctrl-[

In Vim, Ctrl-[ has exactly the same function as Esc. Although they are two keys, using this command is much more practical and requires less movement. It may take a few weeks to get used to it, but the advantage is that it does not require any extra configuration, as it is a built-in command in Vim. (See :help i_CTRL-[)

Ctrl-c

In other editors, this is usually the command for copying text, but in Vim, Ctrl-c is used to exit insert mode and go to normal mode. It is not exactly identical to Esc, but in practice, it is similar enough. However, on my keyboard, the Ctrl-[ command is much easier. (See :help i_CTRL-C)

Alt/Meta

In my terminal, typing Alt+some_key is equivalent to pressing Esc followed by that key. It is not exactly the same as Esc, as it involves an additional action or movement and only works in some terminals. However, it is quite useful. E.g.:

  • Alt+hjkl: Movement
  • Alt+o: Opens a new line below the current position
  • Alt+A: Append to the end of the current line
  • Alt+p: Paste at the current position

Mappings in .vimrc

Some people map Esc to a key combo, the most interesting ones are:

imap ;; <Esc>
imap jk <Esc>
imap ii <Esc>

However, you will have problems if you ever need to type ;;, jk, or ii.

Swap Esc with Caps Lock

Finally, my favorite option, but it requires a bit more configuration because Caps Lock is a modifier key that cannot be mapped by Vim.

We will have to modify the key’s function system-wide, which honestly is not a problem since I rarely use Caps Lock. There is more than one way to do this.

Setxkbmap

The simplest way is to use setxkbmap, which has an option specifically to swap Caps Lock with Esc:

setxkbmap -option caps:swapescape

Add this command to .xinitrc if you want it to be executed every time you start X. In my case, I also use setxkbmap to configure my entire keyboard layout:

setxkbmap -model pc105 -layout gb -variant intl -option caps:swapescape

Xmodmap

If you don’t want Esc to become Caps Lock (and keep two Esc keys), you can use Xmodmap.

  1. Add the following lines to a file (e.g., ~/.Xmodmap):

    remove Lock = Caps_Lock
    keysym Caps_Lock = Escape
    
  2. Edit your .xinitrc to set the mapping every time you start X11:

    xmodmap ~/.Xmodmap
    
  3. I found this script on the internet. It also uses xmodmap but simplifies the process of toggling Caps Lock on and off:

    #!/bin/sh
    case $1 in
        off)
            xmodmap -e "clear lock"
            xmodmap -e "keycode 0x42 = Escape"
            ;;
        on)
            xmodmap -e "keycode 0x42 = Caps_Lock"
            xmodmap -e "add lock = Caps_Lock"
            ;;
        *)
            echo "Uso: caps {on|off}, liga e desliga o Caps Lock"
            echo "Quando desligado, ele funciona como a tecla Esc"
            ;;
    esac
    

Terminal

One issue with the previous solutions is that they only work in X. To swap Caps Lock with Esc in the terminal, simply run the following command:

julio@julio-acer ~> (echo `dumpkeys | grep -i keymaps`; echo keycode 58 = Escape; echo keycode 1 = Caps_Lock) | sudo loadkeys -

For convenience, add an alias to ~/.zshrc:

alias nocaps='(echo `dumpkeys | grep -i keymaps`; echo keycode 58 = Escape; echo keycode 1 = Caps_Lock) | sudo loadkeys -'

Update

Nowadays (2022) I found the ideal solution, which is to have a configurable keyboard.

I bought a Moonlander Mark I and mapped the Caps Lock to the key to the left of A.

No software changes needed.


Julio Batista Silva
Julio Batista Silva
Data Engineer

I’m a computer engineer passionate about science, technology, photography, and languages. Currently working as a Data Engineer in Germany.

comments powered by Disqus