Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Copying from vim search term

Tags:

vim

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

like image 873
ajwood Avatar asked Nov 19 '10 14:11

ajwood


People also ask

How do I copy text from Vim?

In vim command mode press v , this will switch you to VISUAL mode. Move the cursor around to select the text or lines you need to copy. Press y , this will copy the selected text to clipboard.

How do I copy and paste outside of Vim?

To copy text from outside applications into Vim editor, first copy the text using the usual Ctrl-C command then go to Vim editor and type "+p in Normal Mode. I find the above commands very tedious to type every time I copy-paste from outside Vim, so I mapped the Ctrl-y to copy and the Ctrl-p to paste in Vim.


2 Answers

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.

like image 160
Matt Briggs Avatar answered Sep 21 '22 21:09

Matt Briggs


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/.

like image 33
UncleZeiv Avatar answered Sep 20 '22 21:09

UncleZeiv