I'm trying to use :grep to search for one of several words. Since grep accepts regular expressions, I figured I could just use the pipe character | to build a single expression for both words:

:grep -E 'foo|baz' *.c

Vim interprets the | as a separator between two commands. :help :bar suggests that I could escape it as \|, but that doesn't work, either. It seems that the :grep command does not handle backspaces and quotes.

:help map-bar also offers the suggestion to use ^V|, which actually escapes the pipe character, but now the ^V is also in my pattern.

Right now, the only workaround I've found is using environment variables:

:let $BAR = '|' | grep -E 'foo'$BAR'baz' *.c

But this is quite crazy in my opinion. Is there really no better way?

  • Just to be sure: have you read :h magic or learnvimscriptthehardway.stevelosh.com/chapters/31.html ?
    – lsrdg
    May 19, 2017 at 10:48
  • @lsrdg I think that option does not matter because the regexp is parsed by an external command.
    – wrtlprnft
    May 19, 2017 at 10:58
  • sorry, my bad. It just worked for me with 'foo\\|baz'. Have you tried that?
    – lsrdg
    May 19, 2017 at 11:13
  • I'm confused. This really did not work when I tried it an hour ago. Now it does...
    – wrtlprnft
    May 19, 2017 at 11:38
  • With grep, right? I haven't tried with make. Anyway, sounds like good news. And sorry for my first useless comment. (:
    – lsrdg
    May 19, 2017 at 12:01

1 Answer 1


As stated on the question, :h map-bar suggests escaping the pipe with with \|. However, grep apparently understands that as a literal bar, thus the backslash needs to be escaped as well. This should work for grep:

:grep -E 'foo\\|baz' *.c

NOTE: there's no need to escape the backslash with vimgrep (:h :vimgrep). The following seems to give the same results here:

:vimgrep 'foo\|baz' *.c

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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