I currently have this in my vimrc

set iskeyword+=-

But when I press w or b my cursor skips over hyphens. Am I misunderstanding iskeyword?

How do I get word and back commands to treat hyphens (and underscores) like spaces stopping at the word after the hyphen or underscore?

Also, set iskeyword? returns iskeyword=@,48-57,_,192-255,-,#

  • Also, ideally it's not the hyphen it would stop at but the character immediately after. The behavior I'm looking for is getting word and back to treat hyphens and underscores like spaces. – Costa Dec 7 '18 at 19:25

It looks like your understanding is inverted. By adding - to iskeyword, you're telling Vim that it's just a regular word character that should be skipped over by things like w or b.

Instead, you want to remove it1:

set iskeyword-=-

Based on your posted iskeyword, that should do the trick, but it's possible that others (or future you) might have a range that includes -. You can explicitly exclude a character (or range) by prepending it with ^ when adding it (and making sure it's to the right of the range that would otherwise include it):

set iskeyword+=^-

1) The fact that the hyphen was not the last thing in your set iskeyword? might be a hint that it was already in there, meaning your set iskeyword+=- was a noop.

  • I tried set iskeyword-=- in my vimrc but that doesn't work either. More perplexing, I tried set iskeyword=@,48-57,192-255 so removing the hyphen and underscore, and now word and back stop at underscores but not at hyphens. – Costa Dec 7 '18 at 19:11
  • @Costa Strange. Does adding the explicit exclusion work? – 8bittree Dec 7 '18 at 19:16
  • And running verbose, it seems the last place to set it is ftplugin. Strange. – Costa Dec 7 '18 at 19:16
  • But the line in that file is only setlocal isk+=# So that shouldn't impede right? – Costa Dec 7 '18 at 19:24
  • 1
    @Costa Adding # to iskeyword should not affect - behavior. – 8bittree Dec 7 '18 at 19:25

Your Answer

