I have this key map in my Vim configuration:

nmap <leader>rn :%s/\<<C-r><C-w>\>/

But for this code from php I have a problem.


Because, if cursor is on start of the word ** text **, then this map changes $text, not text.

I think to do in this way, from start of the text word, press ve, visual to the end of word, and then press map to replace.

Yes, and the second question, how to replace just on this line?

  • Do you still have something open in your question? How can we help you further? Otherwise maybe could you accept one of the answers using the v button next to the arrow voting buttons. It allow the question to rest :-) Commented Apr 3 at 5:57

2 Answers 2


I would introduce a v mapping:

vnoremap <leader>rn "zy:%s/<C-r>z/
  • "zy copy the selection in the z register.
  • <C-r>z paste the z register.

If you want to get rid of the all file replacement drop the %.

vnoremap <leader>rn "zy:s/<C-r>z/
  • Thank you, i will remember)) Cool!!!
    – serii
    Commented Mar 29 at 7:33
  • Thanks for the feedback :-) If it solves your problem consider accept it using the v button next to the arrow voting buttons. It allow the question to rest :-) Commented Mar 29 at 7:39

If you've already visually selected the word, pressing s or c will let you start changing it. The change is dot-repeatable.

If you wanted to be able to repeat the change (like & with :substitute), you have a few options:

  • search the word and cgn. Then . repeats on the next occurrence, or n to navigate so you can skip occurrences.
  • do a global substitute with confirmation (:%substitute/pat/repl/gc) so you can avoid the $test, for example.
  • load occurrences into a quickfix (or location list) with :[l]vimgrep /word/ %; you can filter the list with :Cfilter from the builtin cfilter plugin (:help :Cfilter) before running :cdo <command to make change>. It is harder to deal with duplicates on one line this way, though, and :vimgrep operates on files (not buffers).
  • Thanks, good job.
    – serii
    Commented Apr 3 at 7:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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