Friday, April 17, 2015

Difficulty in using <Esc> in vi[m] ?

As one grows more and more comfortable with vim, the comfort of the keyboard center line gets more and more difficult to leave. So much so that moving your finger to press escape seems like too much movement to escape insert mode.

For quite some time, I was under the impression that it's just me (and psst.. maybe I have short fingers ;). Realized soon after that there are others who have faced this before.

We can use the :imap command in the ~/.vimrc file to remap the Esc key. Having experimented with quite a few combinations, I find 'jj' to be the most convenient substitute for <Esc> key.

" Mapping jj as a substitute for <Esc>
:imap jj <Esc>

This is straight forward. There are some excellent resources which give a more detailed insight:

http://vim.wikia.com/wiki/Avoid_the_escape_key

http://vim.wikia.com/wiki/Mapping_keys_in_Vim_-_Tutorial_%28Part_1%29

The interesting challenge comes when you also use another editor in conjunction with vi[m]. I use Sublime Text 3 for most of my LAMP stack development. I use Vintageous extensively to lay down the familiar cushion of vim within Sublime Text. Getting the above key remap to work with Vintageous turned out to be a small challenge. There are multiple suggestions available and not all of them work :(

The following keymap entry works for me:

    { "keys": ["j", "j"],
      "command": "_enter_normal_mode",
      "args": {"mode": "mode_insert"},
      "context": [{"key": "vi_insert_mode_aware"}]
    }

Source: https://github.com/guillermooo/Vintageous/wiki/Using-jj-instead-of-Esc

A very interesting piece of history I picked up from the vim wikia site

The Vi editor was originally written on an ADM-3A terminal, which had the Escape key positioned where the Tab key occurs on most modern keyboards. In the modern keyboards pressing [Esc] generally requires stretching to the top of the keyboard and since [Esc] is possible the most important key in vim, it makes sense to remap it to something more convenient.

No comments: