How to Search Text in Vim: A Practical Guide
In this article
Searching text is one of the most frequent tasks in Vim, and doing it efficiently can save you a lot of time. This guide covers the essential search commands, from basic forward and backward searches to using regular expressions and handling special characters. You'll learn how to search, navigate results, and avoid common pitfalls. No prior Vim experience is needed, but you should have Vim installed and know how to open a file.
What You'll Need
- A terminal or command prompt with Vim installed (most Linux and macOS systems have it; Windows users can use WSL or install Vim).
- A text file to practice on. You can create one with
vim practice.txtand add some sample text. - Basic familiarity with Vim's modes (Normal mode, Insert mode, Command-line mode). If you're new, just remember: press
Escto return to Normal mode.
Step-by-Step
- Open a file in Vim. Type
vim filename.txtin your terminal and press Enter. You'll be in Normal mode. - Perform a basic forward search. Press
/(forward slash). The cursor will move to the bottom-left corner, and a/will appear. Type the text you want to find (for example,error) and pressEnter. Vim will jump to the first occurrence after your cursor. - Perform a basic backward search. Press
?(question mark) instead of/. Type your search term and pressEnter. Vim will search backward from your cursor position. - Jump to the next or previous match. After a search, press
nto go to the next match in the same direction, orNto go to the previous match (reverse direction). This works for both forward and backward searches. - Highlight all matches. By default, Vim highlights all matches. If you don't see highlights, ensure
hlsearchis enabled. Type:set hlsearchand press Enter. To turn off highlighting temporarily, type:nohlsearch(or just:noh) and press Enter. - Search for a whole word. To find only whole words (e.g., "cat" but not "catalog"), use
\<and\>around the word. Type/\<cat\>and press Enter. This matches the word boundaries. - Search case-insensitively. Append
\cto your search term to ignore case for that search. For example,/error\cwill match "Error", "ERROR", and "error". To make all searches case-insensitive by default, use:set ignorecaseand optionally:set smartcase(which makes searches case-sensitive if you type any uppercase letter). - Use regular expressions for advanced searches. Vim's search supports regex. . To find numbers, use /\d . To find any of several words, use /\ (the \| means OR).
- Search for special characters literally. If you need to find a character that has a special meaning in regex (like
.,*,[,],^,$,\,/), escape it with a backslash. For example, to find a literal dot, type/\\.(the first backslash escapes the second, and then the dot is literal). - Repeat the last search. Press
//and Enter to repeat the last forward search, or??and Enter for the last backward search. Alternatively, just pressnorNto move to the next/previous occurrence without re-typing. - Search and replace (bonus). To replace all occurrences of a word, use
:%s/old/new/g. The%means the whole file,sis substitute, andgreplaces all occurrences on each line. Addcat the end (:%s/old/new/gc) to ask for confirmation before each replacement.
Common Mistakes
- Forgetting to press Enter after typing the search term. The search only executes after you press Enter.
- Using
/when you meant?. This searches the wrong direction, and you might miss matches that are behind you. - Not escaping special characters. Searching for
.without a backslash matches any character, so you'll get many false positives. - Searching case-sensitively when you don't want to. If you don't set
ignorecase, Vim is case-sensitive by default. This can cause you to miss matches. - Thinking
nalways goes forward.ngoes in the same direction as the original search. If you searched backward with?,ngoes backward.
Tips
- Use
:set incsearchto see matches as you type. This shows the first match while you're still typing, making it easier to correct typos. - Use
*and#to search for the word under the cursor. Press*to search forward for the exact word under the cursor, and#to search backward. This is a huge time-saver. - Use
g*andg#to search for a partial word. If you want to find the word under the cursor even as part of a larger word, useg*(forward) andg#(backward). - Set
smartcasefor the best of both worlds. With:set ignorecase smartcase, searches are case-insensitive unless you type an uppercase letter, in which case they become case-sensitive. - Use the command-line history. After pressing
/, you can press the up arrow to recall previous search terms. This is handy for repeating complex searches.
Troubleshooting
- Search doesn't find anything even though the text exists. Check if you have
ignorecaseoff and the case doesn't match. Also, if you used special characters, they might be interpreted as regex. Try escaping them or use\V(very nomagic) at the start of the search to treat everything literally (e.g.,/\Vmy.text). - Search wraps around the file and you didn't notice. Vim wraps by default. If you don't want wrapping, use
:set nowrapscan. If you're confused about where you are, useggto go to the top andGto go to the bottom. - Highlighting won't turn off. If
:nohlsearchdoesn't work, you might havehlsearchoff and the highlights are from another setting. Try:set nohlsearchto disable it permanently, or:set hlsearchto enable it and then:nohlsearchto clear. - Search is slow on a large file. If you're using a complex regex, it can be slow. Try simplifying the pattern or use
:set regexpengine=1to switch to a faster (but less powerful) engine. - You accidentally replaced something with
:s. If you didn't usecfor confirmation, you can undo withu. If you've made multiple changes, you can undo several times or use:earlier 1mto revert to the state one minute ago.
Frequently Asked Questions
How do I search for a word and replace it in Vim?
Use the substitute command: :%s/old/new/g. The % applies to the whole file, g replaces all occurrences on each line. Add c to confirm each change.
How do I search backwards in Vim?
Press ? instead of /. Then type your search term and press Enter. Use n to go to the next match in the same direction (backward) and N to go forward.
How do I make Vim search case-insensitive?
Type :set ignorecase to make all searches case-insensitive. For a single search, append \c to the pattern (e.g., /error\c). Use :set smartcase to automatically be case-sensitive when you type an uppercase letter.
How do I search for a literal dot or other special character?
Escape the character with a backslash. For example, to find a literal dot, type /\\.. Alternatively, use \V at the start of the pattern to disable regex interpretation, so /\V. finds a literal dot.
How do I search for a whole word only?
Use word boundary markers: /\<word\>. This matches the word exactly, not as part of another word.
How do I search for multiple words at once?
Use the OR operator \|. For example, /error\|warning matches either "error" or "warning".