How to Find or Search a Word in Vim or Vi Text Editor

Table of Contents
Find or Search a Word in Vim or Vi Text Editor
Vim is a popular text editor that comes preinstalled on macOS, Linux Distros and BSD Like operating systems. Vim is a free and opensource text editor available today. Finding or searching a word is a frequent task that we often do. In this tutorial, you are going to learn How to Find or Search a Word in Vim or Vi Text Editor.
Finding a Word in Vim or Vi in Normal Mode
Follow below instructions to search a word in Vim:
- If you are in any other mode then press the
Esc
key to come in Normal Mode. - Now, Vim supports search in two ways forward or backward. If you want to search forward then press
/
and type the word you want to search. - Otherwise, If you want to search backward then press
?
and type the text you want to search followed byEnter
the key. - Now once you get the occurrence of the word. Then you can press
N
to get the next occurrence of the word in the forward direction. - Otherwise, you can press
n
to get the next occurrence of the word in a backward direction.
Searching Word in Vim or Vi Examples
The following are the examples for searching words in Vim or Vi.
Search a Word in Vim in the Forward Direction
In this example, we are going to search word systemd
inside the file /etc/group
.
First, open /etc/group
file using the following command:
vim /etc/group
Now press Esc
key to come in normal mode.
Then press /
key and enter the word systemd
that we want to search and hit Enter
:
/systemd
You can press n
key to get the next occurrence in the backward direction. You can also press N
key to get the next occurrence in the forward direction.
Search a Word in Vim in the Backword Direction
In this example, we are going to search word systemd
inside the file /etc/group
.
First, open /etc/group
file using the following command:
vim /etc/group
Now press Esc
key to come in normal mode.
Then press ?
key and enter the word systemd
that we want to search and hit Enter
:
?systemd
You can press n
key to get the next occurrence in the backward direction. You can also press N
key to get the next occurrence in the forward direction.
How to Highlight the Words When Search
You can enable highlight words when searching by using the :set hlsearch
command. To enable it, inside the normal mode of Vim enter the following command.
:set hlsearch
To stop highlighting words when search, enter the following command:
:set !hlsearch
Search a Current Word in Vim or Vi
The word where the cursor is located called the current word. To find current word follow below instructions:
- Move the cursor on the word that you want to search.
- Now press
*
to search the word in the forward direction. You can also get the next occurrence by pressing the*
key. - You can press
#
to search the word in the backword direction. You can also get the next occurrence by pressing the#
key.
Conclusion
In this tutorial, you have learned How to Find or Search a Word in Vim or Vi Text Editor. If You have any queries regarding this please comment below.
LATEST POSTS
-
How to Install Arduino IDE on Linux Mint 19
-
How to Install PhpStorm on CentOS 8
-
How to Install Git on CentOS 7
-
How to Install WordPress with LAMP Stack on Debian 9
-
How to Install CMake on CentOS 8
-
How to Install Gradle on CentOS 8
-
How to Install Mozilla Firefox on Ubuntu 18.04
-
How to Install Papper Flash on Ubuntu
-
How to Install Notepad++ on Ubuntu 18.04
-
How to Install IntelliJ IDEA IDE on Debian 10
-
How to install Laravel PHP Framework With Nginx on Ubuntu 18.04
-
How to Change Hostname on Debian 9