vimtricks.wiki Concise Vim tricks, one at a time.

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 (overrides ignorecase)
  • \c — forces case-insensitive matching for this pattern (overrides noignorecase)

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 :substitute too: :%s/\CMyFunc/NewFunc/g renames only the exact casing
  • Works in :vimgrep: :vimgrep /\Cerror/j **/*.go
  • The position in the pattern doesn't matter — \C at the end works just as well as at the start
  • Combine with \v (very magic): /\v\Cpattern for both very magic mode and forced case sensitivity

Next

How do I match a pattern only when it is preceded or followed by another pattern, without including that context in the match?