Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Here is the situation, for example, I have several lines like this:

foo test foo

I searched "foo" with "/" and all the two "foo"s above are highlighted. And now I want to replace "foo" with something else like "lee";

Now since the system's clipboard is already occupied by "lee" so I don't want to copy "foo" with ctrl+C into command line to complete the copy and replace process.

Now my question is, how to copy the highlighted text "foo" after the search directly into command line with some shortcut(if any)?

share|improve this question
    
Do you want to replace all instances of "foo" with "lee" ? –  Lawrence Nov 21 '13 at 3:43
    
@Lawrence I know the replace command and the flags such as /g, my question is actually how to copy the highlighted text in normal mode after I type ":s%/{to-be-copied-by-shortcut}/{Ctrl+V(lee)}/g". Hope I have made it clearer. –  Felix Nov 21 '13 at 3:48
    
Is this in *Nix ? –  Lawrence Nov 21 '13 at 3:54
    
I'm using gVim 7.3(windows version of Vim) to be specific. –  Felix Nov 21 '13 at 3:56
    
I would just use the up arrow when entering the command to go up to where you searched for Foo then. If it was Linux then you could use the other paste. –  Lawrence Nov 21 '13 at 3:58

3 Answers 3

up vote 3 down vote accepted

You can replace what you searched for last with this command

:%s//lee/g

So assuming you searched for foo with /foo then you run :%s//lee/g to replace foo with lee.

Found it here - http://vim.wikia.com/wiki/Search_and_replace

share|improve this answer
    
So I have searched wrong keyword. There's no shortcut for this since there's "nothing" to substitute at all. Many Thanks. –  Felix Nov 21 '13 at 4:13
    
No problems ! :) –  Lawrence Nov 21 '13 at 4:19

Lawrence nailed it but you can use the gn command to yank the current match:

vgny

Or, if you want to insert the current word:

<C-r><C-w>
share|improve this answer
    
The second one serves the purpose to some extend as well but limited to a single word. I guess it won't work if my search pattern is phrase or sentence. Anyway thanks for this nice tip though I need to figure out under what situation I should use the first "vgny" command. –  Felix Nov 21 '13 at 9:17

Remember that every time you made a search, it is copied in a register : "/ (easy to remember)

To insert your search pattern :

in normal mode, press "/p

in insert mode, press <C-r>/

on command line, press <C-r>/

share|improve this answer
    
This tip is very useful and now I know some really convenient ways of paste. –  Felix Nov 22 '13 at 1:47

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.