Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Replace backward slash '\' with forward slash '/' in vim

Tags:

linux

vim

I have some scripts(linux) which are generating the o/p with respect to Windows. I expect it to be /home/manty/... it generates \home\manty\... It is very annoying when I try to change one by one all the time. So just for this purpose, I am leaving vim and going to gedit to perform "find and replace". How can I do it in vim?

I tried with following :

:%s/\\/\/

It changed only in the two places. I tried several other combinations as well nothing is working out.

like image 511
Sandeep Avatar asked Oct 25 '13 08:10

Sandeep


People also ask

How do I remove a slash in Vim?

:%s/\\n//g will simply remove every instance of \n .

How do I insert a backslash in Vim?

How do I insert a space + backslash at the end of the all lines in my Vim editor? Go to the end of the line, add space + backslash, go to the next line, add space + backslash, ... repeat.


1 Answers

Ah. I missed the g thing.

:%s/\\/\//g

This command does the trick. g stands for global and will make the change globally (complete file).

Also, you can add an extra option of c if you want to monitor each change. If you want to use c option the command would be :%s/\\/\//gc and then select y or n for each change.

I will wait for better answers from vim geeks. I am sure there must be a better way.

like image 156
Sandeep Avatar answered Sep 28 '22 19:09

Sandeep