How do I force a search to be case-sensitive regardless of the ignorecase setting?
Answer
/\Cpattern
Explanation
Vim's ignorecase and smartcase settings change how all searches behave globally. When you need one specific search to be case-sensitive — without touching your settings — embed \C anywhere in the pattern to force case sensitivity for that search only.
How it works
\Cis a case flag that can appear anywhere in the pattern- It forces the entire pattern to match case-sensitively, overriding
ignorecase - Its counterpart
\cforces case-insensitivity (useful whennoignorecaseis set) - These flags affect only the pattern they appear in; your global settings are unchanged
- Works in
/,?,:s/.../.../,:g/..., and any other pattern context
Example
With :set ignorecase active:
/error " matches Error, ERROR, error
/\Cerror " matches only lowercase 'error'
/\CERROR " matches only uppercase 'ERROR'
Useful when searching a log file for ERROR (critical) but not error (info-level).
Tips
\cand\Ccan be placed anywhere in the pattern:/foo\Cbaror/\Cfooboth work- When
smartcaseis on, a pattern with any uppercase letter already forces case sensitivity —\Cis most useful when the pattern is entirely lowercase but must still match exactly - Combine with
\v(very magic) at the start:/\v\Cpatternfor readable case-sensitive regex