0

I am having the following text in vim:

Hello, \/Good\/Bye.

Now I want to change it to:

Hello, -Good-Bye.

So I use: Visual Select, Yank the Line. then

%s/<Ctrl+R>+"/-/gc

When I push CTRL+R+" The line clones into the search, but It \/ will not work. I need to change it to \\/ . Is there a quick function or keystroke to automatically escape the chars? Please let me know. Thank you!

romainl
  • 186,200
  • 21
  • 280
  • 313
  • [`\V`](https://stackoverflow.com/a/50629050/7976758) disables all magic. [`escape()`](https://stackoverflow.com/a/6871009/7976758) escapes metacharacters. – phd Jun 18 '18 at 19:39

2 Answers2

0

As phd wrote in comments, you can use \V and escape() to do it. For example, write the following function:

function! Regesc(text)
    return '\V' . escape(a:text, '/\')
endf

Then after yanking some text, use it like this:

:%s/Ctrl+R=Regesc(@@)CR/-/gcCR

This will display and run the following command, depending of what you yanked:

:%s/\VHello, \\\/Good\\\/Bye./-/gc

yolenoyer
  • 8,797
  • 2
  • 27
  • 61
0

You could use another delimiter after your visual selection

:'<,'>s,\%V\\/\%V,-,gc

\\ ......... scaped backslash
\%V ........ visual selection area

So you have to put your pattern \\/ inside a visual delimiter

SergioAraujo
  • 11,069
  • 3
  • 50
  • 40