Vi Tip of the Week: Moving a word at a time

This is part of a series of tips on using Vi.  To see the complete series, click here.

If you want to move around in a document quickly with vi, there are 3 useful commands (or 6, depending on how you look at it) for moving word-by-word.

Pressing the w key will move you forward word-by-word.  For example, if you’ve got the line:

This is a test of moving word-by-word.

and your cursor is positioned on any character in “This”, you can press w and the cursor will be moved to the start of the next word, “is”.  Pressing w again will move the cursor again, this time to “a”.

If you want to move backwards word-by-word, you can use the b key.  For example, if the cursor is positioned at the start of “moving”, pressing b will move the cursor to the start of “of”.  Pressing b again will move the cursor to “test”, and so on.

If you want to move forward to the end of a word, you can press the e key.  With the cursor placed on the start of “This”, pressing e will move the cursor to the “s” at the end of “This”. If the cursor is already positioned at the end of a word, pressing e will move you to the end of the next word.  For example, with the cursor positioned on the “s” in “This”, pressing e will move the cursor to the end of “is”.

But I mentioned that there are really 6 commands.  Try the following line of text:

Let’s eat, Grandma.

(A classic example of the importance of punctuation!)  If the cursor is positioned on the “L” in “Let’s”, pressing w will move the cursor to the apostrophe.  Pressing it again will move the cursor to the “s”, and pressing it three more times will move you to the “e” in “eat”, the comma, and the “G” in “Grandma”.  So, as you can see, the w command considers punctuation to be separate words.

If you press Shift-W instead, vi ignores punctuation.  With the cursor on the “L” of “Let’s”, pressing Shift-W moves the cursor to the “e” in “eat”, and pressing it again will move the cursor to the “G” in “Grandma”.

Similarly, Shift-B moves the cursor backwards a word, ignoring punctuation.  With the cursor on the “a” at the end of “Grandma”, pressing Shift-B once will move the cursor to the beginning of “Grandma”.  If you were to press b now, the cursor would be positioned on the comma, but if you press Shift-B, the cursor is positioned on the “e” at the start of “eat”.

Finally, Shift-E works similarly with punctuation.  With the cursor on the “L” in “Let’s”, pressing e moves the cursor to the “t” in “Let’s”, but pressing Shift-E moves the cursor to the “s”.

Summary:

  • w moves the cursor forward a word at a time, and stops at punctuation
  • Shift-W moves the cursor forward a word at a time, ignoring punctuation
  • b moves the cursor backward a word at a time, and stops at punctuation
  • Shift-B moves the cursor backward a word at a time, ignoring punctuation
  • e moves the cursor to the end of the word, or to the next word if it’s already at the end of the word, and stops at punctuation
  • Shift-E moves the cursor to the end of the word, or to the next word if it’s already at the end of the word, ignoring punctuation.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.