11

expand(<cword>) gives me the word under the cursor, but it excludes symbols such as @ or .. For example, if I call it at the end of @word, it will get me word and exclude the @.

Is there a way that I could include symbols as part of the current word?

2 Answers 2

14

The characters that are included in a Vim word are set by the 'iskeyword' option. See

:help <word>
:help word
:help 'iskeyword'

To add another character to this set, such as ., execute the following:

:set iskeyword+=.

See

:help :set+=

Note that the character @ is special, so it has be added like this:

:set iskeyword+=@-@

See

:help 'isfname'
2
  • 1
    @-@? Is that Bram's way of showing he's mad at use for making @ part of words?
    – muru
    Jul 22, 2015 at 5:59
  • 1
    Note that there are side effects to changing iskeyword. For example, if you add @ to iskeyword, the standard word motion command (w) will also move across e.g. @word @otherword. Jul 22, 2015 at 14:13
9

Use expand(<cWORD>).

From :help :<cWORD>:

<cWORD> is replaced with the WORD under the cursor (see WORD)

and from :help WORD:

A WORD consists of a sequence of non-blank characters, separated with white space. An empty line is also considered to be a WORD.

A sequence of folded lines is counted for one word of a single character. "w" and "W", "e" and "E" move to the start/end of the first word or WORD after a range of folded lines. "b" and "B" move to the start of the first word or WORD before the fold.

2
  • Thanks, @Carpetsmoker! I didn't have the time to expand my answer until now. Jul 22, 2015 at 14:04
  • viW, ciW, diW, yiW also will works like <cWORD> against <cword>
    – Mamrezo
    Oct 12, 2020 at 1:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.