How do I enable smart case-sensitive searching in Vim?
Answer
:set ignorecase smartcase
Explanation
How it works
By setting both ignorecase and smartcase, Vim uses an intelligent case sensitivity rule for searches:
- If your search pattern is all lowercase, the search is case-insensitive
- If your search pattern contains any uppercase letter, the search is case-sensitive
This gives you the best of both worlds. You can quickly search without worrying about case, but when you specifically use a capital letter, Vim respects that.
:set ignorecase(or:set ic) - Makes all searches case-insensitive:set smartcase(or:set scs) - Overridesignorecasewhen the pattern has uppercase characters
Both options must be set together for smart case to work. If only ignorecase is set, all searches are case-insensitive regardless. If only smartcase is set without ignorecase, it has no effect.
You can override this per-search using:
\canywhere in the pattern forces case-insensitive\Canywhere in the pattern forces case-sensitive
Example
Add to your ~/.vimrc:
set ignorecase
set smartcase
Now in a file containing:
Hello world
hello World
HELLO WORLD
/hellomatches all three lines (all lowercase = case-insensitive)/Hellomatches onlyHello world(contains uppercase = case-sensitive)/HELLOmatches onlyHELLO WORLD(contains uppercase = case-sensitive)/hello\Cmatches onlyhello World(forced case-sensitive with\C)
This is one of the most commonly recommended settings for a Vim configuration because it matches the behavior most users expect from search.