3

I want to search and replace the selection I made in Visual mode. For example I have the following text:

Lorem ipsum dolor sit amet. Consectetur adipiscing elit. Sed quis gravida lectus amet.

And my selection is amet. I want to have this as a result:

Lorem ipsum dolor sit foo Consectetur adipiscing elit. Sed quis gravida lectus foo

Is there a way to achieve this without having to manually type the amet\. part in :%s/amet\./foo/g?

  • What do you mean without typing %s/amet\./foo/g? Without typing it at all (not using a substitute command) or not typing it entirely (injecting the content of the selection in the command)? – statox Aug 25 '16 at 12:33
  • I am happy with not typing the content of the selection. So something which would automatically fill the :%s/.. part would be great. – tversteeg Aug 25 '16 at 12:35
  • hit * on amet. word, it selects all occurances of amet. then run this command: %s//foo; there is no need to type amet. again and even g flag. – dNitro Aug 25 '16 at 12:42
  • 1
    Related: on the command line, <c-r><c-a> will insert the WORD under the cursor. – Antony Aug 25 '16 at 13:05
  • 1
    @mMontu: Also adding . to isk and using * will select words contain .; set isk+=. – dNitro Aug 25 '16 at 13:08
6

You could try the yanking the visual selection before starting the ex command:

y:%s/<c-r>"/foo/g
  • y: yank the visual selection
  • :%s/: start the ex command
  • Ctrl+r ": insert the last yanked/deleted text
  • /foo/g: the remaining ex command

Note that it won't escape the dot -- you will probably need to write a custom function to achieve that (if you intend to follow this approach, you could use the "PatternsOnText" plugin as a starting point). One simple solution, using \V as suggested by Kent to avoid the need to escape special characters, is the following mapping:

vnoremap <F4> y:%s/\V<c-r>"//g<left><left>

If you are trying to reduce the number of key strokes you should consider the following (specially if you intend to change a small number of matches):

y/\V<c-r>"<cr>cgnfoo<esc>..
  • y: yank and exit visual selection
  • /\VCtrl+r"Enter: use the last yanked text to start a search
  • cgn: change the next match
  • fooEsc: insert foo and exit insert mode
  • .: repeat last change
  • 2
    perhaps a \V is not a bad idea. since OP explicitly escaped .(dot) – Kent Aug 25 '16 at 13:04
  • @Kent indeed, I updated the answer with a mapping using \V. – mMontu Aug 25 '16 at 13:11
  • The \V one does the trick for me, typing <esc>cgvfoo<esc> doesn't work though. – tversteeg Aug 25 '16 at 13:15
  • @tversteeg sorry, I actually meant gn instead of gv - I will update the answer. – mMontu Aug 25 '16 at 13:53
  • +1 for showing me \V, exactly what I needed. Why use <c-r>"Enter instead of <c-r>0? – durron597 Mar 1 '17 at 22:24

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.