Vim and Vi are commonly used text editor in linux and macOS. Sysadmins and power users use these handy tools to quickly create and edit various documents without using third-party apps.
While Vim is an upgraded version with added features, both share the same fundamental functionality.
Since Vi and Vim are CLI-based, a common challenge you might face while working with these editors is finding specific words quickly (in the forward/backward directions), especially when working through large documents.
Searching for words and word patterns in Vim or Vi is fundamental to optimizing your editing workflow.
In this comprehensive tutorial, we’ll explore the search techniques and features of these editors. So whether you’re a beginner or a power user looking to explore advanced patterns in Vim and Vi, we’ll describe the useful techniques you can use to search for a word.
Basics of Pattern Searching in Vim / Vi
Let’s start with an overview of the Vi and Vim search capabilities and discuss five use cases where these capabilities save you significant time and effort.
Understanding the Vim / Vi Search Functionality
Despite their basic-looking interface, Vi and Vim are powerful editors with significant search capabilities. The challenge is how users can access and use these capabilities in their editorial workflows.
We’ll now explore five simple use cases that highlight these capabilities.
Use Case #1: Forward Word Search
When you’re working in Vim / Vi and need to find the next occurrence of a word, use the forward word search. This feature quickly locates that occurrence as you move through the text.
To use this feature, first make sure you are in Normal mode. Now, simply type / followed by the word or pattern you want to search for, and press Enter. The cursor will move to the next occurrence of the word.
For instance, typing /example
will find the next instance of the word “example”.
Use Case #2: Backward Word Search
To search backward in the document, first go to the normal mode. Type ? followed by the word or pattern. Press Enter
to find the previous occurrence of the word.
For instance, typing ?example
searches for example in the backward direction.
Use Case #3: Current Word Search
Vim and Vi allow you to search for the current word under the cursor. Simply press * to search forward or # to search backward.
If the cursor is on the word “function,” pressing * will search for the next occurrence of “function.”
Use Case #4: Whole Word Search
Use the syntax /\<word\>
to search for a whole word in the text. This ensures that the search pattern matches only the complete word.
Searching for /\<function\>
will find exactly the word “function” but not “functions” or “functional”.
Use Case #5: Open a File at a Specific Word
When working with huge text files or code bases, the option to open a file at a specific word or pattern in Vim or Vi is a time-saver. This unique feature lets you open a file and place the cursor at the specified position in the file.
Use the following command to use this feature:
# vim +/word filename
(or)
# vi +/word filename
For instance, you have a Python script named example.py and need to edit the function named calculate_area. Instead of opening the file and manually searching for the function, you can use the following command:
# vim +/calculate_area example.py
This command opens example.py in Vim, with the cursor positioned at the first occurrence of calculate_area
.
Advanced Word Search Techniques
The advanced word search features in V and Vim allow you to leverage regular expressions for more complex word pattern search, case insensitivity, and search & replace capability.
Regular Expressions
Vim and Vi support regular expressions for more complex searches.
For example, using vim +/^def\ calculate_area example.py
will take you to the line where the calculate_area
function is defined.
Search and Replace
Going a step further than the traditional search feature, Vim and Vi allow you to search and replace text.
For instance, use the following command to replace “old” with “new” across the entire file.
:%s/old/new/g
Case Insensitivity
By default, Vim and Vi search operations are case-sensitive. However, you can toggle case-insensitive search in Vim by using the following command:
:set ignorecase
Highlight Search Results
While many users don’t expect result highlighting from a command-line editor, Vim offers this functionality that makes it easier to spot and navigate through occurrences of the search pattern. This visual cue is especially useful when you are working with large files or want to ensure you’ve located all instances of a word.
Use the following command to activate the highlight feature for search results in Vim:
:set hlsearch.
Consider that the highlight feature is enabled in Vim.
Now, if you search for the word “config” in a configuration file with /config
, every occurrence of “config” will be highlighted throughout the document. This makes it easy to scan and locate all instances of the search term.
Let’s check out what you can do with this feature:
Navigate Highlighted Results
Once the search results are highlighted, you can easily move the cursor between them with n for forward search and N for backward search — a common and efficient way to navigate search results in Vim.
Customize Highlight Appearance
Vim makes it straightforward to customize the appearance of highlights, such as color and style. We recommend going through Vim’s color scheme settings and changing your preferences and working environment.
Disable Highlight
If you ever need to disable the highlight, especially after finishing your search, use the command :nohlsearch
or :noh
. This clears the current search highlighting without impacting future searches.
Access Search History
Vi and Vim maintain a search history, enabling users to re-find a previously searched word. This saves time and effort, especially when you need to repeatedly find word patterns in a file.
To access the search history in Vim or Vi, you simply need to open the Vim editor, enter normal mode, and then press q/
for forward search history or q?
for backward search history. This brings up a list of your recent searches.
Navigate History
Once the search history window is open, you can use the arrow keys to navigate the list. Place the cursor on the desired search term and press Enter
to execute that search again.
Working with Search Results
Once you have found the word or pattern, Vim and Vi offer various commands to navigate and manipulate these results. For instance, use n
to move to the next occurrence and N
for the previous one. Plus, these editors remember your search history, making it easy to revisit past searches.
Conclusion
Mastering search operations in Vim or Vi is a valuable skill for programmers and system administrators. From forward searches to customizable highlighting and efficient navigation, these features enhance productivity in working with documents and code.
Whether searching for specific words or customizing the editor’s appearance, Vim and Vi are versatile tools.
FAQs
Q. How can I search for a word in Vim or Vi text editor?
In normal mode, press / followed by the word you want to search for, and then press Enter
.
Q. What is the command to search for the current word in Vim?
Place the cursor on the word you want to search and press * in normal mode to search for the current word.
Q. How can I search for a whole word in the Vim editor?
Use the command /\ to search for the whole word in Vim, where \ represents the word you want to search for.
Q. Is it possible to perform a case-insensitive search in Vim?
You can perform a case-insensitive search in Vim by adding \c to the search pattern, e.g., /\cword
will search for word
case-insensitively.
Q. What is the method to search for a word in the backward direction in Vim?
To search for a word in a backward direction, press ? followed by the word you want to search for, and then press Enter
in normal mode.
Q. How can I replace text in Vim or Vi text editor?
To replace text in Vim, use the command :%s/oldtext/newtext/g
, where oldtext
is the text you want to replace, and newtext
is the replacement.
Q. Can I search text using regular expressions in Vim?
You can search text using regular expressions in Vim by using the appropriate regular expression pattern in the search command.
Q. What is the method to search and replace text using regular expressions in Vim?
To search and replace text using regular expressions in Vim, use the command “:%s/regex/newtext/g
,” where “regex” is the regular expression pattern and “newtext” is the replacement.
Q. How do you search for the previous occurrence of a word in Vim?
Use the command “N
” in normal mode to search for the previous occurrence of the last searched word in Vim.
Q. Is Vi or Vim text editor preinstalled in Linux?
Vi comes preinstalled in most Linux systems, while you may need to install Vim on some distributions.