How do I change until the next underscore in VIm?

65

19

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

Posted 2011-02-10T14:26:11.670

Reputation: 20 371

What's wrong with :nohl? – Ignacio Vazquez-Abrams – 2011-02-10T14:27:26.277

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 – 2011-02-10T14:36:14.413

Answers

86

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

Posted 2011-02-10T14:26:11.670

Reputation: 976

1This 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 – 2017-11-09T13:16:03.887

2Vim never fails, only one's understanding of what it is capable of! – Benjamin R – 2017-12-12T04:57:43.320

2Mnemonic: change 'til.

i.e. ct_ can be remembered as "change 'til underscore". – François Leblanc – 2018-04-04T20:30:02.997

@FrançoisLeblanc That is a great mnemonic, I find those always help me remember vim keys better. :) – Kredns – 2019-06-19T12:44:35.257

15You're confusing f, t, F and T. f moves forward to the character; F moves backward to the character; t moves forward to just before the character; and T moves backward to the character to the right of the target character. So your second example should have been ct_. – garyjohn – 2011-02-10T16:55:35.060

66

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

Posted 2011-02-10T14:26:11.670

Reputation: 9 044

This is my personal favorite – Jay – 2016-02-26T19:17:43.700

2I like this one, but it breaks my syntax color coding in php. – Aaron Surrain – 2017-03-16T18:05:31.247

How can I unset or toggle this? – Behe – 2019-09-05T09:05:02.337

1@Behe you can run :set iskeyword-=_. It will work only in your current vim – Mat M – 2019-11-14T14:23:17.847

7It'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 – 2011-03-02T12:01:14.420

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.

Jdoherty

Posted 2011-02-10T14:26:11.670

Reputation: 11

-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

Posted 2011-02-10T14:26:11.670

Reputation: 119

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 – 2019-11-14T10:03:56.140

@dirdi I agree. I tried to edit the first answer, but it was rejected and suggested to post on my own. – Mat M – 2019-11-14T14:19:55.523