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
iskeywordcontains 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 characteriskeyword-=_— 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/bmotions,iw/awtext objects, and more - Use
setlocalinstead ofsetto only affect the current buffer