开发者

Copying from vim search term

I'm trying to copy text from my vim search term. I spend a fair bit of time building regular expressions for sed search&replace. Since my regex is often quite complicated, I like to build it up in a search before executing :%s/regex/new text/g

In开发者_开发百科 a terminal vim, I can copy my new regex from the search line using the mouse. I'd like to use gvim as much as possible, but it doesn't let right mouse clicks through for me to copy.

Any ideas how to get the search term into a buffer?

Thanks,

Andrew


In command mode (where you are when you hit : in normal mode), you can do ctrl-R /, which will expand to your last search term (other ctrl-R favorites are " for your yank buffer, or % for the full path of the current window)

You actually don't need to do that though. If you leave out the search term for :s, it will assume you want to use the last thing you searched for. so you can /searchregex, and then right after do :%s//replaceregex/ and it will use search regex to do the replace.


Use q: to open an editable window containing your commandline history. From there you can use all your usual Vim toolset to copy/paste/etc.

For the equivalent feature regarding search history, type q/.


q/ shows the search history. For the reverse action of copying a string from your normal buffer into command buffer or search buffer, which doesn't allow the normal use of p, use Ctrl-R 1 to paste.


You can copy text between registers using :let, e.g. copy last search term into register b:

:let @b=@/

Then use ctrl-R b to insert it as in Matt Briggs' answer. Of course that isn't necessary when you can insert it directly using ctrl-R / (in insert or ex mode) or "/p (in normal mode), but with this mapping:

nnoremap <silent> y/ :let @"=@/<CR>

you can type y/ to copy the last search term to the unnamed register for easy pasting. You could use the * register instead to copy to the system clipboard and have the text available to other apps:

nnoremap <silent> y/ :let @*=@/<CR>

See

:help 'clipboard'
:help registers

for more info about Vim registers and using the system clipboard.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜