Vim is a widely used, light-weight, free, multi-platform text and code editor. It is very popular among developers because of its efficiency and ease of use. Though Vim is a simple text editor, it still needs some time to learn for advanced usage.
Learning the basics of Vim is very handy, therefore in this guide, we are going to focus on Vim editor’s search feature. Searching for some specific text (word/string) is one of the very common tasks when working with large files.
Let’s understand the search feature of Vim from basic searching to advance search techniques. Make sure you are in command mode before searching for something.
How to Do a Basic Search in Vim/Vi?
There are a couple of methods to search for a specific pattern in Vim:
- Forward searching
- Backward searching
Any pattern word can be searched in a backward or forward direction from the cursor position. In Vim editor, the search is case sensitive, for example, the “LINUX” and “Linux” will be searched differently. Therefore, there are various approaches to ignore case sensitivity. You can use “:set ignorecase” or “:set ic”. Another way to ignore case sensitivity is by simply adding “\c” with the search pattern: “/linux\c” and for capital words use “\C”.
1: Forward Searching
To begin a basic search, switch the mode if it is still in insert mode by pressing the “Esc” button. Then use the forward-slash “/” to quickly search text. For example, if you are looking for “Linux” in a file, then use “/Linux”, as shown in the images below:
The editor will highlight the first word that comes after the current cursor position. The search command will search the pattern, not the word. For instance, if you type “famous” after forward-slash, the editor will search all the words with the letters “famous” even if it is “infamous”, as demonstrated in the following image:
- Use “n” to jump to the next similar word
- Use “N” to go back to the previous word
2: Backward Searching
For backward searching, the process is the same, simply use “?” with the search string in place of forward-slash. The search will begin from the current position of the cursor, as shown in the following image:
Similarly, to jump to the next occurrence, use “n” and “N” in opposite direction.
How to search for a specific word in Vim?
To search a specific word in Vim, firstly, move the cursor to the word you want to search, now press the “Esc” button to switch mode, and then “*” for the next instance of the same word and “#” for the previous instance of the word.
How to search the entire word in Vim?
The procedure of searching the entire word in Vim is a bit different, the syntax is given below:
Vim will highlight the first word in the file.
How to Highlight Search Results in Vim?
Another important feature of Vim is highlighting the search results. To enable this feature, use “:set hlsearch”, and to disable it, use “:set !hlsearch”.
Vim is a lightweight text editor with numerous features that is very popular among developers. In this guide, we learned another key feature of the Vim editor. We also learned its basic forward and backward search, methods of finding a specific word, and running commands to highlight the search results and ignoring case–sensitivity while searching a pattern.