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

How do I search case-insensitively in Vim without changing global settings?

Answer

/\cpattern

Explanation

How it works

By default, Vim searches are case-sensitive: /Hello will not match hello or HELLO. You can make a single search case-insensitive by adding \c anywhere in the pattern.

The \c flag can go at the beginning, middle, or end of the pattern -- it does not matter where you place it:

  • /\chello matches hello, Hello, HELLO, hElLo
  • /hello\c does exactly the same thing
  • /\Chello forces case-sensitive search (the opposite)

This is a per-search override that does not change your global settings. It is ideal when you usually want case-sensitive search but occasionally need to ignore case.

For a global setting, you can use :set ignorecase. Even better, combine it with :set smartcase, which makes searches case-insensitive unless you include an uppercase letter in the pattern.

Example

Suppose you are searching for a variable that might be named Config, config, or CONFIG:

/\cconfig

This matches all variations:

let config = {}           <- matches
class Config:             <- matches
const CONFIG = "prod"     <- matches

Conversely, if you have ignorecase set globally but want one specific search to be case-sensitive, use \C:

/\CConfig

This only matches Config with that exact capitalization.

The \c and \C flags also work in substitute commands: :%s/\cpattern/replacement/g replaces all case variations of the pattern.

Next

How do you yank a single word into a named register?