简体   繁体   中英

Vim escape backslash paste

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!

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

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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM