Deleting / changing searched text in Vim

you are looking for "confirm"

:%s/hello .//gc

: to go into ex mode, % to apply to the whole buffer, s to substitute, /hello ./ to search for that regex, // to replace it with nothing, g to look for all matches on a line, and c to confirm whether or not to delete before each match.

That is assuming you are looking for confirm. If you want to just do it all at once, it has been mentioned already a bunch of times

:%s/hello .//g

: to go into ex mode, % to apply to the whole buffer, s to substitute, /hello ./ to search for that regex, // to replace it with nothing, and g to look for all matches on a line


I would do what one previous poster suggested: Construct a single delete command with d target and hit . after using n to find each occurrence. In this case 7x would delete anything matching hello . That's a shortcut form of d7l or d7[space] since d takes any movement command.

However, if you want a truly general solution to searching and deleting a regexp, you can execute a single substitution command (even if it fails) and then execute your search and use & to re-apply the last substitution to the current line:

:s/hello .//
/hello .
&
n
&

The first line (the substitution) can fail. You can still apply it again with & later. I'm kind of surprised that the first substitution command doesn't set the default pattern, but maybe it does in vi other than vim.


I think you want gn: (Go to the next match and) select it in visual mode. You can prefix this with a command, e.g. cgn changes the match, dgn deletes etc. I found that if already on a match, this match is used, so that this should do it.


To select the entire match of the current match (if you are at the start of the match) you can type

v//e

To delete to the end of the match

d//e

To change the match

c//e

Only negative is that you cant use n (as that will redo //e ) and instead have to use //

So typical workflow would look like this

/hello ./    : Jump to the start of the next "hello ."
d//e         : Delete the item
//           : repeat the search to get to the begining of the next "hello ."
c//e         : change it
//           : next one.
etc.