Vim tip: Stop escaping slashes

Post by Nico Brailovsky @ 2015-05-07 | Permalink | 1 comments | Leave a comment

If you do a lot of search & replace in Vim, eventually you'll end up escaping a lot of slashes. Whenever you have to replace a path, for example. Isn't that annoying? After a couple of levels you end up with a horrible "\/path\/to\/foo\/bar" pattern. And if you miss an escape slash, good luck. It's better to scrap the whole thing and start over.

Luckily, when you are using the 's'earch command you can pick a different separator. Instead of typing "%s/\/foo\/bar\/baz\//foo\/bar\//g", you can simply type "%s#/foo/bar/baz/#foo/bar/#g". Vim will automagically detect you want to use '#' as a delimiter, and you'll end up with a much more readable pattern.

Extra tip: this also works in sed


In reply to this post, VimTip: Search and f(replace) | An infinite monkey - Nico Brailovsky's blog commented @ 2019-02-26T07:05:03.000+01:00:

[…] Pre-tip: When using search and replace in Vim, you don't need to use slashes […]

Original published here.