Vim Case Insensitive Search



You can ignore case while searching in Vim using the ignorecase option, or you can intelligently decide when case matters with smartcase. To permanently configure these settings, you'll have to edit your .vimrc. If you'd like to have on-the-fly adjustments, you can choose to use prefixes and key mappings.

:set ignorecase or :set icEnables case insensitive search.
:set noignorecaseDisables case insensitive search, returning to case-sensitive mode.
:set smartcaseEnables smart case searching, where Vim respects case only if the search pattern contains uppercase letters.
/\c+searchtermPerforms a case insensitive search (overrides global settings).
/\C+searchtermPerforms a case-sensitive search (overrides global settings).
nnoremap <F2> :set ignorecase!<CR>Toggles the case sensitivity setting with the F2 key.
vim ~/.vimrcOpens the Vim configuration file to set default options.
:wqWrites changes and quits Vim, used after editing .vimrc.

What we'll learn

In Vim, you can start a search by pressing / for forward search or ? for backward search and then typing the pattern. Once you enter a pattern, press n to go to the next one and N to go to the previous one.

By default, searches using / are case sensitive. For example, searching for /example will yield only instances of the word in all lowercase, while "Example" or "EXAMPLE" would be ignored.

However, Vim does have the capability to perform case insensitive searches. There are several ways to execute case insensitive searches in Vim, here's what we'll learn:

  • The ignorecase option to have all searches be case insensitive
  • The smartcase option to intelligently switch between case sensitive and case insensitive searches
  • How to permanently set these options in .vimrc
  • Tips on quickly switching between case sensitive and case insensitive searches

Method 1: ignorecase

To turn on ignorecase, follow these steps:

  1. Enter the command mode by pressing Esc
  2. Type :set ignorecase or its shorthand form :set ic and press Enter

With the ignorecase option enabled, any subsequent searches will be case insensitive. Once this option is on, if you search for /Example, Vim will find "example," "Example," "EXAmple," and other case variations.

It's important to note that :set ignorecase only remains active for your current Vim session. If you close and reopen Vim, you will need to set ignorecase again. You won't need to set it if you configure it in your .vimrc file, which handles permanent settings.

To revert back to case sensitive search, you can disable the ignorecase option by typing :set noignorecase in command mode.

ignorecase is good for when you don't care about case sensitivity while searching, making it more efficient. But with ignorecase enabled, what about situations where you need to do uppercase searches?

Well, Vim offers an efficient way to switch between uppercase and lowercase searches, with smartcase.

Method 2: smartcase

The smartcase option offers a more intelligent approach to case sensitivity during searches. When smartcase is active, Vim pays special attention to uppercase letters.

For instance, searching for /vim with both ignorecase and smartcase enabled will match "vim", "Vim", or "VIM". Case is ignored by default. But, if the search is "Vim", it assumes the capital "V" is deliberate. So now the search respects the case, only matching "Vim" and excluding "vim" or "VIM".

This feature is useful when searching for acronyms or proper nouns, where case might be essential to the search pattern.

To enable smartcase, first ensure ignorecase is set:

  1. Enter the command mode by pressing the Esc key.
  2. Type :set ignorecase and press Enter.

Then turn on smartcase:

  1. Enter the command mode by pressing the Esc key.
  2. Type :set smartcase and press Enter.

smartcase only applies when the search pattern has at least one uppercase letter. If the pattern is entirely lowercase, Vim will perform a case insensitive search, due to the ignorecase option.

Remember that :set ignorecase and :set smartcase both only remain active for your current Vim session.

To make your preferred search option load every time you open Vim, you'll need to modify the Vim configuration file, .vimrc.

Permanently setting search options in .vimrc

The .vimrc file is typically located in your home directory, and it's where you can set default options to customize Vim's behavior.

If you prefer case insensitive searches, you can enable the ignorecase option permanently by adding this line to your .vimrc file:

set ignorecase

To have Vim to be intelligent about case sensitivity, you can add both ignorecase and smartcase to your .vimrc. This will make Vim ignore case when the search term is all lowercase, and respect case if there are any uppercase letters:

set ignorecase
set smartcase

To include these settings in your .vimrc file, open it with Vim:

vim ~/.vimrc

Then insert the desired set command lines as shown above. Save and close the file by typing :wq. Now your search preferences will be used every time you start Vim and you won't need to manually toggle settings for each session.

Advanced tips for toggling search settings in Vim

More advanced Vim users might want to quickly toggle between different search modes instead of permanently configuring Vim's search behavior in .vimrc. Here are some ways to adjust search modes on the spot:

  1. Temporary Overrides with Search Prefixes:

Vim allows you to override your current search setting. Do this by adding \c for case insensitive or \C for case-sensitive to the start of your search pattern.

  • Type /\csearchterm to perform a case insensitive search.
  • Type /\Csearchterm to perform a case-sensitive search.

These prefixes force the search to ignore or respect case regardless of your global settings.

  1. Key Mappings for Efficiency:

If you find yourself frequently switching settings, consider mapping a keyboard shortcut to toggle between ignorecase and noignorecase.

For example, you could add a mapping to your .vimrc such as:

nnoremap <F2> :set ignorecase!<CR>

Pressing F2 would then toggle the case sensitivity setting.