Vim – short for Vi Improved – is a powerful open source command-line text editor clone of the old vi editor. It’s highly configurable with an extensive manual and ships with a host of features, including syntax and color-coded highlighting, comprehensive plugin support, and search and replace, to name a few.
Searching for text or a string by manually scrolling up and down can be a daunting and time-consuming task. Fortunately, the vim editor has a faster and more convenient way to do this.
Main modes
provided by
Vim Vim
provides three main modes: command mode, insert mode, and visual mode. By default, the Vim editor starts first in command mode.
In this mode, keystrokes are inactive and a user cannot insert text or modify the file. However, you can navigate up and down, left and right, using the following keys:
k – Moves up one row. (Equivalent to the up arrow key)
j – Moves down one row. (Equivalent to the down arrow key)
l – Navigates one character to the right or moves forward. (Equivalent to the right arrow key)
h – Navigates a character to the left or moves backwards. (Equivalent to the down arrow key)
You can also prepend the keys with a numeric letter to move up or down a certain number of rows or move forward and backward a certain number of characters. For example,
6k – up 6
rows
4j – down 4 rows
The
insertion mode
This mode allows you to type text and make changes to a text file as you see fit. The insertion mode can be accessed from the command mode by pressing the following keys.
The ‘i’ key (insert) allows you to insert a character at the current cursor position.
The ‘a’ key (append) – This moves the cursor one character to the right and allows you to insert the mode.
The ‘o’ key – This creates a new line below the current row and switches to insert mode.
Visual
mode
is usually used to highlight text, similar to clicking and dragging with a mouse. To start making a text selection, simply type ‘v’ and then take advantage of the arrow keys to highlight the text.
Perform a basic search in Vim
To search for text, you must be in command mode. If you are in insert mode, simply press the ‘ESC’ key.
To perform a basic search for the string or text you want, go to the beginning of the file and simply press the forward slash ( / ) key. Then type the search string and press ENTER on the keyboard to start searching.
<img src
=”https://linuxhint.com/wp-content/uploads/2021/06/1-3.png” alt=”” />
The forward slash key ( / ) performs a direct search. Finds the string or pattern from the current cursor position to the end of the file. To search for the following pattern, simply press the letter n on the keyboard.
To search backwards, press the question mark symbol ( ?), type the search string, and then press ENTER on your keyboard. This searches for the string from the current cursor position to the start of the file.
NOTE:
The
search operation looks for a string or pattern and not the entire word. For example, if you search for the string ‘form‘, the search functionality will return results even when the string is present in more powerful or complete words such as ‘formal‘ and ‘uniform‘.
Search
for a complete word
To search for a complete word, start by pressing the /or? Symbol. After that, type the symbol \< to mark the beginning of the word to be searched and then /> to indicate the end of the search word. Then, finally, press ENTER to begin the search.
For example, to perform a direct search for a pattern, run:
Here, we are looking for the full word – ssh – in the /etc/ssh/sshd_config configuration file
.
Ignore case-sensitive
By
default, Vim is case-sensitive, as is the search master. To ignore case-sensitivity, overfix the search pattern with the \c operand. For example, /path\c looks for any occurrence of the string ‘path’, either uppercase or lowercase.
Another way to
accomplish this is to press the ESC key followed by two whole dots followed by the ignorecase text set or short form, set ic
.
Then press the ENTER key. After that, type the ( / ) followed by the search pattern. In the following example, notice how we get the uppercase string from the PATH pattern.
Search search history of
searched strings
Vim maintains a history of all search items. To view the searched strings, simply type /o ? in command mode and press the Up Arrow or Down Arrow keys to scroll through previously searched patterns.
Summary
That sums up how you can search for strings, patterns, or entire words in the vim editor.