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

How do I enable smart case-sensitive searching in Vim?

Answer

:set ignorecase smartcase

Explanation

How it works

By setting both ignorecase and smartcase, Vim uses an intelligent case sensitivity rule for searches:

  • If your search pattern is all lowercase, the search is case-insensitive
  • If your search pattern contains any uppercase letter, the search is case-sensitive

This gives you the best of both worlds. You can quickly search without worrying about case, but when you specifically use a capital letter, Vim respects that.

  • :set ignorecase (or :set ic) - Makes all searches case-insensitive
  • :set smartcase (or :set scs) - Overrides ignorecase when the pattern has uppercase characters

Both options must be set together for smart case to work. If only ignorecase is set, all searches are case-insensitive regardless. If only smartcase is set without ignorecase, it has no effect.

You can override this per-search using:

  • \c anywhere in the pattern forces case-insensitive
  • \C anywhere in the pattern forces case-sensitive

Example

Add to your ~/.vimrc:

set ignorecase
set smartcase

Now in a file containing:

Hello world
hello World
HELLO WORLD
  • /hello matches all three lines (all lowercase = case-insensitive)
  • /Hello matches only Hello world (contains uppercase = case-sensitive)
  • /HELLO matches only HELLO WORLD (contains uppercase = case-sensitive)
  • /hello\C matches only hello World (forced case-sensitive with \C)

This is one of the most commonly recommended settings for a Vim configuration because it matches the behavior most users expect from search.

Next

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