How to search in vim

By Patricia Sauer ·

Navigating through files in Vim can be tricky without the right tools. Fortunately, Vim offers powerful search capabilities to make file navigation fast and efficient. In this article, we’ll explore how to search in Vim and navigate through results.

To search in Vim, use the / command followed by the word or pattern you’re looking for. For example:

/word

This searches forward in the file for the first occurrence of the word “word.”

To search backward, use ? instead of /:

?word

Let’s say you have the following file content:

Hello, this is a Vim tutorial.

If you want to find the word “Vim”:

  • Type /Vim and press Enter. This moves the cursor to the word “Vim.”
  • Press n to move to the next occurrence of the word.
  • Press N to move to the previous occurrence.

After performing a search, you can navigate through the results:

  • n: Jump to the next match in the direction of the search.
  • N: Jump to the previous match in the opposite direction.

For example, if you searched forward using /Vim, pressing n will take you to the next occurrence, and N will take you to the previous one.

Case Sensitivity

By default, Vim searches are case-sensitive. If you want to make your search case-insensitive, use the following command:

:set ignorecase

Now, searching for /vim will match “vim,” “Vim,” or “VIM.”

If you want to make the search case-sensitive again, use:

:set noignorecase

Alternatively, use :set smartcase to make searches case-insensitive only when the search pattern is all lowercase. When the search pattern contains uppercase letters, it becomes case-sensitive. This is a great way to keep your searches flexible!

Searching for the Current Word

To quickly search for the word under the cursor:

  • Use * to search forward.
  • Use # to search backward.

For example:

  • Place the cursor on the word “Vim.”
  • Type * to find the next occurrence of “Vim.”
  • Type # to find the previous occurrence.

This is especially useful for navigating through repetitive terms like variable names in code.

Highlighting Search Results

By default, Vim highlights search results. If it’s not enabled, you can turn it on with:

:set hlsearch

This highlights all matches in the file. To remove the highlights after searching, type:

:nohlsearch

You can also toggle this behavior off permanently using:

:set nohlsearch

Bonus: Using Regular Expressions

Vim supports regular expressions for advanced search patterns. For example:

  • /\d: Finds the first digit in the file.
  • /^Hello: Finds lines that start with “Hello.”
  • /V..m: Matches “Vim” or other words starting with “V” and ending with “m.”

These patterns make it easy to locate complex text sequences.

Recap

In this article, we learned:

  • How to search forward and backward using / and ?.
  • How to navigate through search results using n and N.
  • How to make searches case-insensitive with :set ignorecase and :set smartcase.
  • How to search for the current word under the cursor using * and #.
  • How to highlight and clear search results with :set hlsearch and :nohlsearch.
  • How to use regular expressions for advanced searches.

Here’s a handy summary of the commands:

CommandResult
/patternSearch forward for “pattern”
?patternSearch backward for “pattern”
nGo to the next match
NGo to the previous match
:set ignorecaseMake searches case-insensitive
:set smartcaseCase-insensitive unless pattern contains uppercase letters
*Search forward for the word under the cursor
#Search backward for the word under the cursor
:set hlsearchHighlight all search matches
:nohlsearchClear search highlights
/\dSearch for digits
/^patternSearch for lines starting with “pattern”

With these search techniques, you can navigate through files in Vim like a pro! Try them out and see how much faster your workflow becomes.

More about vi & the command line

All articles →