views:

78

answers:

6

When I do an interactive search for some pattern, each time I hit n, I get the next result. How do I delete / change each result that I come to?

Ideally, what I'm looking for would work like this: I hit n to get the search result, and then magic command to highlight that result in visual mode, and then I can do d or c to delete or change the highlighted text.

Example:

I enter the command

/hello .

and it matches hello, a space, and any character after it.

So say the first match it reaches is "hello w".

Now, I want to delete all of hello w, search for the next match (say it is hello a), change the next match to hello there, and keep doing different things to each match.

I'm not looking for just search-and-replace, because I want to be able to perform any action on each result interactively, such as delete the first result, replace the second result with bye, and replace the third result with later.

+1  A: 

Change the first one, using cwfooesc or whatever, then use the . command to repeat that edit for each further occurrence that you want to change.

Greg Hewgill
@Greg: That changes the *word*, OP wants to delete/replace *text*.
codaddict
Okay, so use whatever *text* editing command you need to change what you need to change. It's impossible to recommend something specific without a specific example.
Greg Hewgill
@Greg: I've added an example.
Chetan
A: 

Press the x key.

or user search and replace: [esc] :%s/[search-regex]/[replace-regex]/g

If replace-regex is blank you'll delete whatever you just searched.

shoebox639
And if you want to replace text, just do [r] and type, then [esc][n][r] then type some more. But a search and replace is probably best for you.
shoebox639
Doesn't `x` delete just one character? And `r` replace just one character? I'm looking to delete or replace the entire matched text.
Chetan
Crap ur right. Well have you tried the search and replace?
shoebox639
+2  A: 

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.

Ben Jackson
Thanks for your answer! Is there some way I can have Vim highlight each match when I come to it in visual mode, so that I can operate on the visually selected text? That would be the optimal solution.
Chetan
You can hilite the current search pattern any time by turning on hlsearch (`:set hlsearch`)
Ben Jackson
@Ben: I meant highlight it in visual mode, so I can perform an action on it.
Chetan
A: 

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

Matt Briggs
I know about confirm, but I was looking for something a little different. Please check out the new bolded text in the question for clarification.
Chetan
what you are describing is confirm. You type in the top command, it brings you to the first result and asks if you want to replace. you say "y" or "n", which either does it or doesn't, and moves to the next instance, asking the same question
Matt Briggs
+2  A: 

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.
Michael Anderson
Ah, this is exactly what I was looking for! Awesome! Just to clarify, what exactly does `//e` search for? The end of the match? How does it work?
Chetan
A more general question, actually, is, what does `//` search for?
Chetan
Actually, I've asked the question separately, please answer it here: http://stackoverflow.com/questions/3984622/how-does-the-command-work-in-vim
Chetan
A: 

How about doing this with recordings.

qa/textsearch HIT ENTER v9lvq

9 is the (length of textsearch - 1) i.e. 10 - 1 = 9

Now once you have prepared the recording in register 'a', do @agv ( @a: executes recording named 'a' and gv selects your search text.)

Now you can do whatever with your selected text, once you are done you can go to the next one with @@

hope that helps..

Aman Jain