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

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

  • \C is a case flag that can appear anywhere in the pattern
  • It forces the entire pattern to match case-sensitively, overriding ignorecase
  • Its counterpart \c forces case-insensitivity (useful when noignorecase is 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

  • \c and \C can be placed anywhere in the pattern: /foo\Cbar or /\Cfoo both work
  • When smartcase is on, a pattern with any uppercase letter already forces case sensitivity — \C is most useful when the pattern is entirely lowercase but must still match exactly
  • Combine with \v (very magic) at the start: /\v\Cpattern for readable case-sensitive regex

Next

How do I run a search and replace only within a visually selected region?