1

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)?

  • 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
4

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

  • 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
0

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>
  • 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
0

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

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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