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 ofignorecase\C— force case-sensitive matching for this pattern, regardless ofignorecase
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
\cis especially useful whensmartcaseis enabled: type\cto force insensitivity even when your pattern has uppercase letters- Use
\Cwhen you need an exact-case match in a config whereignorecaseis always on - These atoms also work in substitution patterns:
:%s/\cfoo/bar/greplacesfoo,Foo,FOO, etc. - They work in
:global,:vimgrep, and any other pattern context