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

How do I override Vim's case sensitivity for a single search without changing the global ignorecase setting?

Answer

/\c

Explanation

Vim lets you override the ignorecase and smartcase settings on a per-search basis using the \c (case-insensitive) and \C (case-sensitive) atoms directly inside the pattern. Place \c or \C anywhere in your search pattern and it overrides the global setting for that search only.

How it works

  • \c — force case-insensitive matching for this pattern, regardless of ignorecase
  • \C — force case-sensitive matching for this pattern, regardless of ignorecase

The atom can appear at any position in the pattern — beginning, middle, or end. Vim applies it to the entire pattern.

Example

:set noignorecase    " default: case-sensitive

/Hello           " matches: Hello  (case-sensitive, will NOT match hello)
/\cHello         " matches: Hello, hello, HELLO, hElLo (case forced off)
/Hello\C         " matches: Hello ONLY (force case-sensitive, even if ignorecase is set)

Tips

  • \c is especially useful when smartcase is enabled: type \c to force insensitivity even when your pattern has uppercase letters
  • Use \C when you need an exact-case match in a config where ignorecase is always on
  • These atoms also work in substitution patterns: :%s/\cfoo/bar/g replaces foo, Foo, FOO, etc.
  • They work in :global, :vimgrep, and any other pattern context

Next

How do I swap two words in Vim using visual paste without any plugins?