Vim Case Insensitive Search
2/26/2024
TLDR
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.
Command | Description |
---|---|
:set ignorecase or :set ic | Enables case insensitive search. |
:set noignorecase | Disables case insensitive search, returning to case-sensitive mode. |
:set smartcase | Enables smart case searching, where Vim respects case only if the search pattern contains uppercase letters. |
/\c +searchterm | Performs a case insensitive search (overrides global settings). |
/\C +searchterm | Performs a case-sensitive search (overrides global settings). |
nnoremap <F2> :set ignorecase!<CR> | Toggles the case sensitivity setting with the F2 key. |
vim ~/.vimrc | Opens the Vim configuration file to set default options. |
:wq | Writes 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:
- Enter the command mode by pressing
Esc
- Type
:set ignorecase
or its shorthand form:set ic
and pressEnter
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:
- Enter the command mode by pressing the
Esc
key. - Type
:set ignorecase
and pressEnter
.
Then turn on smartcase
:
- Enter the command mode by pressing the
Esc
key. - Type
:set smartcase
and pressEnter
.
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:
- 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.
- 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.