84

If I have this text in vim, and my cursor is at the first character:

www.foo.com

I know that I can do:

  • cw to change up to the first period, because a word (lowercase w) ends at any punctuation OR white space
  • cW to change the whole address, because a Word (uppercase w) ends only at whitespace

Now, what if I have this:

stupid_method_name

and want to change it to this?

awesome_method_name

Both cw and cW change the whole thing, but I just want to change the fragment before the underscore.

My fallback technique is c/_, meaning 'change until you hit the next underscore in a search,' but for me, that also causes all underscores to be highlighted as search terms, which is slightly annoying.

Is there a specifier like w or W that doesn't include underscores?

Nathan Long
  • 26,565
  • What's wrong with :nohl? – Ignacio Vazquez-Abrams Feb 10 '11 at 14:27
  • 1
    I do want search terms to be highlighted most of the time; just not when I use search as a movement. (I also just asked this question: http://superuser.com/questions/244042/is-it-possible-to-not-trigger-my-normal-search-highlighting-when-using-search-as) – Nathan Long Feb 10 '11 at 14:36

4 Answers4

116

You can do cf_. f won't highlight the searched character.

You can also do ct_ if you don't want to include the _.

doubleface
  • 1,276
  • 1
  • 9
  • 4
  • 3
    This is also incredibly extendable and therefore powerful. I'm guessing you can put any character after the f or t and it will go to the next (or prev for caps) occurrence. Thanks. – Heather Nov 09 '17 at 13:16
  • 12
    Mnemonic: change 'til.

    i.e. ct_ can be remembered as "change 'til underscore".

    – François Leblanc Apr 04 '18 at 20:30
82

Put this in your .vimrc:

set iskeyword-=_

Then _ will be treated as a word boundary (though not a WORD boundary), and cw could be used to just change "awesome", and cW to change the whole thing.

See:

:help iskeyword

and

:help word

for more info.

frabjous
  • 11,007
2

camelcasemotion is a pretty handy vim plugin that allows you to move through words when using underscore or camelcase notation. Using this plugin you can place a comma in front of many of the traditional vim motion commands which will allow you to treat words in underscore or camelcase notation as full words.

1

As a summary of all previous answers:

There is no specifier to exclude _

You can do cf_. The searched character (_ here) will be included in the replaced string.
You can also do ct_ if you don't want to include the searched character (_ here).

  • f and t won't highlight the searched character in the file, unlike /.
  • f and t allows to search for one character
  • / will allow for a pattern or a longer string , and will not include it, like t
Mat M
  • 139
  • If you intend to add more information to an answer, just extend / modify it. Do not copy its content and post it as an own answer. This is plagiarism. – dirdi Nov 14 '19 at 10:03
  • @dirdi I agree. I tried to edit the first answer, but it was rejected and suggested to post on my own. – Mat M Nov 14 '19 at 14:19