How do I force a search to be case-sensitive even when ignorecase is enabled?
Answer
\C in search pattern
Explanation
Adding \C anywhere in a search pattern forces case-sensitive matching for that search, overriding the global ignorecase setting. It is the uppercase counterpart to \c (which forces case-insensitive matching) and gives you per-search control over case sensitivity.
How it works
:set ignorecase(or:set ic) — makes all searches case-insensitive by default\cin a pattern — forces case-insensitive for that specific pattern\Cin a pattern — forces case-sensitive for that specific pattern, regardless ofignorecaseorsmartcase
\C can appear anywhere in the pattern — at the start, middle, or end. /Foo\C, /F\Coo, and /\CFoo are all equivalent.
Example
With :set ignorecase active and this buffer:
apple Apple APPLE
/apple— matches all three (case-insensitive due toignorecase)/apple\C— matches onlyapple(case-sensitive forced by\C)/Apple\C— matches onlyApple
Tips
- Useful with
smartcase:smartcaseonly enables case-sensitivity when your pattern contains uppercase, but\Clets you force sensitivity even for all-lowercase patterns like/id\Cto findidbut notIdorID - Works in substitute patterns:
:%s/foo\C/bar/greplaces only lowercasefoo - Works in
:vimgrep,:global, and any other command that takes a pattern - To force case-insensitive on a per-search basis (the reverse), use
\c