Tuesday, October 12, 2010

Vim: Escape

UPDATE: Scroll all the way to the bottom for the best VIM ESC mapping in the world!

Vim is a modal editor, meaning it has modes. It's modes are command mode, and edit mode (AKA normal mode and insert mode). Edit mode allows you to add and delete text and move your cursor around. Command mode allows you to execute various commands that can operate on the text.

Most editors only have edit mode. To execute commands you use shortcut keys that must depend on non-printable keys like CTRL, ALT, SHIFT. You quickly run out of simple combinations (ex: CTRL+S), so you have to introduce more complicated key combinations (ex: CTRL+K, CTRL+C). Visual Studio calls these "chords." I call it keyboard gymnastics.

Some people like the "chord" approach, and other people prefer the mode approach. I like the mode approach because if I'm going to execute non-trivial commands, I'd like to type a command.  The main downside to the mode approach is you have to get back and forth between the modes. When you launch Vim and open a file you start out in command mode. There are a bunch of useful different ways to get into edit mode, all of which are just a single keystroke: i, a, o, O, I, A.

Once in edit mode, getting back to command mode is a bit harder because now typing a key will print the character, so Vim uses ESC to leave edit mode and go back to command mode.  Sadly, on modern keyboards this goes against everything Vim stands for because the ESC key is so far away from the home row.  And if you're using Vim properly, you'll be going back and forth between these modes A LOT. So we need a better way.

Vim has an alternative built right in: CTRL+[

(You've mapped your Caps Lock key to Control, right?)

This is kind of an uncomfortable keystroke, but with practice you will get used to it.  If you'd rather not, you CAN map your own keyboard combination to Esc.  For example, you could try CTRL+space:

inoremap <C-space> <Esc>

Whatever you use, I strongly suggest you try to stop using the Escape key.

UPDATE:
A number of people in the comments suggested using various combinations of the j and k keys.  I didn't even realize you could remap printable characters, but you can!  I've been using this for quite a while now and I'm completely addicted!  I call it the "smash":

Add this mapping to your vimrc:
inoremap jk <esc>
inoremap kj <esc>

Now all you have to do is smash the j and k key.  It doesn't matter which you type first!  Just smack them both as if it was a single key!  Absolutely brilliant!

12 comments:

  1. "jj" is a great "escape".

    See http://www.reddit.com/r/vim/comments/dq572/vim_escape/c12362u

    ReplyDelete
  2. This comment has been removed by the author.

    ReplyDelete
  3. I was always confused about escape myself until i saw this picture of the keyboard on an ADM-3A terminal that Joy used, it all made sense then.
    http://upload.wikimedia.org/wikipedia/commons/a/a0/KB_Terminal_ADM3A.svg

    ReplyDelete
  4. I first tried mapping my Caps Lock key to backspace as suggested by the Colemak keyboard layout that I use. That didn't stick. (But I still use the Colemak layout.) I now map Caps Lock to Esc. I just don't use Ctrl that much while I am in Vim.

    ReplyDelete
  5. I have my capslock key mapped to escape, instead of the control key. I try to use Vi key mappings in as many places as possible. I use Vimperator in Firefox, Vi input mode in Bash, Mutt for email, Vifm for file management, Evince to view PDF files (it uses j and k for scrolling). That covers most things I do on my computer.

    The capslock key is easy to reach from the home row, and since escape is used so often in Vim and other Vi-like interfaces, it makes more sense to map capslock to escape than to control. It becomes a single key press easily reached from the home row.

    :wq

    ReplyDelete
  6. How would I map to ?

    ReplyDelete
  7. How would I map "Esc" to "Right-Alt Space" ?

    ReplyDelete
  8. I've mapped 'jk' to Esc and that works best.

    ReplyDelete
  9. This comment has been removed by the author.

    ReplyDelete
  10. Control + [

    This is what I use now a days.

    ReplyDelete
  11. depending on what you want to do next you can use alt + move key to get leave insert mode.

    ReplyDelete
  12. If you switch between vim and emacs alot you could use ctrl-g maybe?

    ReplyDelete

Note: Only a member of this blog may post a comment.