Skip to content

Vim - Searching

Searching is a fundamental action in any text editor, and Vim provides powerful tools to perform searches efficiently. This chapter will cover various commands and settings to master searching in Vim.

In this chapter, we will discuss the following items:

  • Search-related settings
  • Searching in the current file
  • Searching in multiple files
  • Searching in Vim’s help files

Vim offers several settings to customize search behavior. You can set these temporarily for the current session or add them to your ~/.vimrc (or ~/_vimrc on Windows, ~/.config/nvim/init.vim for Neovim) file for persistence.

To enable incremental search (showing matches as you type):

:set incsearch

To highlight all search matches:

:set hlsearch

For example, if hlsearch is enabled and you search for “fox”, all occurrences of “fox” in the current buffer will be highlighted. To temporarily clear the highlighting, use :nohlsearch (or :noh). The highlighting will reappear with the next search.

To make searches case-insensitive:

:set ignorecase

To intelligently override ignorecase if your search pattern contains an uppercase letter (e.g., searching for “Fox” will be case-sensitive, while “fox” will be case-insensitive if ignorecase is also set):

:set smartcase

To disable these settings, prepend “no” to the setting name:

:set noincsearch
:set nohlsearch
:set noignorecase
:set nosmartcase

Use the following commands to perform a search in the forward direction (towards the end of the file):

CommandDescription
/Search for in the forward direction. Press Enter to start the search.
nFind the next occurrence of the last search pattern in the same direction.
NFind the previous occurrence of the last search pattern, reversing the direction.
//Repeat the previous forward search. Useful if you’ve used ? previously.

Tip: Search patterns can be regular expressions. For help, see :help pattern.

Use the following commands to perform a search in the backward direction (towards the beginning of the file):

CommandDescription
?Search for in the backward direction. Press Enter to start the search.
nFind the next occurrence of the last search pattern, which means continuing backward for a ? search.
NFind the previous occurrence of the last search pattern, reversing direction (so, forward for a ? search).
??Repeat the previous backward search. Useful if you’ve used / previously.

Place the cursor on any word and use these commands for quick searching:

CommandDescription
*Search forward for the whole word under the cursor.
#Search backward for the whole word under the cursor.
g*Search forward for the current word under the cursor (matches parts of words too).
g#Search backward for the current word under the cursor (matches parts of words too).

Vim’s built-in :vimgrep (or :vg) command allows you to search for a pattern in multiple files. This populates the quickfix list with matches.

For instance, to search for the string “MyFunction” in all *.js files in the current directory and its subdirectories:

:vimgrep /MyFunction/ ./**/*.js

Note the use of /pattern/ for vimgrep. The . refers to the current directory, and ** is a glob that searches recursively.

After running :vimgrep, the quickfix list will contain all matches. Use these commands to navigate the list:

CommandDescription
:copenOpen the quickfix window to display all matches.
:ccloseClose the quickfix window.
:cnext or :cnGo to the next match in the quickfix list.
:cprevious or :cp or :cNGo to the previous match in the quickfix list.
:cfirstGo to the first match.
:clastGo to the last match.

Alternatively, many users integrate external tools like grep, ripgrep (rg), or The Silver Searcher (ag) for faster multi-file searches. This can be configured using the grepprg option and then using the :grep command. For example, with ripgrep:

:set grepprg=rg\ --vimgrep\ --no-heading
:grep MyFunction ./**/*.js

The navigation commands (:copen, :cn, etc.) work the same way with :grep.

Vim has extensive built-in help. To search for a keyword across all installed help files, use :helpgrep (or :helpg). This also populates a quickfix list (or a location list if used from a help window).

For example, to find all help topics related to “quickfix”:

:helpgrep quickfix

Then use quickfix navigation commands like :copen, :cn, :cp to browse the results. Each result will take you to the relevant section in the help files. For more, see :help :helpgrep.