How to search in VIM/VI Editor? [VI/VIM Search]

Finding/Searching a text in VI/VIM Editor is a very common task, though this article you will know that how to find/search in VIM/VI editor/ known as VI/VIM Search

Updated: 22 May, 23 by Susith Nonis 11 Min

List of content you will read in this article:

Vim, a sophisticated and highly customizable command-line program, is a text editor that enables quicker text editing. Vim was invented by Bram Moolenaar. Depending on our needs, we may make use of the different plugins available in Vim. As in macOS and most of the best Linux distros, VI Editor comes Preinstalled. One of the most common tasks for everyone is Finding/Searching a text in Vim/VI Editor while working with files. As this is very basic information, So working and boosting your productivity with such basic Vi commands will help you. this article gives you a brief about Vim Search/Vim Find/Vi Search/Vi Find.

VIM, or Visual Editor for Unix-like Operating Systems, provides the greatest command-line text file editing experience for Unix. Vim is a revamped version of recent operating systems' popular vi text editor. This is a text editor with a lot of options.

1.   Simple Vim Search

Users must be in normal mode to search in Vim. When you open the Vim editor, you're in this mode and hit the Esc key to return to regular mode from any other mode.

You can quickly discover text using the Vim's//forward slash and?' question mark. Press '/' to search for the forward searches, and click '?' for backward searches. Input the search pattern and click Enter to search.

The following are the fundamental stages for searching in Vim:

  1. Press the button of / key.
  2. In the box, type the criteria for the scan.
  3. Click Enter to commence the search.
  4. Press N to identify the next incident, then press N to discover the previous incident.

For example,

Open file1 in the vim editor

$ vim /file1

Search for a word named "Hotel" in the forward direction:

  • Press ESC key
  • Type /Hotel
  • Hit n to search forwards for the next occurrence of a word named "Simran". You can press N to search backward.

2.   Look for the Entire Word

To find out the complete word, start your search by pressing / or ?, type \< to mark the starting of a required word, enter the search pattern, type \> to mark the endpoint of the word, and hit Enter to perform the search.

For example, to find "Linux," you might type /\<Linux\>:

3.   Specify a Word to Open a File at That Word

Vim/Vi can open files at a certain word, allowing you to skip a step and get straight to the phrase you're looking for.

Use the following mentioned command for Vim find.

vim +/word [filename]

For example, to access the /d1/f1 file where the phrase "xyz" appears for the first time, run the command:

vim +/xyz /d1/f1

4.   Find in Vim Without Regarding for Case Sensitivity

When looking inside a file, Vim is case-sensitive by definition. For example, when you search for the word /Linux, no results with the uppercase L appear as "Linux".

There are several techniques to accomplish case insensitivity in Vim.

Use the following Linux command to search for a specific term using the case insensitive attribute:


5.   Results from Your Search Should be Highlighted

Vim provides a handy function that emphasizes the file's search results. In the text editor, run the following command to activate highlighting:

: set hlsearch 

6.   Vim Search History 

Vim maintains track of all the searches you've done so far in this session. Use the arrow keys to discover a prior search operation, hit / or? and move through the search history. Hit Enter to start the search. Before executing the procedure, you may also change the search pattern.

7.   Look up the Current Word

You may also find a full word by dragging the cursor over it and hitting * (asterisk) to search forwards or # (hash) to look backward. Press * or # once more to discover the next matches.

For example,

Let's say you open the following file:

$ cat file.txt

Place the cursor on any word in normal mode, such as 168. To search for the next occurrence of the word 168, press * or # to search backwards.

8. Search for the previous word (backward search) in the VIM editor:

In VIM editor, you can easily search for the previous occurrence of a word using the backward search feature. To initiate a backward search in the VIM editor, follow these steps:

  1. Press the "/" key to enter the search mode in VIM.
  2. Type the word you want to search for in reverse order. For example, if you want to find the previous occurrence of the word "example," type "elpmaxe".
  3. Press the "Enter" key to perform the search.

VIM will find the previous instance of the word and position the cursor at its location. You can press the "n" key to find the next occurrence. Similarly, pressing the "N" key will search for the previous occurrence.

9. Search and open a file from the CLI in the VIM editor:

VIM editor provides a convenient way to search for and open files directly from the command line interface (CLI). To search and open a file in VIM, follow these steps:

  1. Open the terminal or CLI on your system.
  2. Type the following command to open the file in VIM:

vim filename

Replace "filename" with the actual name or path of the file you want to open.

  1. Press the "Enter" key to execute the command. VIM will open the file in a new buffer, allowing you to view and edit its contents.

Using this method, you can quickly search for and open any file in VIM without first opening the editor and then manually navigating to the file.

10. Finding a Character String in the VIM editor:

VIM editor offers powerful search capabilities to find a specific character string within a file. To search for a character string, follow these steps:

  1. Enter the VIM editor by opening a file using the following command:

vim filename

  1. Press the "/" key to enter search mode.
  2. Type the character string you want to search for and press "Enter". For example, to search for the word "example," type "/example".
  3. VIM will highlight the first occurrence of the character string. To find subsequent occurrences, press the "n" key. To search in the VIM editor in the opposite direction, press the "N" key.

VIM provides various options to refine and customize your search, such as case-insensitive searches, searching within a range of lines, and using regular expressions.

11. Refining the Search in the VIM editor:

You can use additional search options to refine your search in the VIM editor and narrow down the results. Here are a few techniques to refine your search in the VIM editor:

  • Using case-insensitive search: Add the "\c" option before the search pattern to perform a case-insensitive search. For example, to search in the VIM editor for the word "example" regardless of case, use "/\cexample".
  • Searching within a range: You can restrict the search to a specific range of lines by specifying the line numbers before the search pattern. For example, to search for the word "example" between lines 5 and 10, use "5,10/example".
  • Utilizing regular expressions: VIM supports powerful regular expressions for searching. You can employ regular expressions to search for complex patterns within the text. For example, to search for words starting with "vim," use "/\vim\w*".

These techniques allow you to customize your search in the VIM editor and make it more precise, saving time and effort.

12. Replacing a Character String in VIM Editor

VIM offers powerful search and replaces functionality, making replacing character strings within your files easy. To replace a character string in VIM, you can follow these steps:

  1. Open the file in VIM by running the command: `vim filename`.
  2. Press the `:` key to enter command mode at the bottom of the screen.
  3. Type `%s/old_string/new_string/g` and hit Enter.
  •     `%s` represents the substitute command in VIM.
  •     `old_string` should be replaced with the string you want to replace.
  •     `new_string` should be replaced with the desired replacement string.
  •     The `g` at the end stands for "global" and ensures that all string occurrences are replaced in the file.
  1. VIM will highlight each occurrence of the old string individually and prompt you to confirm the replacement—Press `y` to replace the highlighted occurrence or `n` to skip it.
  2. You can also use the `:%s/old_string/new_string/gc` command to confirm each replacement interactively.

13. Searching Forward for the Next Result of a Word in VIM Editor

Searching for specific words or patterns can be incredibly useful when working with large files or navigating through code. To search forward for the next result of a word in VIM, follow these steps:

  1. Open the file in VIM by running the command: `vim filename`.
  2. Press the `/` key to enter search mode at the bottom of the screen.
  3. Type the word or pattern you want to search for and hit Enter.
  • To search for the word "example," type `/example` and press Enter.
  1. VIM will move the cursor to the next occurrence of the searched word or pattern. To repeat the search and move to the next occurrence, press `n`.
  2. If you want to search backward for the previous occurrence, press `N`.

14. Opening a File at a Specific Word in VIM Editor

In VIM, you can open a file and automatically position the cursor at a specific word or line. To open a file at a specific word, follow these steps:

  1. Open VIM by running the command: `vim`.
  2. Type `:e filename` to open the desired file.
  • Replace `filename` with the file name you want to open.
  1. After the filename, add `+` followed by the line number or the word you want to position the cursor at.
  • To open a file named "example.txt" and position the cursor at line 10, use the command `:e example.txt +10`.
  • To position the cursor at a specific word, replace the line number with `/word` in the command. For example, `:e example.txt +/search_word`.
  1. Press Enter, and VIM will open the file and place the cursor at the specified location.

By following these instructions, you can effectively search, replace, and navigate within your files using VIM, improving your productivity and efficiency as a text editor user.

Hence in this article, we look at how to search in Vim using the forward Slash '/' and question mark '?'. Also, various other search techniques may help us easily find the specific search criteria or pattern in our document. Hence using these methods, you can easily find in Vim editor and get what you want. Vim is really user-friendly and very easy to use for the developers, and the search command can accelerate the search tasks that have to be performed in the Vim editor. Hence, this tutorial helped you understand the Vim editor's search functionality.

People Are Also Reading:

Susith Nonis

Susith Nonis

I'm fascinated by the IT world and how the 1's and 0's work. While I venture into the world of Technology, I try to share what I know in the simplest way with you. Not a fan of coffee, a travel addict, and a self-accredited 'master chef'.