I get more confused after reading @John O'M 's answer.
But after some search, I get his point.
Here is my notes:
Character classes:
\i identifier character (see 'isident' option) /\i
\I like "\i", but excluding digits /\I
\k keyword character (see 'iskeyword' option) /\k
\K like "\k", but excluding digits /\K
\w word character: [0-9A-Za-z_] /\w
\W non-word character: [^0-9A-Za-z_] /\W (ps: not limited to ASCII, can match Chinese charaters)
Modified from nvim's help
word/WORD
I. consist of a sequence of
group 1. letters, digits and underscores, 【the definition of `word` can be changed with the 'iskeyword' option.
After `set iskeyword+=-`, `-` becomes a member of group 1
group 2. other non-blank characters, (including non-word characters, e.g. Chinese charaters)
(when characters in group 1 are seperated by characters in group 2,
such as "I爱you", character "爱" makes the `WORD` become three `word`s.
Similarly, the `WORD` "我n你" contains 3 `word`s
II. separated with:
white space (spaces, tabs, <EOL>).
I like the use of "XOR" in @Nicholas Cousar's comment:
(modified by me)
A word consists of a sequence of
group 1 (letters OR digits OR underscores i.e. keywords),
XOR
group 2 (a sequence of other non-blank characters i.e. non-keywords),
separated with white space (spaces, tabs, )
Real Example
https://github.com/dominikduda/vim_current_word
pain point:
it solves all the problems other similar plugins have with knowing what exactly is a word and when cursor is on the word.
Plugin determines what is a word based on what is a keyword in current file type.
For example foo#bar
in .vim
file will be a single word (because vimscript allows to put #
as part of variable name), but in .rb
file foo#bar
will be considered as two words (because #
starts comment).
Helps predict how vim word movements (w
, e
, b
) will behave
Plays well with * and #
However, this plugin does not work so well as described !!
This plugin will highlight I爱U
as a word, I-U
will not be highlighted as a word ,because -
is not in my iskeyword
(iskeyword=@,48-57,_) . But nvim' s word motions, e.g., w
, *
, and #
regard I爱U
as 3 words.