Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

VIM: Change current highlighted search term matched with /search

Tags:

vim

Is there any command equivalent to "delete until the end of the current highlighted search match and enter insert mode"?

For example, I search for a term with:

/Element

It finds the string ExampleElementExample, places the cursor on the E in Element, and highlights Element.

I would like a generic command that applies to all searches that is equivalent to c7l or ctE in this particular case. However, I also want to be able to easily repeat this command to the next match by pressing n, ..

c//e basically does what I want, but falls short because it replaces the current search buffer, so n no longer takes me to the next match. Right now I'm using ctE or visual mode, but I feel like there must be a better option.

What is the fastest and most efficient way to execute this command?

like image 785
James Duffy Avatar asked Jan 29 '26 11:01

James Duffy


1 Answers

If your Vim is recent enough (7.3 with a patch-level above 6xx), you can use gn:

barbazfoobazbar

/foo<CR>

barbaz[foo]bazbar

cgnvim<CR>

barbazvimbazbar

You can hit . to jump to the next foo and change it to vim in one go.

See :help gn.

like image 61
romainl Avatar answered Feb 01 '26 02:02

romainl