Open In App

How to Search in Vim in Linux

Last Updated : 08 Jan, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

For many Linux users and developers, Vim is one of the most efficient and versatile text editors. Among its strong points is its search function, enabling users quickly to locate and re-arrange text within files. Here we will look at different ways and commands to search smartly in Vim on a Linux system.

Creating a Vim text File:

Step 1: Open your terminal and create a file using touch or any of the text editors, in this case, we will use Nano to create a text file by the name vimdemo.text.

nano vimdemo.txt

img1

Step 2: Now, type the content of the file or copy paste it, as you prefer. you can see the content in the below image to save the content in file press CTRL + X and then type ‘Y’ or ‘yes’ and hit enter.

img2

Step 3: We have created a text file now we need to open this file with Vim text editor, to do so just type the command in the terminal.

vim `filename`

img3

After the file is opened in Vim, we can perform various searches on it, below are the searches you can perform on your file, make sure that you can in console mode. you can enter in console mode by pressing `ESC` key.

Follow the simple commands mentioned below in console mode:

1. Basic Search: Basic Search includes searching a document or a file for a word or some sentences, Vim allows us to perform basic search which a simple command, To search for a specific term in Vim, you can press `/` followed by the term you want to search and then hit enter.

/search_term

img1

To navigate through the search results, use `n` for the next occurrence and `N` for the previous occurrence.

2. Case-insensitive Search: Vim offers search options with case sensitive content, if you want to search content no matter if it’s in upper case or lower case then you can use vim’s Case sensitive search, To perform this search, add `\c` before the search item. like shown in the below command.

/search_term\c

img2

3. Whole Word Search: If you don’t want to search the letters and you want that result should come when the whole word is found in document then you can apply whole word search, To search for whole words only, enclose the search term within `\b`. like the below command.

/\<search_term\>

img3

4. Highlighting Search Results: When we start searching sometimes we often miss the occurrences of a word or a letter, to make sure you don’t miss any letter or occurrence you can use search highlighting, you can Enable search result highlighting by adding the following command.

:set hlsearch

img4

Once the command is being run, it will enable the highlighting in your document and you can search through the document, like shown in the screenshot below.

img41

and, to turn off highlighting temporarily, use:

:nohlsearch

5. Search and Replace: when you want to replace the first occurrence of a word with another you can use this search option, to replace a term with another, use the `:s` command like mentioned below. Add the `g` flag at the end of the line to replace all the occurrences on a line.

:s/search_term/replacement_term/g

video-for-fifth

6.Type the Search and Replacement Pattern: Sometime after writing a document you may want to replace a word with some other word, but you see that there are too many occurrences of that particular word. now of you start replacing each one of them one by one it’s gonna take too much time. but vim offers search option to replace a term with another, When using the :substitute command for search and replacement, type the pattern as follows:

:%s/search_term/replacement_term/g

video-for-sixth

7. Search History/ Enter the Search Pattern: after you have applied different searches and wanna know what all searches you have already performed To view and navigate through search history you can press `q/`.

q/

img7

8. Search and Jump: Look for and move in Vim need joining line numbers with looking operation. By appending the line number to the search command (:123 /search_term), Vim fast places the cursor to where it finds your keyword on a certain line. This makes moving around big files faster and easier.

:123 /search_term

video-for-eight

9. Position the Cursor: To position the cursor on the first occurrence of the word you search, we will use `:normal` command to position the cursor. Type the command in the following order.

:normal /search_term

v1

10. Navigating through Search Results: Navigate through search results using n for the next occurrence and N for the previous occurrence. you can use this feature when there are multiple occurrences of a word in file and you need to go at a particular one.

11. Search Text using Regular Expressions in Vim: Vim supports powerful regular expressions for advanced search patterns. For example, to match any digit, use \d.

/\d

imglast

Conclusion

Learning to search in Vim is vital for effective text editing and manipulation. Once you learn and incorporate these commands and techniques into your workflow, navigating, searching, replacing text will be child’s play. Give these commands a try, and see how Vim’s powerful search functions can boost your productivity on a Linux system.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads