开发者

Deleting / changing searched text in Vim

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开发者_Python百科 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.


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.


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.


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.


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


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.


How about doing this with recordings.

qa/textsearch ENTERv9lvq

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 @@


Also for removing all lines containing a string, you can use:

:g@search_query@d

g command to globally search

@ Separator

d command to delete.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜