How do I override ignorecase or smartcase for a single search without changing my settings?
Answer
/\cfoo
Explanation
Vim's \c and \C flags let you force a search to be case-insensitive or case-sensitive on a per-search basis, regardless of your ignorecase and smartcase settings. This is essential when you have smartcase set and need to occasionally toggle the behavior without touching your config.
How it works
\canywhere in the pattern makes the entire search case-insensitive:/\cfoomatchesfoo,Foo,FOO\Canywhere in the pattern makes the entire search case-sensitive:/\CFoomatches onlyFoo- The flag can appear anywhere in the pattern—at the start is conventional
Example
With set smartcase active:
/Foo " case-sensitive (uppercase triggers smartcase)
/foo " case-insensitive (all lowercase)
/\cfoo " forced case-insensitive — matches Foo, FOO, foo
/\CFoo " forced case-sensitive — matches only Foo
Useful when you want to find all variants of a symbol: /\cerrorhandler matches ErrorHandler, errorhandler, ERRORHANDLER.
Tips
- The flags work in
:substitutetoo::%s/\cfoo/bar/greplaces all case variants offoo - They also work in
:global::g/\cTODO/collects all TODO/todo/Todo lines \coverrides\Cif both appear — place only one- This is more precise than temporarily
:set ignorecasebecause it scopes the override to a single pattern