Vim is a popular open-source Unix text editor. In this article, we will discuss how to perform search operations in Vim / Vi.
How to Search in Vim / Vi [Vim Tutorial]
Before jumping into the main content, let’s have a look into the command set in Vim.
Editing Text Using Vim
cc
– replace an entire lineC
/c$
– replace from the cursor to the end of a linecw
– replace from the cursor to the end of a wordJ
– merge the line below to the current one with a space in between themgJ
– merge the line below to the current one without any space in between themr
– replace a single characters
– delete a characteru
– undoCtrl
+r
– redo
Search In File Using Vim
*
– jump to the next instance of the current word#
– jump to the previous instance of the current word/pattern
– Use this to search forward for the specified pattern?pattern
– Use this to search backward for the specified patternn
– repeat the search in the same directionN
– repeat the search in the opposite direction
Now, let’s have a step-by-step guide to search file in Vim or Vi editor. You must be in normal mode if you want to search in Vim. Usually, When you launch the Vim editor, you’re in the normal mode and if you want to go back to the normal mode from any other state then you can just do it by pressing the Esc key.
Basic steps for the search in a file using Vim:
- Press
/
. - Type the search pattern.
- Press
Enter
to perform the search. - Press
n
to find the next occurrence orN
to find the previous occurrence.
If you want to search for a whole word, then you can do it by /\<search pattern \>
. The search result is case-sensitive in Vim. Vim keeps track of all the search operations. if you want To see the search history, press /
or ?
and use the arrow up/down keys to find a previous search history.