71

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?

2
95

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

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

2
  • 2
    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. – Dom Nov 9 '17 at 13:16
  • 3
    Mnemonic: change 'til. i.e. ct_ can be remembered as "change 'til underscore". – François Leblanc Apr 4 '18 at 20:30
72

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.

3
  • 8
    It's great to know that I can define my own word boundaries, but after some thought, I think the best solution is ct_ as doubleface says below, since it's concise and is default vim behavior. – Nathan Long Mar 2 '11 at 12:01
  • 2
    I like this one, but it breaks my syntax color coding in php. – Aaron Surrain Mar 16 '17 at 18:05
  • How can I unset or toggle this? – Behe Sep 5 '19 at 9:05
1

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.

0

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
2
  • 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

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.