Need to replace a single word or rename a variable? Looking to make bulk changes across HTML or code? Find out here how to search and replace text in Vim and Neovim — saving yourself a lot of time lost to frustration.
First, Uncommon from Above, Uncommon from Below
The common sea robin looks uncommon from above (with fins as wings that resemble seashells to perfection) and looks uncommon from below — with fins as legs that do not resemble much as all.
Using the legs, prionotus carolinus walks along the seafloor on the search for buried prey. The extensions not only sense vibrations, they also appear to be equipped for smelling telltale signs of chemistry.
Now, if you are looking for something as well, let’s extend Vim’s sensory legs:
How to Search Text in Vim
Time needed: 1 minute.
To search for text in Vim and Neovim:
- Press / (slash) in normal mode.
“Normal” mode: Press Esc, for instance, to exit insert mode in Vim.
- Type the string you want to find.
Special characters: The characters
/, $,^and.have special meaning; to search for any of them, precede the character with\(backspace); see below for the special characters’ function.
Example:/^thewill find “the” and “them”, for instance, at the beginning of a line. - Press Enter.
Here’s why: Only pressing Enter moves the text cursor to the first match and ends the search (even though Vim has probably already highlighted and navigated to the desired section during search).
Next: Press N to jump to the next match.
Previous: Press Shift N to go to the previous match; Vim is typically set up to wrap the search around the beginning and end of the document by default.
Search shortcut: Press * in normal mode to search for the word under the cursor (as a whole word) and jump to the next occurrence.
Common Vim Search Operators
| Search Operator | Function |
|---|---|
^ | Matches the beginning of a line Example: ^the finds “the” only if it appears at the beginning of a line. |
$ | Matches the end of a line Example: that$ finds “that” if it appears at the end of a line. |
. | Matches anything (including whitespace) Example: t.o will find “not only” and “vibrations”. |
*\+ | Matches the previous character (or group) not at all, one time or multiple times Matches the previous character at least once Example: t*o will match “to”, but also “looking”, for instance. |
\s\_s\S | Matches all whitespace Matches whitespace and newline characters Matches any non-whitespace character Example: _\S\+\s\S* will match “_prionotus carolinus_”. |
\( and \)\| | Group search terms or characters Matches what comes before or what comes after the operator Example: \(resemb\|tellta\)le matches both “resemble” and “telltale” (but not “able”). |
\< and \> | Matches only whole words Example: \<the\> finds “the” but not “they”. |
Many more operators let you construct dangerously intricate (and useful) patterns for searching.
Replace with nothing: How to Delete a Line (or Lines) in Vim
How to Replace Text in Vim
To replace text fast in Vim or Neovim:
- Press : (colon) in normal mode to enter command mode.
- Type
%s/to begin the substitution command.
Here’s why: Whilesstands for substitution,%is a shortcut for searching the whole document; without it, only matches on the current line will be replaced (other ranges are possible);/, finally, will separate the search term from its replacement. - Enter the search term to match the word or pattern you want to replace; see above.
- Append
/(slash). - Now enter the string you want to replace matched text.
- Append
/gto finish the search command.
Here’s why:/marks the end of the replacement string, andgwill replace all matches globally; instead ofg, you can use other flags — includingc, which will ask for confirmation before replacing.
Example::s/\C\<common\>/rare/gwill replace all occurrences of “common” (matching case and only the whole word) with “rare” across the document. - Press Enter.
Prompts: If you used the prompting flag, respond for the first (and possibly subsequent matches) with Y to replace the match, L to replace this match and then stop, N to skip this match, A to replace all matches and Esc to skip and quit.
One replacement too far? How to Undo in Vim
How to Search and Replace in Vim: FAQ
Can I make a search match or ignore case?
Yes.
To make a search case sensitive regardless of the default setting in Vim, precede the search term with \C; to ensure case will be ignored, use \c.
Can I use special or Unicode characters?
Yes.
Treat all characters except those reserved in Vim searches (and preceded with \ to match directly) the same.
Can I search across files with Vim?
Yes.
To search for a string or search pattern in multiple files using Vim:
- Press : for command mode in normal mode.
- Type
vimgrep /term/ filesreplacingtermwith the search term andfileswith the list of files you want to search.
Wildcard: You can use *, of course, to match arbitrary characters in file names.
Example: Use:vimgrep /common/ *.mdto find “common” in all *.MD files in the current directory. - Press Enter.
Here’s what happens: If at least one match is found, you will be taken to that match.
Other matches: Use:cnextto jump to the next match (and:cpreviousfor the previous one).
(Tested with Vim 9.1 and Neovim 0.10–0.11; first published October 2024, last updated October 2025)