How can you do a case insensitive search with line contexts displayed in vim while using the :g ex command?

I tried the following but it doesn't seem to work...

g/pattern/z#.1i|echo "================================"

I also tried this:

g/pattern/iz#.1|echo "================================"

...but it results in an error.

2 Answers 2


There are two ways to achieve this:

One is to set ignorecase, then the pattern regex will ignore the case. Yet, this solution is poor if you are writing a script that may need to be reused by someone.

A better solution is to use the \c (ignore case) \C (do not ignore case) modifiers in the regex. This command:

g/\cpattern/z#.1|echo "================================"

Will always ignore the case of pattern no matter what you have set ignorecase to.

And this command:

g/\Cpattern/z#.1|echo "================================"

Will always perform a case sensitive search for pattern, even if you have ignorecase set to true.

Reference (the forward slash is important):

  • :help /ignorecase

You can do a set ic before the search command.

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.