Text editors like How to Search to Find a Word in Vim or Vi Text Editor? and its predecessor Vi are extensively used. They may be found on the majority of Linux and macOS distributions and are both open-source (free).
Finding terms or patterns to search for in Vim/Vi is a typical activity that makes it simple to browse through huge files.
This lesson from our Blog uses real-world examples to demonstrate how to search in Vim/Vi.
In the Vim/Vi text editor, there are two ways to look for a pattern of numbers or letters.
About the cursor point, the direction is decided.
Use the command: to advance the search.
/pattern
Put the item(s) you want to find instead of a pattern.
For instance, to find all occurrences of the pattern “root,” you would:
1: To make sure Vim/Vi is in regular mode, use Esc.
2: Enter this command:
/root
The first occurrence of the pattern after the cursor is highlighted in the text editor. You can see that the outcome is a word’s worth of text that includes the desired pattern in the image below.
This is so because searching for complete words is not specified by the command.
Choose Enter from this position, then:
How to Search to Find a Word in Vim or Vi Text Editor? Type the following in the search box to go backward:
? pattern
Put the item(s) you want to find instead of a pattern.
For instance, if you wanted to find every instance of the pattern “nologin,” you would:
1: To make sure Vim/Vi is in regular mode, use Esc.
2: Enter this command:
? nologin
Before the cursor, Vim/Vi highlights the first occurrence of the chosen pattern.
To confirm the search, press Enter. So you can:
Related Article: Run a Bash Script in Terminal
The word where the cursor is situated is the current word.
Move the cursor to a word and tell Vim/Vi to look for the next instance of that word rather than describing the pattern and asking it to find it.
Each time you press * or # the cursor moves to the next/previous instance.
How to Search to Find a Word in Vim or Vi Text Editor? To search for whole words, use the command:
/\
The text editor only highlights the first whole word precisely as specified in the command.
Vim (and Vi) can open files at a specified word allowing you to skip a step and go directly to the searched term.
To open a file at a specific word, use the command:
vim +/word [file_name]
or
vi +/word [filename]
For example, to open the /etc/passed file where it first uses the term “root”, use the command:
vim +/root /etc/passed
The first line that the text editor shows when it opens the file and displays the word “root” is shown in the illustration below.
When searching within the file, Vi(m) defaults to treating the case differently. For instance, when you search for the term “user,” results without the capital U are not displayed (i.e., User).
Case insensitivity can be achieved in Vim/Vi in a number of ways.
Use the following command to conduct a case-insensitive search for a certain word:
/\<word\>\c
How to Search to Find a Word in Vim or Vi Text Editor? Vi(m) is told to ignore case and display all results thanks to the c property.
Vim can be configured to ignore case for all searches across the entire session as an alternative. Run the following command to do so:
: set disregard case
The configuration file can also be changed to make the text editor disregard the case permanently. Add the line: set ignore-case to the. vimrc file by opening it.
The nice feature in Vi(m) that emphasizes search results in the file is quite helpful. Enter the following command in the text editor to enable highlighting:
hlsearch set
Run: to turn off this functionality.
set to "hlsearch"
Vi (m) records the search commands used throughout the session. Use the up and down keys along with the characters. or / to navigate through previously used commands.
After reading this tutorial, “How to Search to Find a Word in Vim or Vi Text Editor?” you will see many possibilities for using Vim to search for and find words.
Once the search term has been located, you can continue by text-cutting, copying, or pasting.
Read More: