vim

How to search for text in VIM

Vim – short for Vi Improved – is a powerful open-source command-line text editor clone of the good old vi editor. It is highly configurable with an extensive manual and ships with a wealth of features, including syntax highlighting and color codes, comprehensive plugin support, and search and replace, to mention a few.

Searching text or a string by manually scrolling up and down can be a daunting and time-consuming undertaking. Thankfully, the vim editor has a faster and convenient way of doing this.

Main modes provided by Vim

Vim provides three main modes: command mode, insert mode, and visual mode.

By default, the Vim editor is first launched in command mode. In this mode, key-presses 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 of Arrow up key )

j – Moves down one row. (Equivalent of Arrow down key )

l – Navigates one character to the right or moves forwards. (Equivalent of Arrow right key)

h – Navigates one character to the left or moves backward. (Equivalent of Arrow down key )

You can also prefix 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 – moves up 6 rows

4j – Moves down 4 rows

The insert mode

This mode allows you to type in text and make changes to a text file as you deem fit. The insert 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 cursor’s current position.

The ‘a’ key ( append ) – This moves the cursor one character to the right and gets you to insert mode.

The ‘o’ key – This creates a new line below the current row and switches to insert mode.

The visual mode

The 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’ then leverage the arrow keys to highlight text.

Perform a basic search in Vim

To search text, you need to be in command mode. If you are in insert mode, simply press the ‘ESC’ key.

To perform a basic search of the string or text that you want, move 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.

The forward-slash ( / ) key performs a forward search. It searches for the string or pattern from the current cursor position right up to the end of the file. To search for the next pattern, simply press the letter n on the keyboard.

To search backward, press the question mark symbol ( ? ), type the search string, and press ENTER on the keyboard. This searches the string from the current cursor position to the start of the file.

NOTE:

The search operation searches for a string or pattern and not the entire word. For instance, if you search for the string ‘form’, the search functionality will return results even when the string is present in more powerful or whole words such as ‘formal’ and ‘uniform.’

Search for a complete word

To search for a complete word, begin by pressing the / or? Symbol. After that, type the \< symbol to mark the beginning of the word to be searched then /> to signify the end of the search word. Then finally, hit ENTER to commence the search.

For instance, to perform a forward search of a pattern, run:

/\<word/>

Here, we are searching for the complete word – ssh – in the /etc/ssh/sshd_config configuration file.

/\<ssh/>

Ignore case sensitivity

By default, Vim is case sensitive, and so is the search pattern. To ignore case sensitivity, suffix the search pattern with the \c operand. For example, /path\c searches for any occurrence of the string ‘path’, whether upper or lowercase.

Another way you can achieve this is to press the ESC key followed by a whole colon followed by the text set ignorecase or the short form, set ic.

Next, hit the ENTER key. After that, type the ( / ) followed by the search pattern. In the example below, notice how we get the uppercase string of the pattern PATH.

/path

Search the history of searched strings

Vim keeps a history of all the search items. To view the strings searched, simply type / or ? in command mode and press either the Arrow up or Arrow down keys to scroll through previously searched patterns.

Wrapping up

That summarizes how you can search for strings, patterns, or complete words on the vim editor.

About the author

Karim Buzdar

Karim Buzdar holds a degree in telecommunication engineering and holds several sysadmin certifications. As an IT engineer and technical author, he writes for various web sites. He blogs at LinuxWays.