Vim that uses an exclamation mark in substitute

I recently found a replacement command for vim where the author replaced / with ! as follows:: :%s!foo!bar , and I do not understand the difference with the traditional :%s/foo/bar .

I was looking for some documentation on this syntax, but I did not find anything suitable, so I tried to experiment myself, and I could not clearly understand the difference between the two forms. Here is what I found:

  • You cannot mix / and ! in the same team. For example:: :%s/foo!bar does not work.
  • Sign ! may be useful with templates, including / . For example, if I want to replace </ with % in my file, I can do :%s!</!%!g instead of :%s/<\//%/g : I do not need to hide / in the first command, but I would be surprised if this is the only use ! .
  • Some regex seems to fail with / and works fine with ! , but since I am far from being a master of regular expressions, I am not sure about this question.

So my question is: what is the difference between / and ! in vim lookup and when should I use one instead of the other?

+7
vim regex substitution
source share
1 answer

Both can be used as long as they are consistent (do not mix). Commonly Used. But another char can be used if the line you want to replace contains several / . For example:

 :%s!/usr/local/bin!/usr/bin! :%s:/usr/local/bin:/usr/bin: :%s,/usr/local/bin,/usr/bin, 
+11
source share

All Articles