Vim Search Ignore Case
Searching, or pattern matching, is a foundational Vim skill: Simply enter /
and a string to find the next match. You can navigate files quickly this way by hitting <enter>
after searching to jump to the first match. By default, these patterns are case sensitive. Searching for FooBar
will not match references to foobar
. But Vim search ignore case can be enabled using a combination two options I’d recommend:
set ignorecase
– Makes pattern matching case-insensitiveset smartcase
– Overridesignorecase
if your pattern contains mixed case
With this powerful combination of options, you get the best of both worlds: Using lowercase in your pattern will automatically match any case. But using one or more uppercase characters in your pattern will restrict matches to exact, case-sensitive matches. Set these options in your .vimrc
to ignore case in Vim search.
Bonus trick: If you include
\c
anywhere in your pattern, the entire pattern functions as ifignorecase
is on (the values you have set for these two options are ignored). If you use\C
somewhere in your pattern, then Vim will force matching case for the pattern.
Here’s a short video where I demonstrate these two options in practice. I first enable them from Vim command mode. Then I search for colin
in lower case. You can see it matches every reference, including the mixed-cased Colin
. That is thanks to ignorecase
. Next, I search for Colin
. Because I’ve enabled smartcase
, Vim knows I want a case-sensitive search and finds only the exact match. Try it out!
Demo of Ignoring Case in Vim Search
Also check out these two previous search-related posts:
- Stay in Search Mode makes modifying and improving your pattern easier.
- Search Project for Current Word is a fast way to do whole-project searches.
How do I ignore case in Vim search?
To ignore case in Vim search by default, set the ignorecase
option in your .vimrc
by adding the following line:
set ignorecase
Get an even better experience by enabling the smartcase
option in your .vimrc
with the following line:
set smartcase
This will match exact-case text when you use a mixed-case pattern.
Why would I want to ignore case?
Using the search function with /
is a fast way to navigate through documents in Vim. By ignoring Vim search case by default, you can match patterns faster by typing less. You can get the best of both worlds by enabling smartcase in Vim. There's a reason it's called smart case -- it enables moving very quickly through files but allow allows exact case match searching when needed.
How does the \c and \C overrides work?
Although it seems a but unintuitive, including either \c
or \C
inside your search pattern will override these options. Note that they are not flags for the end, but rather escaped characters you include inside your pattern. Check out these ignore case search override examples from the Vim docs:
\cfoo
will match foo
, Foo
, or FOO
no matter what your settings are. And foo\C
will match only foo
.
What are the Vim ignorecase docs?
If the ignorecase
option is on, the case of normal letters is ignored. smartcase
can be set to ignore case when the pattern contains lowercase letters only. When \c
appears anywhere in the pattern, the whole pattern is handled like ignorecase
is on. The actual value of ignorecase
and smartcase
is ignored. \C
does the opposite: Force matching case for the whole pattern. {only Vim supports \c
and \C
} Note that ignorecase
, \c
and \C
are not used for the character classes.
Examples: pattern 'ignorecase' 'smartcase' matches foo off - foo foo on - foo Foo FOO Foo on off foo Foo FOO Foo on on Foo \cfoo - - foo Foo FOO foo\C - - foo
What are the Vim smartcase docs?
smartcase
scs
boolean (default off) global
Override the ignorecase
option if the search pattern contains upper case characters. Only used when the search pattern is typed and ignorecase
option is on. Used for the commands /
, ?
, n
, N
, :g
and :s
.
Not used for *
, #
, gd
, tag search, etc. After *
and #
you can make smartcase
used by doing a /
command, recalling the search pattern from history and hitting .