How do I force a case-sensitive or case-insensitive search for just one pattern without changing my settings?
Answer
\C
Explanation
Vim's \C and \c atoms let you override ignorecase and smartcase on a per-pattern basis. Place \C anywhere in the pattern to force case-sensitive matching for that search only, or \c to force case-insensitive — regardless of your global settings.
How it works
\C— forces case-sensitive matching for this pattern (overridesignorecase)\c— forces case-insensitive matching for this pattern (overridesnoignorecase)
The atom can appear anywhere in the pattern; it applies to the entire search.
Example
With ignorecase set globally:
/\Cfunction " matches only 'function', never 'Function' or 'FUNCTION'
/\cFUNCTION " matches 'function', 'Function', and 'FUNCTION'
This is especially useful with smartcase. For instance, smartcase makes a search case-insensitive when all letters are lowercase. But when you need to search for a known-lowercase identifier exactly, prefixing with \C ensures Vim won't broaden the match.
Tips
- Works in
:substitutetoo::%s/\CMyFunc/NewFunc/grenames only the exact casing - Works in
:vimgrep::vimgrep /\Cerror/j **/*.go - The position in the pattern doesn't matter —
\Cat the end works just as well as at the start - Combine with
\v(very magic):/\v\Cpatternfor both very magic mode and forced case sensitivity