# How do I replace '\\' with '\'?

Tried

``````:s/\\\/\\/g
``````

but VIM will search for `\\/\` instead.

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

• :%s/\\\\/\\/g double them up, eh? May 10 '19 at 1:55
• 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

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
``````
• 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? May 10 '19 at 8:03
• 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
• @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,\$: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.

• `:%s/from/to/g` may be better. % means all lines in the file May 12 '19 at 2:48
• 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. May 13 '19 at 15:31