Searching, or pattern matching, is a foundational Vim skill that everyone must master. A simple tap of /
followed by a string to match or regular expression is all it takes. By default, these patterns are case sensitive. Searching for FooBar
will not match references to foobar
. There are two options to improve this behavior that I’d recommend to everyone:
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.
Here’s a gif where I demonstrate these two options in practice. I first enable them from Vim command mode. (Consider add those to your .vimrc
if you like them.) 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!
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.
Was this useful? Help us improve!
With your feedback, we can improve VimTricks. Click a link to vote:
You can also use `\C` in your search pattern to force case-sensitive matching. With `smartcase` set, using `\C` with an all-lowercase pattern will let you search for only the lowercase version.