Introduction: Vi and vim is a text editor for Linux, macOS, Unix, and *BSD family of operating systems. Vim is a free and open source text editor. One can search using various vi text editor keys. This page shows how to search and find words in vi or vim text editor running on a Linux or Unix-like systems.
|Operating system/app||vim or vi text editor|
|Root privileges required||No|
|Estimated completion time||5m|
To search using Vim/vi, for the current word:
- In normal mode, you can search forward or backward.
- One can search forward in vim/vi by pressing
/and then typing your search pattern/word.
- To search backward in vi/vim by pressing
?and then typing your search pattern/word.
- Once word found in vim, you can press the
nkey to go directly to the next occurrence of the word in backwards. Press the Nkey to go directly to the opposite direction, i.e. forwards.
Let us see some examples.
Let us open a file named /etc/passwd:
$ vi /etc/passwd
$ vim /etc/passwd
Search for a word named “vivek” in forward direction:
nto search forwards for the next occurrence of word named “vivek”. You can press Nto search backwards.
Let us open a file named demo.txt in the current directory:
$ vi demo.txt
$ vim demo.txt
Search for a word named “bar” in backwards direction:
Nto search backwards for the next occurrence of word named “bar”. You can press nto search forwards.
Say you have a file named data.txt as follows displayed using the cat command:
$ cat data.txt
192.168.2.254 - default router 192.168.2.253 - wifi 192.168.2.252 - wifi bridge 192.168.2.254 - dns server 192.168.2.30 - backup server 192.168.2.254 - firewall 192.168.2.18 - vm server 192.168.2.203 - RHEL7 192.168.2.254 - dhcp server 192.168.2.200 - SUSE server 192.168.2.201 - FreeBSD nfs server
In normal mode, move the cursor to any word say 254. Press * to search forwards for the next occurrence of word 254, or press # to search backwards:
The vi / vim text editor supports running any : command using the following syntax:
vi +commandHere fileName
vim +LineNumber fileName
vi +/searchTermHere fileName
vi +/LineNumberHere fileName
vim +/LineNumberHere fileName
To open file and go to function called main(), enter:
$ vim +/main filename-here
Next open file and go to line number 42, enter:
$ vim +42 fileName
See “VI / VIM: Open File And Go To Specific Function or Line Number” for more info.
We can search for a whole word in VIM using the following syntax:
/\<WORD-TO-SEARCH\> # search for foo word /\<FOO\> # search for FreeBSD word /\<FreeBSD\> # Case sensitive vs insensitive search done by adding the \C or \c # /\<FreeBSD\> " Case sensitive /\<FreeBSD\>\C " Case sensitive /\<FreeBSD\>\c " Case insensitive
We can also do case insensitive search in Vim using the following two config options. For example, ignore case in search patterns, type:
Now search it to match Foo, foo, FOO and so on:
Another option is to override the ‘ignorecase’ option if the search pattern contains upper case characters.
:set smartcase "
Please note that the ‘ignorecase’ also applies to search and replace option under vim.
Getting help is easy for search commands. All you need to do is type the following commands:
This page shows how to search using Vim or vi text editor for the word, including the use of the super star (*) to search for the current word. See vim help page for more info here.