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

How do I change which characters Vim treats as part of a word for motions and text objects?

Answer

:set iskeyword+={char}

Explanation

iskeyword defines which characters are considered word characters in Vim. This affects word motions (w, b, e), word text objects (iw, aw), whole-word searches (\<, \>, *), and completion. Adding or removing characters lets you tailor word navigation to the language or format you are working in.

How it works

  • :set iskeyword? — display the current value
  • :set iskeyword+={char} — add a character to the word definition
  • :set iskeyword-={char} — remove a character from the word definition
  • The default includes letters, digits, and _; many filetypes add - or .
  • Use :setlocal iskeyword+=... to apply the change only to the current buffer

Common values:

  • @ — all letters (A-Z, a-z, and locale letters)
  • 0-9 — digits
  • _ — underscore
  • - — hyphen (useful for CSS, kebab-case HTML, Lisp)
  • . — period (useful for dotted identifiers in Lua, Erlang)

Example

In a CSS file, background-color is a single identifier. Without - in iskeyword, w steps through background, then -, then color separately. Add it for the session:

:setlocal iskeyword+=-

Now yiw on background-color yanks the whole token.

Tips

  • Put filetype-specific settings in ~/.vim/ftplugin/{filetype}.vim instead of vimrc so they only apply when needed
  • After changing iskeyword, * (star search) will respect the new definition — very useful for searching hyphenated CSS class names
  • :help iskeyword and :help 'iskeyword' explain the full syntax for specifying character ranges like 48-57 (digits 0–9 by ASCII code)

Next

How do I run a search and replace only within a visually selected region?