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

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

  • \c anywhere in the pattern makes the entire search case-insensitive: /\cfoo matches foo, Foo, FOO
  • \C anywhere in the pattern makes the entire search case-sensitive: /\CFoo matches only Foo
  • 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 :substitute too: :%s/\cfoo/bar/g replaces all case variants of foo
  • They also work in :global: :g/\cTODO/ collects all TODO/todo/Todo lines
  • \c overrides \C if both appear — place only one
  • This is more precise than temporarily :set ignorecase because it scopes the override to a single pattern

Next

How do I match a pattern only when it is preceded or followed by another pattern, without including that context in the match?