word vs. WORD in Vim
— vim — 2 min read
Vim has many motion commands like w
, e
, b
and their uppercase counterparts that allow users to navigate text more quickly. When using these commands, it's important to understand the difference between "word" and "WORD".
:help word
A word consists of a sequence of letters, digits and underscores, or a sequence of other non-blank characters, separated with white space (spaces, tabs, end-of-line). This can be changed with the 'iskeyword' option. An empty line is also considered to be a word.
In other words, a "word" is a sequence of characters that are separated by whitespace, punctuation, or the beginning/end of a line.
:help WORD
A WORD consists of a sequence of non-blank characters, separated with white space. An empty line is also considered to be a WORD.
In other words, a "WORD" is a sequence of characters that are separated by whitespace or the beginning/end of a line. Punctuation is not considered a separator for a WORD.
Let's take a look at an example to illustrate the difference. Consider the following sentence:
The storm raged on all night: lightning flashed, thunder boomed, and the wind howled through the trees.
If we use the "w" command to navigate through the sentence, it would take us to each individual word:
The storm raged on all night: lightning flashed, thunder boomed, and the wind howled through the trees.
However, if we use the "W" command instead, it would take us to each individual WORD:
The storm raged on all night: lightning flashed, thunder boomed, and the wind howled through the trees.
Here's another comparison counting how many words / WORDs different text has:
Text | words | WORDs |
---|---|---|
This is a test | 4 | 4 |
list.size | 3 | 1 |
if (list.size > 3) { | 9 | 5 |
Additionally, it's important to use "word" and "WORD" appropriately in combination with other Vim commands. For example, if you want to delete a word or WORD, you can use the "dw" and "dW" commands, respectively.