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

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
  • \c in a pattern — forces case-insensitive for that specific pattern
  • \C in a pattern — forces case-sensitive for that specific pattern, regardless of ignorecase or smartcase

\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 to ignorecase)
  • /apple\C — matches only apple (case-sensitive forced by \C)
  • /Apple\C — matches only Apple

Tips

  • Useful with smartcase: smartcase only enables case-sensitivity when your pattern contains uppercase, but \C lets you force sensitivity even for all-lowercase patterns like /id\C to find id but not Id or ID
  • Works in substitute patterns: :%s/foo\C/bar/g replaces only lowercase foo
  • 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

Next

How do I jump to a tag in Vim and automatically choose if there is only one match?