How do I make word motions like w, b, and e treat hyphens or other characters as part of a word?
Answer
:set iskeyword+=-
Explanation
By default, Vim treats hyphens, dots, and many punctuation characters as word boundaries. This means w, b, e, ciw, diw, and other word-based motions and text objects split on these characters. By modifying iskeyword, you can make Vim treat additional characters as part of a word, which is essential when working with CSS properties, Lisp identifiers, shell variables, or kebab-case naming conventions.
How it works
:set iskeyword+=-— adds the hyphen to the set of keyword charactersciwonbackground-colornow changes the entire hyphenated identifier instead of justbackgroundwjumps overmy-variable-nameas a single word instead of stopping at each hyphen- Use
:set iskeyword?to inspect the current value
Example
With default iskeyword, pressing ciw on the b in:
background-color: red;
Selects only background. After :set iskeyword+=-, ciw selects background-color.
Tips
- Add to a filetype-specific config:
autocmd FileType css setlocal iskeyword+=- - Add dots for Clojure/Lua namespaces:
:set iskeyword+=. - Use
:set iskeyword&to reset to the default value W,B,E(uppercase) always use whitespace-only boundaries regardless ofiskeyword