How to search in VIM/VI Editor? [VI/VIM Search]
Vim search happens in normal mode. Hit Esc first, then type / followed by what you want to find, and press Enter. That's the whole trick. To search upward instead, use ?. To jump between matches, press n for the next one and N for the previous.
Vim, a sophisticated and highly customizable command-line program, is a text editor that enables quicker text editing. Invented by Bram Moolenaar, it comes preinstalled on macOS and most operating systems. Whether you're editing config files or writing code, knowing how to search efficiently is one of the most basic yet productivity-boosting skills you can learn. This guide covers everything beyond the basics — next/previous navigation, exact word matches, case-insensitive search, highlighting, regex patterns, line-range search, and find-and-replace. I'll also clear up the "Ctrl+F in Vim" confusion and explain how Neovim and older vi behave. If you're still warming up to the editor, our common Vim commands guide is a useful sidekick.
Quick Answer: Vim Search Commands
Here's the cheat sheet bookmark it.
| Command | What it does | Example |
/pattern |
Search forward | /error |
?pattern |
Search backward | ?error |
n |
Jump to next match (same direction) | n |
N |
Jump to previous match (opposite direction) | N |
* |
Next occurrence of word under cursor | * |
# |
Previous occurrence of word under cursor | # |
:set hlsearch |
Highlight all matches | :set hlsearch |
:nohlsearch |
Clear current highlighting | :noh |
:%s/old/new/g |
Replace all matches in the file | :%s/foo/bar/g |
How to Search for Text in Vim
Three steps. That's it.
Step 1: Make sure you're in normal mode
If you've been typing text, you're in insert mode. Press Esc to drop back into normal mode. This is the single most common reason a beginner's search "doesn't work" — they're still in insert mode and the slash just becomes a literal character.
Step 2: Type / and your search term
The cursor jumps to the bottom of the screen. Type your pattern:
/errorStep 3: Press Enter and walk through matches
Vim leaps to the first match below the cursor. Press n to go forward, N to go back. When search hits the end of the file, it wraps around to the top by default.
For a practical example, open a file and search for a word named "Hotel" in the forward direction:
$ vim /file1- Press Esc
- Type /Hotel
- Hit n to search forwards for the next occurrence. Press N to search backward.
How to Search Backward in Vim
Quick correction to a myth floating around: you don't type your word in reverse. You just swap the slash for a question mark.
?errorThis searches from your cursor toward the top of the file. After a backward search, n keeps going upward (same direction as the original search) and N flips it.
How to Find an Exact Word in Vim
Searching for cat with /cat will also match category, concatenate, and anything else containing those letters. Often not what you want.
Use \< and \> for whole-word matches
/\<Linux\>Those are word boundary markers. Only standalone "Linux" matches — not "LinuxKernel" or "GNU-Linux-2024."
Use * and # for the word under the cursor
Park your cursor on any word, press *, and Vim finds the next exact occurrence. # goes backward. If you want a looser, partial match instead, use g* and g# — those skip the word boundary check.
Here's a practical example from the command line. Open a file and place the cursor on any word in normal mode, such as "168":
Press * to search for the next occurrence of "168", or # to search backwards.
Case-Sensitive and Case-Insensitive Search in Vim
By default, Vim is case sensitive. /Error won't find error. Here's how to fix that.
One-off case-insensitive search
Append \c to the pattern:
/error\cThat ignores case for this search only. Add \C to force case sensitivity instead. You can also combine this with exact word matching using the Linux command syntax:
/\<word\>\cEditor-wide settings
:set ignorecase
:set smartcase
:set noignorecaseignorecase makes every search case-insensitive. smartcase is the sweet spot — searches are case-insensitive unless you include an uppercase letter, in which case Vim assumes you meant it. Keep both turned on in your .vimrc and you'll rarely think about it again.
How to Highlight Search Results
Highlighting makes a huge difference when you're scanning a long config file.
:set hlsearch
:set incsearch
:nohlsearchhlsearch highlights every match in the buffer. incsearch jumps to matches as you type — incremental search, like a browser's find bar. :nohlsearch (or just :noh) clears the current highlight without disabling the setting itself. The next search will light up again.
Advanced Vim Search Options
Search with regular expressions
Vim's search bar accepts regex out of the box. A few patterns worth knowing:
/^\s*server " lines starting with optional whitespace then "server"
/[0-9]\+ " one or more digits
/error$ " "error" at end of a lineNote the backslash before + — Vim uses "magic" mode by default, which treats some metacharacters literally unless escaped. If that drives you up the wall, prefix patterns with \v for "very magic" mode where regex behaves like most other tools.
Restrict search to a line range
:10,20g/error/p
:10,20s/foo/bar/gThe first lists every line between 10 and 20 containing "error." The second replaces "foo" with "bar" only inside that range.
Search history
Vim keeps track of all the searches you've done so far in the current session. Press / then tap the up arrow to scroll through past searches. Type q/ to open the full search history in a scratch buffer — handy when you've been hopping between patterns and want to reuse one. Before executing, you can also edit the search pattern.
How to Find and Replace in Vim
This is where most beginners conflate searching with replacing. They're different commands. Searching uses /; replacing uses the substitute command :s.
:s/old/new/ " replace first match on current line
:s/old/new/g " replace all matches on current line
:%s/old/new/g " replace all matches in entire file
:%s/old/new/gc " same, but ask for confirmation each timeBreakdown: % means "every line in the file," g means "global within the line" (otherwise only the first match per line gets replaced), and c means "confirm each one." When prompted, press y for yes, n for no, a for all, or q to quit. Honestly, using gc almost always is the safest bet — one accidental global replace will teach you that lesson fast.
Open a File and Jump to a Match from the Terminal
You can skip opening the file and scrolling. Pass the jump as a command-line argument:
vim +10 file.txt # opens file.txt and jumps to line 10
vim +/error file.txt # opens file.txt and jumps to first "error" match
vim +/xyz /d1/f1 # opens /d1/f1 at the first occurrence of "xyz"This is useful in scripts, in tail-style log inspection workflows, or just to save five seconds. You can also use Vim's built-in :e filename +/word command to achieve the same result without leaving the editor — open the file with :e example.txt +/search_word and Vim places the cursor at the specified match. Once you're done editing, check our guide on how to save and exit Vim if you're still getting used to the editor lifecycle.
Vim vs Vi vs Neovim Search
Good news: /, ?, n, N, *, and # work identically in Vim and Neovim. Search-and-replace syntax is the same too. If you're already comfortable in Vim, Neovim's search feels indistinguishable.
Older vi implementations (the original BSD or System V flavours) usually lack hlsearch, incsearch, smartcase, and the \c modifier. The basic forward/backward search still works, but you won't get highlights or incremental matching. On most modern Linux distros, vi is symlinked to Vim anyway, so you usually get the full feature set.
What About Ctrl+F in Vim?
This one trips up everyone arriving from a regular text editor. Ctrl+F in Vim does not open a find dialog. It pages down — one full screen forward. Ctrl+B pages back. To actually find text, use /. There's no find dialog. There never will be. The slash is the dialog.
Common Vim Search Problems and Fixes
- Search isn't finding anything. Press
Escfirst. You're probably still in insert mode. - Highlights won't go away. Run
:noh. To make this a one-key shortcut, map it in your.vimrc. - Exact word search misses obvious matches. Check your boundary syntax:
\<word\>, not<word>. - Case-sensitivity is biting you. Either append
\cto the pattern or run:set ignorecase smartcase. - Regex doesn't behave. Try prefixing with
\vfor very magic mode, or escape special characters with a backslash. - Search wraps unexpectedly. Vim wraps by default. Disable with
:set nowrapscan.
Conclusion
Searching in Vim starts with two keys / for forward and ? for backward and scales all the way to regex-powered find-and-replace across line ranges. We covered the essentials: navigating matches with n and N, locking onto exact words with \< \> or * and #, controlling case sensitivity with \c and smartcase, and highlighting results with hlsearch. We also went beyond the basics into regex patterns, line-range searches, search history, and the all-important substitute command for replacing text.
Vim is user-friendly once the fundamentals click, and the search commands are among the most important tools to master. They accelerate every editing task whether you're hunting down a misbehaving config line, refactoring code, or simply navigating a large file.
The best way to build muscle memory is practice on a real server environment a Linux VPS or VPS server gives you full root access to configure Vim exactly how you want it. For heavier workloads and production environments, a dedicated server delivers the consistent performance you need. Keep a Vim cheat sheet nearby while the muscle memory builds, and you'll be searching at the speed of thought before long.
People Are Also Reading: