vimtricks.wiki Concise Vim tricks, one at a time.

How do I navigate by WORD boundaries that include punctuation in Vim?

Answer

W, B, and E

Explanation

How it works

Vim distinguishes between two types of word objects:

  • A word (lowercase w, b, e) is a sequence of letters, digits, and underscores, or a sequence of other non-blank characters. Punctuation like hyphens, dots, and colons are treated as separate words.
  • A WORD (uppercase W, B, E) is any sequence of non-blank characters separated by whitespace. Punctuation is included as part of the WORD.

The uppercase WORD motions are:

  • W moves forward to the beginning of the next WORD.
  • B moves backward to the beginning of the previous WORD.
  • E moves forward to the end of the current or next WORD.

Example

Consider this line:

foo-bar.baz   http://example.com   some_var

With the cursor on f:

  • Pressing w four times moves through: foo, -, bar, . (each punctuation character is a separate word).
  • Pressing W once jumps straight to h in http://example.com, skipping all the punctuation.

This makes W, B, and E ideal for navigating through URLs, file paths, namespaced identifiers, or any text where punctuation and letters should be treated as a single unit.

The WORD motions also work with operators: dW deletes from the cursor to the start of the next WORD, and cE changes from the cursor to the end of the current WORD. They are often faster than their lowercase counterparts when editing code that uses special characters like ->, ::, or //.

Next

How do you yank a single word into a named register?