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

How do I select a word under the cursor in visual mode?

Answer

viw

Explanation

How it works

The command viw selects the word under the cursor in visual mode. It combines three concepts:

  • v enters characterwise visual mode
  • i specifies "inner" (excluding surrounding whitespace)
  • w targets a word text object

This selects the entire word regardless of where your cursor is positioned within the word. The "inner" variant (iw) selects just the word characters, while aw ("a word") also includes the trailing whitespace.

Common variations:

  • viw - select the inner word (just the word itself)
  • vaw - select a word including trailing whitespace
  • viW - select an inner WORD (includes punctuation, stops at whitespace)
  • vaW - select a WORD plus trailing whitespace

Once selected, you can operate on the word with any visual mode command: d to delete, y to yank, c to change, U to uppercase, u to lowercase, and more.

Example

Given this line with the cursor on the letter o in world:

hello world foo
  1. Press viw to select world
  2. The entire word world is highlighted
  3. Press c to change it, type vim, press Esc

Result:

hello vim foo

This is much faster than manually positioning to the start of the word and selecting character by character. It works from any position within the word.

Next

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