Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

In vim, I typed /foo Enter and found some matches.

Now I want to replace all instances of foo with bar (i.e. :%s/foo/bar).

Can this be done without retyping the foo? (e.g. into the command line following :)

share|improve this question
up vote 12 down vote accepted

Yes, you can do it with :%s//bar.

Explanation: in the substitute command when the search pattern is empty, Vim uses the last search pattern.

Alternatively, you can explicitly paste the last search pattern, which is stored in a special register. You can quickly insert its contents by typing Ctrl-R+/ in insert or command mode.

So enter command mode, type %s/, then Ctrl-R+/ to paste the last pattern, and then the remaining /bar.

share|improve this answer

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.