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

How do I customize what Vim considers a 'word' for motions and text objects?

Answer

:setlocal iskeyword+=-

Explanation

The iskeyword option defines which characters are part of a "word" for motions like w, b, e, *, and text objects like iw. By adding or removing characters, you can make word-based navigation and selection match your language's conventions.

How it works

  • iskeyword contains a list of character ranges that constitute "word" characters
  • Default: @,48-57,_,192-255 (letters, digits, underscore, extended ASCII)
  • iskeyword+=- — adds hyphen as a word character
  • iskeyword-=_ — removes underscore as a word character

Example

" For CSS/HTML: treat hyphenated words as one word
autocmd FileType css,html setlocal iskeyword+=-
Text: background-color: red;

Default iskeyword:  w moves: background|-|color|:| |red|;
With iskeyword+=-:  w moves: background-color|:| |red|;

ciw on 'background' now changes 'background-color'

Tips

  • For Lisp/Scheme: iskeyword+=!,?,+,-,*,/,=,<,>,& for function names
  • Check current value: :set iskeyword?
  • Affects * word search, w/b motions, iw/aw text objects, and more
  • Use setlocal instead of set to only affect the current buffer

Next

How do I return to normal mode from absolutely any mode in Vim?