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}.viminstead 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 iskeywordand:help 'iskeyword'explain the full syntax for specifying character ranges like48-57(digits 0–9 by ASCII code)