I am a new Linux and Unix-like operating system user. How do search and find words in Vi Vim? How can I find a Word in Vim or vi text editor?

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.

Find a word in Vim or vi text editor

To search using Vim/vi, for the current word:

  1. In normal mode, you can search forward or backward.
  2. One can search forward in vim/vi by pressing / and then typing your search pattern/word.
  3. To search backward in vi/vim by pressing ? and then typing your search pattern/word.
  4. Once word found in vim, you can press the n key to go directly to the next occurrence of the word in backwards. Press the N key to go directly to the opposite direction, i.e. forwards.

Let us see some examples.

Searching for words in vim/vi

Let us open a file named /etc/passwd:
$ vi /etc/passwd
OR
$ vim /etc/passwd
Search for a word named “vivek” in forward direction:

  • Press ESC key
  • Type /vivek
  • Hit n to search forwards for the next occurrence of word named “vivek”. You can press N to search backwards.
Linux Unix macOS find a Word in Vim or vi text editor
Demo of finding a word in Vi/Vim

How to search for a word in backwards in vim/vi

Let us open a file named demo.txt in the current directory:
$ vi demo.txt
OR
$ vim demo.txt
Search for a word named “bar” in backwards direction:

  • Press ESC key
  • Type ?bar
  • Hit N to search backwards for the next occurrence of word named “bar”. You can press n to search forwards.

How to search for the current word

Say you have a file named data.txt as follows displayed using the cat command:
$ cat data.txt
Sample outputs:

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:

Searching in vim
Gif 01: Searching in vim for the current word demo

Search and open file from the CLI

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.

Conclusion

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.

Posted by: Vivek Gite

The author is the creator of nixCraft and a seasoned sysadmin, DevOps engineer, and a trainer for the Linux operating system/Unix shell scripting. Get the latest tutorials on SysAdmin, Linux/Unix and open source topics via RSS/XML feed or weekly email newsletter.

Similar Posts