vim

Vim Case Insensitive Search

You may have used many searches while programming. There are many situations when you have to search the exact match of a string within some text file or code, including its case sensitivity. In such cases, you may find it difficult to do so. Vim has provided us with a way to search the match without getting troubled with its case, i.e., upper or lower. Thus, this article will discuss the case insensitive search of some strings of characters within the Vim editor of the Ubuntu 20.04 Linux operating system. Therefore, you have to make sure that the Vim editor is completely configured on your end. We will be starting with opening the console application as we have to do most work at the terminal. So, use the Ctrl+Alt+T shortcut to do so. Within the terminal, you have to open any text or code file within the Vim editor. So, we have opened the already created new.txt file using the vim command shown in the screenshot.

$ vim new.txt

Your file will be opened in the Normal mode of Vim editor. You have to open the Vim insert mode by pressing the “I” button. Within Vim’s insert mode, you have to add some text data into this file having more than one occurrence of one word, i.e., Linux or is.

Open the visual mode of Vim using the “v” key on the Normal mode. You will be diverted to the Visual block as shown below.

Let’s say we want to search the word “is” within the text shown below without adding any case insensitive configurations. So, we have to press the “/” sign to search a string in the command mode. We have added the “is” string with the “/” character and pressed the Enter key.

The output would be something like below. You can see, it will show the first occurrence of the word “is” by highlighting it in the Visual mode of Vim as below.

To see the very next occurrence of “is”, we have to press the “n” or next button from the keyboard. The output below shows the 2nd occurrence of the word “is” at position 48.

To get back to the previous occurrence or in the backward direction, press the “p” or back button from the keyboard while working in the Visual Mode, as shown in the attached picture.

Ignore case Command Method:

The very first method to search a string without checking its case sensitivity is the set ignorecase command. So, within the Normal mode of Vim, open the command line with the “:” character. Now add the below-stated set ignore case command as press the key “Enter” to apply it. This is the direct method to do it. You can also do it within the Vim configuration file, i.e., vimrc.

To use the Vim configuration file, you need to add the below-stated command within the command line using the “:” character as shown below. Press Enter to open it.

You have to enable the filetype indent plugin and enable the properties you want to use in Vim, as shown in the image. After these properties, we have enabled the ignorecase by using the set command within the Vim configuration file as highlighted. To save the changes, try out the stated command in the command line as shown in the image as well.

After saving the changes and closing the Vim file, you are back to the Normal mode of the Vim editor. Now, you need to apply these changes to the Vim editor as well. So, use the source command in the command line, as shown in the image below.

Now, it’s time to see the changes by searching a certain string in the Vim editor. So we have used the “/” character in the Visual mode of Vim along with the string “Linux” to do a case insensitive search. Pressed the Enter key for final action.

In return, we have got the first occurrence of the word “Linux” highlighted at the Visual mode of Vim without considering the lower case of a searching string. The first occurrence contains the first capital character in it, while the searched string was all lower case.

Press the “n” or “next” button to search for the very next occurrence. You can see it has highlighted the word “LINUX” with all capital letters in it without considering its case sensitivity within the Visual mode.

Same as this, you can also search the very next occurrence of the word “Linux” without considering its case sensitivity. For that, use “n” or the next key for forwarding search and “p” or back button for backward search.

You can also make use of the “smart case” property along with the ignorecase of Vim. So, open the vim configuration file and enable the smart case property using the set command as shown in the image below. Now use the “wq” command in the command line to save and quit the vim configuration file.

:set ignorecase

:set smartcase

:wq

The smart case works a little differently from the ignore case. So, within the Visual mode of Vim, use the “/” with the string to search in the command line. Press enter for the final action.

You will see that the Vim visual mode will give you the output like below. It will highlight all the areas containing every occurrence of the word “Linux” in the text of the new.txt file. It will start from the first existence of the word “Linux” to the first character of the last existence of the word “Linux”.

Conclusion:

In this article, we have discussed the different methods to do case-insensitive searches within the Vim editor. We have discussed the ignorecase command and smartcase property within our examples. We have also searched without applying any case insensitive configuration, i.e., normal search. We hope this artefact will be in according to your expectations.

About the author

Omar Farooq

Hello Readers, I am Omar and I have been writing technical articles from last decade. You can check out my writing pieces.