3 Ways of Making Case Insensitive Search in Vim/Vi

By default, Vim/Vi’s search is case sensitive. However, if I want to search case insensitively, is there any method? For example, I search for dog, instead of matching dog only case sensitively, I also want to find out phases containing Dog, DOg, etc.

There are several methods:

1. Setting the search to be case insensitive in Vim

Set vim to ignorecase:

:set ignorecase

dog will match Dog, dog, or DOg.

To unset it:

:set noignorecase

You can also put it to your .vimrc if you want the behavior to be the default one.

2. Using c or C in search pattern

You can also force a pattern to be case insensitive or sensitive by adding \c or \C regardless the setting of the settings of ignorecase.

For example, the search with /dog\c is always case insensitive. And /dog\C is always case sensitive.

3. Use smartcase

The smartcase can only be used when the search pattern is typed and ignorecase option is on.

To enable it:

:set ignorecase
:set smartcase

If your search pattern is purely lowercase, it will search case insensitively.

If your search pattern contains uppercase characters, it will search case sensitively.

6 comments:

  1. The second method above is incorrectly stated and should read (see vim ‘:help /\c’):
    2. You can also force a patten to be case insensitive or sensitive by \c or \C regardless the setting of ignorecase.

    For example, the search /dog\c is always case insensitive. And /dog\C is always case sensitive.

      1. Method 3 (smartcase). My experience is that if the search is just small letters, it only finds small letters. It is NOT case insensitive as you said. For example, /the will find “the” but not “The”.
        If it were case insensitive /the would find both “the” and “The”

        1. We added some screenshots in the post to illustrate the methods. From our tests, it works as expected after the smartcase is set. If it still does not work to you, you may check the vim version you are using and whether there are some special configurations.

        2. UPDATE
          I checked my _vimrc file and did not include ‘set ignorecase’. With ‘set ignorecase’ added, then ‘set smartcase’ works as described in the article (/The only finds The, but /the finds the and The).

          Help (options.txt) states ” Only used when the search pattern is typed and ‘ignorecase’ option is on.” = Has no effect unless ignorecase is on.

Leave a Reply

Your email address will not be published. Required fields are marked *