vim Editing - how to edit the highlighted segment of a search or convert to visual block
In vim, if I have the following text:
hello there
Say I search for hell in the text above.
/hell
and press n to move to the next instance.
开发者_JS百科
hell
o
hell is now highlighted in vim and my cursor is on the 'h'.
What is the most efficient way to now yank/delete that highlighted text.
Is there a way to 'yank to the end of the highlighted text'? Or 'create visual block from highlighted text'?
I know I can use %s/hell/whatever/gc to step through as an alternative.
TIA Tom
y//e
, or d//e
should do the trick.
:let @" = @/
as well, even if you have moved the cursor.
I don't know of a built in mapping (Edit: but see Luc Hermitte's answer below as that's a much better solution than my bodges End Edit), but you could do the yank or select with a couple of mappings:
nmap ,y y/<C-R>/\zs<CR>
nmap ,v v/<C-R>/<BS>\zs<CR>
The ,y
mapping uses the '/' register to pull in the last search term search, adds \zs
to make the search point be the end and the yank proceeds up to that point. The ,v
mapping does a visual selection, but has to delete the last character of the search (with <BS>
) to make it end at the right place.
For what it's worth, you can simplify the %s/hell/whatever/gc
that you suggested by refining your search with /
and then using a shortened form:
/hell
:%s//whatever/gc
This is because :s
uses the last search term by default.
精彩评论