Last Updated: February 25, 2016
·
1.316K
· dmichaelavila

The Second Rule of vim

tl;dr:

  1. exit insert mode immediately after inserting text
  2. use ^[ instead of ESC to exit insert mode

There should be a second rule of vim, after Justin Kelly's First Rule. The first rule, loosely, is to begin using h,j,k,l. Although he suggests more specifically disabling your arrow keys so that you have to get used to h,j,k,l. I prefer the first rule in the looser form: using h,j,k,l exclusively for arrow-key-like-navigation.

My proposal for the second rule is to immediately exit insert mode once you've completed inserting text. An example will help clarify. Let's say you're in the middle of typing a sentence, e.g. "The foo is excellent|" (that is your cursor after the word excellent), and you decide that you want to change the word foo to bar. You should not hold down the arrow key until you get to foo and then erase it and type bar. At this point you're done inserting text and should have already exited insert mode. From normal mode the change might look like the following sequence of normal mode commands ?f enter cw bar ^[.

This might seem like a lot of keystrokes, 10 total, but it's only 1 more than the method of holding down the arrow key to erase and type bar. (hold left arrow + erase foo + type bar + ^[ = 9 keystrokes.) Although you can use ^w to reduce that (I realize after testing myself.) Either way, working in normal mode scales much better with more text than working within insert mode.

The power of vim really comes with mastery of normal mode. The more time you spend there, the better. Knowing a little more about exiting insert mode makes doing so easier and therefore more likely. So, to exit insert mode you would typically hit the ESC key. The infinitely better way to exit insert mode is to use the ASCII equivalent ^[. That is, hold down the CTRL key and press [. This keeps you in close proximity to home row.

Some people are going to note that you can use ^C as a general "stop whatever I'm doing" command. This is bad advice. ^C is more of a "kill" and is not graceful in various scenarios. You can find information about this on the internet.

5 Responses
Add your response

Oh, fun - I'll golf. 3bdwi then type "bar "; or 3bdEi then type "bar".

Edit - that's 9 either way

over 1 year ago ·

@rthbound ha! Nice.

over 1 year ago ·

i would like to type 3bce .
ha!

over 1 year ago ·

@scorpiuszjj you're right, the d could be changed to a c to avoid the i at the end, thus reducing the keystrokes further.

over 1 year ago ·

Instead of ?f enter cw bar ^[

You also could type:

Ff cw bar ^[

over 1 year ago ·