乐闻世界logo
搜索文章和话题

How to do case insensitive search in Vim

1个答案

1

Performing case-insensitive searches in Vim is a very practical feature, especially when working with text files that mix uppercase and lowercase letters. There are several ways to achieve this:

If you only occasionally need case-insensitive searches, you can use \c during the search to indicate case insensitivity. For example, to search for the word 'example' regardless of its case (uppercase, lowercase, or mixed), you can do the following:

vim
:/\cexample

Here, \c tells Vim that this search is case-insensitive.

2. Set Vim to Default Case-Insensitive

If you frequently need case-insensitive searches, configure Vim to default to case-insensitive searches by setting the ignorecase option:

vim
:set ignorecase

After this setting, all searches will default to case-insensitive.

3. Smartcase Option

Vim provides a useful smartcase option that works alongside ignorecase. When ignorecase is enabled and the search term contains uppercase letters, Vim automatically performs a case-sensitive search. This enables more intelligent handling of search requirements. To set it up:

vim
:set ignorecase :set smartcase

This combination means that if you enter a search term in all lowercase, the search is case-insensitive. However, if the search term contains any uppercase letters, the search automatically switches to case-sensitive.

Example

Suppose your text file contains the following content:

shell
Example example EXAMple eXample
  • After using :set ignorecase, searching :search example will find all four variants.
  • If you use :set ignorecase and :set smartcase, then searching :search Example will only match the first line 'Example' because it contains uppercase letters, triggering case-sensitive search.

By using these methods, you can flexibly perform case-sensitive or case-insensitive searches in Vim as needed, significantly improving editing efficiency.

2024年7月20日 15:19 回复

你的答案