Smart Systems for Home

Plain how-to guides for smart home, security and home Wi-Fi

How to Search Text in Vim: A Practical Guide

· 6 min read · Home Tech

In this article
  1. What You'll Need
  2. Step-by-Step
  3. Common Mistakes
  4. Tips
  5. Troubleshooting
  6. Frequently Asked Questions

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.txt and add some sample text.
  • Basic familiarity with Vim's modes (Normal mode, Insert mode, Command-line mode). If you're new, just remember: press Esc to return to Normal mode.

Step-by-Step

  1. Open a file in Vim. Type vim filename.txt in your terminal and press Enter. You'll be in Normal mode.
  2. 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 press Enter. Vim will jump to the first occurrence after your cursor.
  3. Perform a basic backward search. Press ? (question mark) instead of /. Type your search term and press Enter. Vim will search backward from your cursor position.
  4. Jump to the next or previous match. After a search, press n to go to the next match in the same direction, or N to go to the previous match (reverse direction). This works for both forward and backward searches.
  5. Highlight all matches. By default, Vim highlights all matches. If you don't see highlights, ensure hlsearch is enabled. Type :set hlsearch and press Enter. To turn off highlighting temporarily, type :nohlsearch (or just :noh) and press Enter.
  6. 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.
  7. Search case-insensitively. Append \c to your search term to ignore case for that search. For example, /error\c will match "Error", "ERROR", and "error". To make all searches case-insensitive by default, use :set ignorecase and optionally :set smartcase (which makes searches case-sensitive if you type any uppercase letter).
  8. 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).
  9. 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).
  10. Repeat the last search. Press // and Enter to repeat the last forward search, or ?? and Enter for the last backward search. Alternatively, just press n or N to move to the next/previous occurrence without re-typing.
  11. Search and replace (bonus). To replace all occurrences of a word, use :%s/old/new/g. The % means the whole file, s is substitute, and g replaces all occurrences on each line. Add c at 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 n always goes forward. n goes in the same direction as the original search. If you searched backward with ?, n goes backward.

Tips

  • Use :set incsearch to 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* and g# to search for a partial word. If you want to find the word under the cursor even as part of a larger word, use g* (forward) and g# (backward).
  • Set smartcase for 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 ignorecase off 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, use gg to go to the top and G to go to the bottom.
  • Highlighting won't turn off. If :nohlsearch doesn't work, you might have hlsearch off and the highlights are from another setting. Try :set nohlsearch to disable it permanently, or :set hlsearch to enable it and then :nohlsearch to 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=1 to switch to a faster (but less powerful) engine.
  • You accidentally replaced something with :s. If you didn't use c for confirmation, you can undo with u. If you've made multiple changes, you can undo several times or use :earlier 1m to 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".