How do I force a case-sensitive search in Vim even when ignorecase is enabled?
Answer
/pattern\C
Explanation
Appending \C anywhere in a search pattern forces the entire search to be case-sensitive, regardless of whether ignorecase or smartcase is set. This is useful when you have case-insensitive searching enabled globally but need an exact, case-sensitive match for a specific search without temporarily changing your settings.
How it works
/pattern\C— the\Catom overridesignorecaseand forces case-sensitive matching for the whole pattern- The
\Ccan appear anywhere in the pattern (beginning, middle, or end) - The complementary atom
\cforces case-insensitive matching, even ifnoignorecaseis set - Both atoms affect the entire pattern, not just the characters after them
Example
Suppose you have :set ignorecase active. Searching for /Error would match error, ERROR, and Error. To match only the exact capitalization:
/Error\C
This finds only Error, skipping error and ERROR.
Tips
- Use
\cin the pattern to force case-insensitive search even whennoignorecaseis set — useful for one-off case-insensitive matches in a case-sensitive setup - These atoms work in
:substitutepatterns too::%s/Error\C/Warning/greplaces only exactly-casedError - Combine with
\Cfor precise replacements when your vimrc hasignorecaseandsmartcaseset