63

vi treats dash - and space   as word separators for commands such as dw and cw.

Is there a way to add underscore _ as well?

I quite often want to change part of a variable name containing underscores, such as changing src_branch to dest_branch. I end up counting characters and using s (like 3sdest), but it would be much easier to use cw (like cwdest).

1
  • 1
    FWIW you can avoid the counting with ct_dest
    – Cuadue
    Commented Mar 6, 2015 at 23:33

6 Answers 6

46

Is there a way to add underscore _ as well?

:set iskeyword-=_ 

What is, and is not a member character to keywords depends on the language. For help on iskeyword use :help iskeyword.

9
  • 2
    I was using vi, not vim, so not an option Commented Oct 31, 2012 at 7:49
  • 2
    Any reason for using vi rather than vim?
    – user67416
    Commented Nov 1, 2012 at 16:15
  • 5
    I guess you've never worked in a locked down, highly changed-controlled environment run by paranoid PHBs! :) i.e. getting vim installed would have meant a world of pain... Commented Nov 29, 2012 at 15:57
  • 19
    I quit that job, and you couldn't pay me enough to go back.
    – user67416
    Commented Nov 29, 2012 at 19:19
  • 10
    @JoeWatkins I posted that info about 'iskeyword' not for you, but rather for all the Vim users who are led here by Google. Commented Aug 29, 2013 at 15:19
27

In case you're using vim, you can change that by setting the iskeyword option (:he iskeyword). If that is not an option, you can always use ct_ instead of counting.

1
  • Just vi, not vim. Wasn't aware of ct - that will certainly be a help, thanks Commented Oct 22, 2008 at 12:09
6

One other good option in such cases is to use camelcasemotion plugin.

It adds new motions ,b, ,e, and ,w, which work analogously with b, e, and w, except that they recognize CamelCase and snake_case words. With it you can use

c,edest

and this will replace "src_branch" with "dest_branch" if your cursor was on first character of "src_branch".

1

You could type cf_dest_ and save the counting part.

Edit: or as suggested: ct_ changes text until right before the underline character. (I'm using the f motion more, so it came more naturally to me)

Or you could redefine 'iskeyword' (:help iskeyword for details).

0

I was just looking at this myself and added this to my .vimrc:

set iskeyword=!-~,^*,^45,^124,^34,192-255,^_

My .vimrc had issues with ^| and ^", which was part of the default iskeyword for my setup, so I changed to their ascii values and it works fine. My main modification was to add "^_" to the end of the default setting to keep vim from seeing underscore as being part of a word.

2
  • 3
    FYI, removing the underscore from iskeyword can mess up syntax highlighting. If anyone knows how to get vim to see the underscore as a non-word character for navigation purposes but not for syntax highlighting, let me know.
    – kbosak
    Commented Apr 9, 2009 at 12:43
  • You can use a plugin to get this effect, for example, camelcasemotion.
    – Edan Maor
    Commented May 9, 2012 at 8:35
-1

To delete to the next underscore enter "df_" To change to the next underscore enter "cf_" NOTE: don't include the double quotes.

Your Answer

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

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