vim tutorial: vim search

Vim search can help us quickly locate the location of the content when looking for or modifying the content of a file.

This tutorial shows how to perform search operations in Vim.

There are two ways to search in vim:

  • Search location by word
  • Position location by line number

Search location by word

  1. Open the terminal

2. Use vim to open the file and enter vim normal mode

3. In the vim normal mode window, type / or ? , Then type the search pattern/word and press enter to run the search

  • / Search forward
  • ? Search backward

4. After finding a word in vim, you can press the n key to go directly back to the next word. Press the N key to move directly in the opposite direction.

Position location by line number

  1. Open the terminal

2. Use vim to open the file and enter vim normal mode

3. In the vim normal mode window, type : , then type the line number to jump to and press Enter

Vim search practical skills

Search for Whole Word

To search for a whole word, start the search by pressing / or ?, type \< to mark the beginning of a word, enter the search pattern, type \> to mark the end of a word, and hit Enter to perform the search.

For example, search for the whole word “root”:

:/\<root\>

Vim search history

Vim will keep track of all search operations you perform in the current session.

To browse the search history, press / or ? , And use the up/down arrow keys to find the previous search operation.

To run the search history command, just press Enter.

Search the Current Word

Vim can search for the word where the cursor is currently located. Move the cursor to the word and press * (asterisk) to search forward or # (hash) to search backward to search the entire word.

To find the next match, press * or # again.

When using * or # to search for a word, the complete search command will be displayed on the left side of the last line in vim command line

Vim search ignores/case sensitive

To ignore case, type in the Vim command line :set ignorecase or :set ic .

To distinguish between uppercase and lowercase, type in the Vim command line :set noignorecase or :set noic .

Vim set display line number

To display the line number, type in the Vim command line :set number or :se nu .

Vim search and open file

Vim text editor supports running with the following syntax:

vim +LineNumber fileName
vim +/searchWord fileName

Open the file and go to the line where the word “root” appears:

➜  ~ vim +/root /etc/passwd

Open the file and go to line 100:

➜  ~ vim +100 /etc/passwd

Add a Comment

Your email address will not be published.