Overview of Vim/Vi Editor: Importance of Efficient Searching
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.
Basic Vim/Vi Searching: Navigating Search Results
In the Vim/Vi text editor, there are two ways to look for a pattern of numbers or letters.
- Look ahead for the desired pattern
- Look for the specified pattern going backwards
About the cursor point, the direction is decided.
Looking for a Word’s Next Result: Searching Forward in Vim/Vi
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:
- N will advance you to the following instance of the pattern in the same direction
- N will advance you to the following instance of the pattern in the opposite direction.
Searching for a Word Backward: Commands for Backward Search
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:
- go to the following occurrence of the pattern while keeping n constant.
- hop with N to the following pattern instance in the opposite way.
Related Article: Run a Bash Script in Terminal
Looking for a New Word: Changing Search Terms Efficiently
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.
- Check that normal mode is selected by hitting Esc.
- Next, position the pointer over the desired word.
- You can: from here
- Use * to find the following occurrence of the word.
- Look for the word’s prior occurrence using #
Each time you press * or # the cursor moves to the next/previous instance.
Searching for Whole Words Only: Avoiding Partial Matches
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.
Steps to Open Files Directly at a Word: Practical Use Cases
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.
Performing Case-Insensitive Searches: Benefits and Use Cases
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.
Featured Search Results: Highlighting Important Matches
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"
Understanding Search History in Vim/Vi
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.
Conclusion
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:
- How to Set Up a VPN on Your VPS: Step-by-Step Guide
- VPS vs Cloud Hosting – Which is Better?
- What is the Difference Between VPS and VPN? Explained
- How to Add a Domain to Your VPS: Step-by-Step Guide
- What is KVM VPS Technology? Comprehensive Overview