How do I override case sensitivity for a single search pattern without changing my settings?
Answer
\c
Explanation
Vim lets you embed \c or \C directly inside a search pattern to control case sensitivity for that search only, regardless of your 'ignorecase' and 'smartcase' settings.
How it works
\canywhere in the pattern makes the entire match case-insensitive\Canywhere in the pattern makes the entire match case-sensitive- The modifier can appear at any position in the pattern — beginning, middle, or end
- These override both
'ignorecase'and'smartcase'for the duration of that pattern
Example
With 'ignorecase' off, searching for:
/\cError
matches error, Error, ERROR, and any other capitalisation.
With 'ignorecase' on, searching for:
/\CERROR
matches only the exact string ERROR, ignoring your case setting.
The modifier also works in substitutions and :global:
:%s/\cfoo/bar/g
:g/\CTODO/d
Tips
\cis invaluable when'smartcase'is enabled and your pattern has no uppercase letters — you can force truly case-insensitive matching by adding\c- Combine with
\v(very magic) for powerful patterns:/\v\cfoo(bar|baz) - The position of
\c/\Cin the pattern does not matter; putting it first is a common convention for readability