Vim editor is known to improve the productivity of its users with its various keybinds and commands. But these keyboard shortcuts come with a bit of a learning curve. One of the most common issues for a beginner is using Vim to search for text as the usual CTRL + F method doesn’t work in Linux. Fear not friend, as in this article, we will explore different methods to search for text in Vim.
Forward or Backward Text Search in Vim
This is the most common and fundamental way to search for text in Vim and every user must know how it works. With this, you can quickly search for words, phrases, or even patterns within the given file. Follow these steps to search for text in Vim:
1. Position the Cursor
Move the cursor using the h, j, k, and l keys if you are in normal mode or with the arrow keys on the keyboard to place the cursor from where you want to start searching in the text.
2. Use the Search Command
In Vim, you can search for text in either the forward direction with the ‘/’ symbol or the backward direction using the ‘?’ symbol. Use the appropriate symbol for your search needs.
3. Type the Word to Search
Next, type the search word or phrase which you want to search for and hit enter. For example, to search for the word “Linux” in the forward direction, use this command:
/Linux
The cursor will move to the beginning of the searched text. This method searches for text in a case-sensitive manner. To ignore the case-sensitive search and search irrespective of case sensitivity, use \c with the command. Just add ‘\c’ before the text to search. For example:
/\clinux
4. Navigate through Search Results
Now, press “n” to move to the next search result in the forward direction or press ‘N’ to move in the backward direction. Once you’re done searching text, exit the Vim editor and move on to the next document.
Search Text using Regular Expressions in Vim
The previous method works flawlessly for simple words or phrases. But what if you need to search for a common pattern, say you want to filter all emails across the entire text? Don’t worry, as you can use regular expressions to search for specific patterns in Vim.
Regular expressions are a collection of characters consisting of both special characters and alphanumeric characters, representing a specific string pattern. For example, you can use the following regular expression symbols:
Regular Expression Symbol Action ‘.’ to match any single character ‘*’ to match any number of occurrences to the preceding character ‘+’ to match one or more occurrences of the preceding character ‘[]’ to match from a defined character set ‘\w’ matches with any word with matching preceding characters
Here’s how you can search for text in the Vim editor in Linux using regular expressions.
1. Position the Cursor
Use the arrow keys to move the cursor to demarcate the starting point for searching.
2. Use the Search Command
Press the ‘/’ symbol to search in the forward direction or use the ‘?’ to search in the reverse direction.
3. Enter the Search Pattern
Type the regular expression search pattern which you need to search for. For example, to search for all email IDs in the given text, use this:
\<[A-Za-z0-9._%+-]\+@[A-Za-z0-9.-]\+\.[A-Za-z]\{2,\}\>
Seems complicated? Let’s break it down –
- \< and \> marks the beginning and end of the email ID and ensures it is matched as a whole word.
- [A-Za-z0-9._-]\+ used to match every occurrence of alphanumeric characters, plus, dots, underscores, and hyphens
- @ exactly matches the ‘@’ symbol
- [A-Za-z0-9.-]\+ matches with every occurrence of alphanumeric characters, dots, and hyphens
- \. matches the dot in the email ID
- [A-Za-z]\{2,\} matches with the last part of the domain name like .com, .in, etc.
4. Navigate through Search Results
To move to the next search result in the forward direction, press ‘n’, or press ‘N’ to move in the backward direction.
Search and Replace Text in Vim Editor
Imagine you have to replace 100s of occurrences of a specific word. For such a situation, Vim has a built-in search and replace feature where you can use both simple text and regular expressions according to your needs for bulk changes and effective text management.
Here’s how you can search and replace text in Vim:
1. Enter the Command Mode
Press the ‘:’ button on the keyboard to enter the command mode where you can execute built-in Vim commands.
2. Type the Search and Replacement Pattern
Now type the search pattern and the replacement pattern as per this syntax to initiate the operation:
%s/search_pattern/replacement_text/<flags>
In the above syntax, the “%” symbol specifies Vim to apply the operation to the entire file, and the ‘s’ specifies to use the substitute command
Here are some flags you can use with this command:
- g – stands for “global” flag and is used to replace all occurrences of the search pattern with replacement throughout the text.
- c – stands for “confirm” flag and is used to prompt before each replacement takes place.
- i – stands for “ignore case” flag and is used to search cases insensitively in the given file.
For example, if you want to replace the word “Linux” with “Unix” throughout the given text while prompting before each replacement, then use this command:
%s/linux/unix/gc
You will see the following prompt at the bottom:
replace with Unix (y/n/a/q/l/^E/^Y)?
Press either ‘y’ to replace only the first occurrence, ‘n’ to skip the current replacement, ‘a’ to replace all occurrences, ‘q’ to quit this operation, ‘l’ to replace and quit. You can even press CTRL + E to scroll up or use CTRL + Y to scroll down.
Once done, you will get a confirmation message with the number of replacements made.
Frequently Asked Questions
To search for multiple words in Vim, first, use the search command ‘/’, then add the flag ‘\v’ and then write the multiple words separated by the pipe ‘|’ character like this syntax: \v<first_wor>|<second_word>|<third_word>