3

Tried

:s/\\\/\\/g

but VIM will search for \\/\ instead.

So is there any way I can do this in Vim?

  • 5
    :%s/\\\\/\\/g double them up, eh? – wbogacz May 10 '19 at 1:55
  • 1
    When working with \ or /, I often use # as a delimiter for the substitute command because this is more easy to grasp visually (I feel), i.e. :s#\\\\#\\#g – Rolf May 29 '19 at 6:26
5

In s/\\\/\\/:

  1. the first \ escapes the second \;
  2. the second \ is escaped, so it's no longer special; and
  3. the third \ isn't escaped, so it escapes the / after it.

So you need to escape the third \ like the second was:

:s/\\\\/\\/g
| improve this answer | |
  • thanks!! It worked. but in '\\\\/', why the 4th '\' isn't escaped by the 1st '\' like the 2nd and the 3rd one? in vim, how many character will be escaped by '\' exactly? – Teddy C May 10 '19 at 8:03
  • 1
    Usually, \ escapes the next character, unless it is itself escaped by a \ . There are a lot of exceptions. See :h pattern-atoms for some of those – muru May 10 '19 at 9:04
  • 1
    @TeddyC The 3rd \ isn't escaped by the 1st. The 2nd is escaped by the 1st and the 4th is escaped by the 3rd. – Rich May 10 '19 at 14:55
1
:1,$:s/\\\\/\\/g

1 means the first line and $ means till the last one. each \\ means \, and \/ removes the / as divider, here you have your syntax error.

| improve this answer | |
  • 1
    :%s/from/to/g may be better. % means all lines in the file – Teddy C May 12 '19 at 2:48
  • 2
    What OP posted isn't a syntax error though. :s/\\\/\\/g uses \\\/\\ as the pattern and g as the replacement. (A trailing / is implied.) – muru May 13 '19 at 5:23
  • @muru I thought that the trailing / was mandatory. – Hola Soy Edu Feliz Navidad May 13 '19 at 15:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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