vim: replace a string with the “/” character in it

Tag: vim Author: yiwen215 Date: 2014-04-08

I've been using the following to replace characters:

  1. To find each occurrence of eth0 in the current line only, and replace it with br0,enter (first press Esc key and type):

    :s/eth0/br0/g
    
  2. To find and replace all occurrences of eth1 with br1, enter:

    :%s/eth1/br1/g
    
  3. To find and replace all occurrences of eth1 with br1, but ask for confirmation first, enter:

    :%s/eth1/br1/gc
    
  4. To find and replace all occurrences of case insensitive eth1 with br1, enter:

    :%s/eth1/br1/gi
    

However, how would I replace a string with one that has a / in it. For example, I want to replace all my debug printf statements with //. When I type..

:%s/printf/ // /gc

it gives me a Trailing characters error. If someone could teach me how to do this i would be thankful.

Thank You!

Best Answer

You can use a different separator:

:s+sprintf+ // +g

Other Answer1

escape the / in :s/.../../ or better, use other separator:

e.g. s:@[email protected]@g