Patricia Sauer - Authentic Programming

Moving the cursor word by word in vi

several distributed word cards

In the last post we learned how to navigate by single characters. Using this technique, it might still take aaaaaaaages, to move to the bottom of a file 😱 We can be faster by learning how to move by whole words. So let's have a look at how to navigate word by word in vi.

For all the following commands, we will consider "Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua." our example text to see where we would navigate to.

Moving forwards

Let's have a look at the different commands for moving forwards.

Moving to the beginning of the next word using 'w'

This command moves the cursor to the beginning of the next word. A word is defined as a sequence of characters separated by white space or punctuation.

Considering, we are at the "L" of our example sentence, we would then be at "i"(psum), "d"(olor), "s"(it), "a"(met), ",", "c"(onsetetur), and so on. The comma will be considered being a word, therefore, the cursor will stop there, too.

Navigation in vi using w

Moving to the beginning of the next word using 'Shift + w'

This command moves the cursor forward one word, but it considers punctuation as part of the word. This will lead to the same results as using 'w' but the cursor won't stop at the comma.

Considering, we are at the "L" of our example sentence, we would then be at "i"(psum), "d"(olor), "s"(it), "a"(met), "c"(onsetetur), and so on.

Navigation in vi using shift + w

Moving to the end of the current word using 'e'

This command moves the cursor to the end of the current word. A word is defined as a sequence of characters separated by white space or punctuation.

Considering, we are at the "L" of our example sentence, we would then be at "m" (of 'Lorem'), "m" (of 'ipsum'), "r" (of 'dolor'), "t" (of 'sit'), "t" (of 'amet'), ",", "r" (of 'consetetur'), and so on. The comma will be considered being a word, therefore, the cursor will stop there, too.

Navigation in vi using e

Moving to the end of the current word using 'Shift + e'

This command moves the cursor to the end of the current word, including punctuation. This will lead to the same results as using 'e' but the cursor won't stop at the "t" of "amet" but will consider the comma as the end of the word "amet" instead.

Considering, we are at the "L" of our example sentence, we would then be at "m" (of 'Lorem'), "m" (of 'ipsum'), "r" (of 'dolor'), "t" (of 'sit'), ",", "r" (of 'consetetur'), and so on.

Navigation in vi using shift + e

Moving backwards

Moving to the beginning of the current word using 'b'

This command moves the cursor to the beginning of the current word, including punctuation.

Considering we are at the "r" of "consetetur" of our example sentence, we would then be at "c" (of 'consetetur'), ",", "a" (of 'amet'), "s" (of 'sit'), "d" of ('dolor'), "i" (of 'ipsum)', "L" (of 'Lorem'). The comma will be considered being a word, therefore, the cursor will stop there, too.

Navigation in vi using b

Moving to the beginning of the current word using 'Shift + b'

This command moves the cursor to the beginning of the current word, but it considers punctuation as part of the word. This will lead to the same results as using 'b' but the cursor won't stop at the comma.

Considering we are at the "r" of "consetetur" of our example sentence, we would then be at "c" (of 'consetetur'), "a" (of 'amet'), "s" (of 'sit'), "d" of ('dolor'), "i" (of 'ipsum)', "L" (of 'Lorem').

Navigation in vi using shift + b

Moving by more than just one word

Like the single-character cursor movements, the commands for moving word by word, can be preceeded by a number to indicate the number of words to move forward or backward. For example, 3w will move the cursor forward three words, and 2b will move the cursor backward two words.

Recap

In this article we learned that

  • we can navigate word by word forward and backward
  • w/W can be used to move the cursor to the beginning of the next word
  • e/E can be used to move the cursor to the end of the current or next word
  • b/B can be used to move the cursor to the beginning of the current or previous word
  • word movements can be preceeded by numbers to multiply the movements like "3w" for moving the cursor forward three words

You can check the following table to get another overview about the commands and their effects.

CommandResult
wMove the cursor to the beginning of the next word; count symbols and punctuation as a word
WMove the cursor to the beginning of the next word; consider puntuation as part of the word
eMove the cursor to the end of the current or next word; count punctuation as a word
EMove the cursor to the end of the current or next word; consider puntuation as part of the word
bMove the cursor to the beginning of the current or previous word; count punctuation as a word
BMove the cursor to the beginning of the current or previous word; consider puntuation as part of the word

Watch it on YouTube

You can watch me explaining how to navigate word by word in vi and vim in this video:

Liked this article?

Buy Me A Coffee

© 2018 - 2024 Patricia Sauer