Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I know there's probably a way to do that using regex, but I can't quite seem to figure out regex in vim. I tried searching: /-\{2} but it catches triple hyphens, too.

I also can't just do a traditional find and replace, since I have triple hyphens in my file. Searching for -- returns triple hyphens in addition to double hyphens.

share|improve this question
Did you try :%s/--/---/g? It works for me. – Tomer Vromen Feb 18 '12 at 23:27
I should have said that my file also contains triple hyphens, so if I do that, I would get triple hyphens turning into quadruple hyphens. – Jon Feb 19 '12 at 18:54
up vote 8 down vote accepted

How about :%s,-\@<!---\@!,---,g? That way it won't change '---' to '----'.

share|improve this answer
That works, thanks! I don't understand how, but I'll go look up what commas and @ symbols mean in vim. – Jon Feb 19 '12 at 19:03
@Jon The comma is a delimiter—whatever character immediately follows it is used to separate the search expression, the replace expression, and any options (in this case g). (Usually / is used, but it's not so nice to look at when you have lots of backslashes.) The -\@<! prevents the pattern from matching immediately after a '-', and the `-\@!' keeps it from matching immediately before a '-'. – Dr Kitty Feb 20 '12 at 0:29

Your example works for me, so I suspect you have a different character in your file that only looks like a hyphen.

I would suggest placing your cursor on one in normal mode, typing yl to yank the character, then typing / followed by pressing control-r then " (double quote). If that works, you can use the same trick to create your substitute command.

share|improve this answer
I should have said above that my example, while it returns double hyphens, also returns triple hyphens, and so if I were to do a find/replace using that example, it would turn all my triple hyphens into quadruple hyphens, which is not what I want. But thanks very much for the yl, /, ctrl-r, " trick--I didn't know that one, and that's going to solve a lot of other problems for me. – Jon Feb 19 '12 at 19:01

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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