3

I frequently work with code style which asks that many non-class symbols use snake_case -- Python, Ruby, and C especially. If I have a symbol like longfirstpart_longsecondpart, I'd like to be able to jump to the underscore in the middle à la w skipping to the next word.

Is there a built-in shortcut or init.vim variable for this kind of behaviour?

4

This can be done very easily with the iskeyword option. From :h iskeyword

                        *'iskeyword'* *'isk'*
'iskeyword' 'isk'   string (Vim default for MS-DOS and Win32:
                        "@,48-57,_,128-167,224-235"
                   otherwise:  "@,48-57,_,192-255"
                Vi default: "@,48-57,_")
            local to buffer
            {not in Vi}
    Keywords are used in searching and recognizing with many commands:
    "w", "*", "[i", etc.  It is also used for "\k" in a |pattern|.  See
    'isfname' for a description of the format of this option.  For C
    programs you could use "a-z,A-Z,48-57,_,.,-,>".
    For a help file it is set to all non-blank printable characters except
    '*', '"' and '|' (so that CTRL-] on a command finds the help for that
    command).

So add this to your .vimrc:

set iskeyword-=_

and that should work for you.

2
  • While this does work in terms of just skipping forward, it completely breaks my colorscheme if I add an underscore or a slash as a keyword. Any way around that?
    – Jules
    Oct 5 '17 at 19:23
  • what colorscheme is this? Oct 7 '17 at 7:15
4

I would simply use f_, but that means I know I want to move to the underscore instead of abstractly to the next word.

You can repeat the last f with ;, and both f and ; accept a count.

1
  • 1
    Not only does ; go forward again but , goes backward. Sorta like hitting N instead of n after a search. Oct 6 '17 at 21:25
0

There are exist two plugins for the improving experience to move through camelCase and snake_case.

  1. The first one is he camelcasemotion plugin, defining the additional commands, normally prepended with a comma. But this one is old and unmaintained for many years.

  2. The second one is actively developed, and uses completely different approach - vim-wordmotion. It redefines the word boundaries, so all default commands will work as is, just within new rules:

CamelCaseACRONYMWords_underscore1234
w--->w-->w----->w---->w-------->w->w
e-->e-->e----->e--->e--------->e-->e
b<---b<--b<-----b<----b<--------b<-b

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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