How to Search Text in Vim Editor? [8 Cases]

LINUX
FUNDAMENTALS
A Complete Guide for Beginners Enroll Course Now

To search texts of a file in the Vim editor, type /<word> and press ENTER. It will place the cursor to the first match of “word” inside the file.

In this article, I will explore different strategies for searching in the Vim editor which will be helpful for Vim users to find the necessary parts of their files easily. So without further delay, let’s get started.

1. How to Search for Next Word (Forward Search)?

To search for the next word in the Vim editor, first, switch to the normal mode:Current cursor position.The image shows the initial cursor position.

Then type /<word> and press ENTER. It will take you to the first match of “word” inside the file. Here, I will search for the “for” word inside the file. To this, I will type /for and press the ENTER:Search result in Vim editor.

The image shows that four matches of the “for” word are found, and the cursor is at the exact next match of the initial cursor position. To move to the next match, press n, and press N to move to the previous match.

2. How to Search for Previous Word (Backward Search)?

To search for the previous word, first, switch to the normal mode. Then type ?<word> and press ENTER button. It will take you to the first match of “word” inside the file:Current cursor position.The image shows that the cursor is at line 12.

Now, I will search for the “for” word inside the file. To do this, I will type ?for and press the ENTER:Four matches found for search in Vim editor.

The image shows that four matches of the “for” word are found, and the cursor is at the exact previous match of the initial cursor position. To move to the next match, press N and press n to move to the previous match.

3. Search for Any Line Starting With a Word

To search for any line starting with a word, type /^<word> from the normal mode. Here, I will search for lines starting with “if”. To do this, I will type /^if :"If" at the beginning of a line.The image shows that the line starting with “if” is found in line 7.

4. Search for Any Line Ending With Specified Word

To search for any line ending with “word” type /<word>$ from the normal mode. Here, I will search for a line ending with “value”. To do this, I will type /<value>$:Line ending with value.The output image shows that the line ending with “value” has been found in line 3.

5. Case Insensitive Search in Vim

To search for case insensitive, type /<word>\c. This searching command will intend to ignore whether the case is upper or lower. For example, I will search for the “if” word. To do this, I will type /If\c and press the ENTER button:Case insensitive search result in Vim editor.The image shows that “if” is found to match with “If”, which is case insensitive.

6. Highlight Searched Result in Vim

To highlight the searched result type :set hlsearch in the normal mode. It will highlight all the search results.

The image below shows all the searched “if” words are highlighted:Search results are highlighted in Vim editor.By default, in many updated Vim versions, the search results are displayed in highlighted words. If this doesn’t happen by default after searching, you can insert :set hlsearch line inside the ~/.vimrc file. It will set to highlight all searched results permanently.

Note: To clear search result highlighting, execute :set nohlsearch in the normal mode. Or You can insert it inside the ~/.vimrc file.

7. Search Current Word in Vim

To find the current word (the word where the cursor is currently on), press * to find the next occurrence (the same word if any) and # to find the previous occurrence from the current cursor position. For instance, see from the following image, my cursor is currently on the “if” word of line 5:Cursor initial position.Now, press * to move the cursor to the next “if”:Current word is found in VimThe image shows that the cursor is moved to the next “if” after pressing “ * ”.

8. Search for Vim History

To go to the command history of the Vim Editor, type q: from the normal mode. It will open the command history of the Vim editor:Command search history in Vim.

Conclusion

Here, I have discussed different approaches to searching texts in Vim editor. This is a beginner guide for newbie users who use Vim for crafting their files. Don’t forget to put a rating of my article and give your valuable thoughts in the comment box.

People Also Ask

How to search a word in Vim editor?

To search in the Vim editor, type /<word> and press ENTER button in the normal mode. It will place the cursor to the first match of “word” inside the file.

How to use vim command?

To open a file in Vim using the vim command, type vim filename. Then edit the file from insert mode by pressing i key.  After editing switch to the normal mode pressing ESC to save, exit etc.:

  • To save file, type :w and press the ENTER.
  • To exit Vim, type :q and press ENTER.
  • To save unsaved changes, type :q! and press the ENTER It will forcefully quit the Vim editor.
  • To save the unsaved changes and quit the Vim editor, type :wq and press the ENTER.

How to search only selected text in Vim?

To search only selected text in Vim, first press ESC to exit selection mode. Then type /\%V and press the ENTER button. Here \%V indicates the Vim to search the last visual selection.

How to search text in terminal?

To search text in a terminal, use Shift + Ctrl + f.

How to clear search in Vim?

To clear the search result in Vim, press Ctrl. It will clear the searched highlighted texts until the next search takes place.

Related Articles


<< Go Back to Vim in Linux | Linux Text Editors | Learn Linux Basics

Rate this post
Susmit Das Gupta

Hello everyone. I am Susmit Das Gupta, currently working as a Linux Content Developer Executive at SOFTEKO. I am a Mechanical Engineering graduate from Bangladesh University of Engineering and Technology. Besides my routine works, I find interest in going through new things, exploring new places, and capturing landscapes. Read Full Bio

Leave a Comment