Vim - Searching
Vim - Searching
Section titled “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
Search-Related Settings
Section titled “Search-Related Settings”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 incsearchTo highlight all search matches:
:set hlsearchFor 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 ignorecaseTo 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 smartcaseTo disable these settings, prepend “no” to the setting name:
:set noincsearch:set nohlsearch:set noignorecase:set nosmartcaseSearch in Current File
Section titled “Search in Current File”Search in Forward Direction
Section titled “Search in Forward Direction”Use the following commands to perform a search in the forward direction (towards the end of the file):
| Command | Description |
|---|---|
| / | Search for |
| n | Find the next occurrence of the last search pattern in the same direction. |
| N | Find 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.
Search in Backward Direction
Section titled “Search in Backward Direction”Use the following commands to perform a search in the backward direction (towards the beginning of the file):
| Command | Description |
|---|---|
| ? | Search for |
| n | Find the next occurrence of the last search pattern, which means continuing backward for a ? search. |
| N | Find 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. |
Search for Word Under Cursor
Section titled “Search for Word Under Cursor”Place the cursor on any word and use these commands for quick searching:
| Command | Description |
|---|---|
| * | 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). |
Search in Multiple Files
Section titled “Search in Multiple Files”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/ ./**/*.jsNote 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:
| Command | Description |
|---|---|
| :copen | Open the quickfix window to display all matches. |
| :cclose | Close the quickfix window. |
| :cnext or :cn | Go to the next match in the quickfix list. |
| :cprevious or :cp or :cN | Go to the previous match in the quickfix list. |
| :cfirst | Go to the first match. |
| :clast | Go 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 ./**/*.jsThe navigation commands (:copen, :cn, etc.) work the same way with :grep.
Search in Help Files
Section titled “Search in Help Files”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 quickfixThen 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.