1

I am new to VI. How do you replace two forward slashes with one globally?

I did:

:%s/\//

and

:%/\//\/g

Those don't work as desired.

Example or Goal

//This is good//because it//works;

//on every//Line it//is used one;

Desired output

/This is good/because it/works;

/on every/Line it/is used one;
3
  • You're missing a final / on your second command, but it's probably easier to use a different delimiter: s|//|/|g – Heptite Jul 21 at 21:52
  • While I'm always happy to mark questions as duplicate I don't think this one is a duplicate of the one linked by @Quasímodo . Sure the usage is the same but if OP doesn't know that :s can take different separators they would never find the other question. Plus this question focuses on substituting /s while my question focuses on the general usage of :s with !... So I wouldn't close this one as duplicate. – statox Jul 22 at 8:07
  • @Quasímodo yes that's much closer – statox Jul 22 at 13:51
5

The command you are trying to use is the following:

%s/\/\//\//g
%             The range: All lines in buffer
 s            the substitution command
  /    /  /   the separators of the substitution command
   \/\/       in the matching pattern you need to escape both slashes
        \/    you also need to escape the slash in the replacement string
           g  and finally the flag to match all occurrences in the line

However as the comments said in this case you should use a different separator for the substitution command this way you don't need to escape the /. For example this would do the job:

%s;//;/;g
1
  • Thank you everyone for the great answers. A different separator is easier. This one with the awesome breakdown of parts is exactly what I was looking for. I wanted to know how to accomplish this using the escape option. – Kenny Jul 22 at 13:50

Not the answer you're looking for? Browse other questions tagged or ask your own question.