Vim/Vi is an open-source text editor in Linux based operating system used for editing words or patterns. We can search the words/patterns forward and backward in Vim. The search command helps you to find the matching word from the document directly.
This guide will assist you in searching the words/patterns easily in Vim/Vi. The following points will be discussed in this article:
- How to Search in Vim?
- Example 1: Search Forward in Vim
- Example 2: Search Backward in Vim
- Example 3: Search with a Command in Vim
- Example 4: Search the Current Word Forward & Backward in Vim
- Example 5: Search Word Without Case Sensitivity
- Example 6: Highlight the Search Word
Let’s discuss these methods in detail in the next section.
How to Search in Vim?
In normal or visual modes, the different ways to search the word/pattern are possible in Vim/Vi text editor. Let’s discuss it!
Example 1: Search Forward in Vim
The forward search means from the start to the end of the document. To search “forward” in Vim/Vi text Editor, follow the below steps:
- Open the file using the “vim filename” command in the Vim text editor and press the forward-slash (“/”):
- After “/”, search the word/pattern (in this case: “Vim”) and press “Enter”. The matched word will be highlighted:
- To navigate to the next matching word in the document, press “n” for moving forward and press “N” for moving in the reverse direction.
Example 2: Search Backward in Vim
To search a keyword in the “backward” direction in the Vim text editor, follow the below-mentioned steps:
- Open Vim and press the “?”.for backward search:
- Enter the desired keyword after the “?” (in this example: text), and hit the “Enter” key:
- To navigate for the matching word within the Vim document, press “n” to move backward for the next occurrence or “N” for moving forward.
Example 3: Search with a Command in Vim
We can search for a word in Vim using the below pattern. Replace “editor” with the specified searched word. To search the word “editor” in my document:
/\<editor\>
It will take you to the last occurrence of the searched word; press “n” or “N” to navigate to the next occurrence in the document.
Example 4: Search the Current Word Forward & Backward in Vim
Sometimes, you need to search the occurrences of the current word in the entire document; Vim allows you to search the current word. Take the cursor to the search (in my case: “is”) word and press “*” to search forward and “#” to search backward:
Note: The occurrence “line number” and index on the specific line are also shown on the bottom-right side.
Example 5: Search Word Without Case Sensitivity
The Vim editor is case-sensitive, as the terminal. But if you want to make the Vim case insensitive, use the “:set ignorecase” in Vim:
:set ignorecase
Now, search for any word with case insensitivity. In the below example: I searched for “LiNe”, and it’s showing results for “line”, which is not possible without “:set ignorecase”:
Another way to search for case insensitive words directly is using the “\c” with the search command. For example: to search for Vim, we can use the following pattern:
/\<vim\>\c
Example 6: Highlight the Search Word
If you want to highlight the searched word for clarity, use the “:set hlsearch” and then find the desired word. The matching word will be highlighted (yellow):
: set hlsearch
That’s all from this article.
Conclusion
To search a word/pattern in Vim/Vi, use the “/<word>” for forward and “?<word>” syntax for backward search in the document. Moreover, we can search the word utilizing the “/\<editor\>” command, and for searching the current word in the whole document, use “*” or “#”. The method of avoiding case sensitivity and highlighting the text is also discussed in the guide.