Vi and Vim Stack Exchange is a question and answer site for people using the vi and Vim families of text editors. Join them; it only takes a minute:

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

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.

share|improve this question
up vote 3 down vote accepted

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
share|improve this answer

You can do a set ic before the search command.

share|improve this answer

Your Answer

 
discard

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.