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:
venters characterwise visual modeispecifies "inner" (excluding surrounding whitespace)wtargets 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 whitespaceviW- 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
- Press
viwto selectworld - The entire word
worldis highlighted - Press
cto change it, typevim, pressEsc
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.