How to Search in Vim / Vi

By 

Updated on

6 min read

Vim Search

Vim and its predecessor, Vi, are preinstalled on macOS and most Linux distributions. When working on remote servers or minimal systems, Vim is often the only editor available.

Searching text is one of the most common tasks when editing files, and mastering Vim’s search commands can be extremely useful when your preferred editor is not available.

This guide explains how to search in Vim / Vi using forward and backward search, whole-word matching, search history, and case-sensitive or case-insensitive patterns.

Vim Modes

You must be in normal mode to search in Vim. This is the default mode when you open the editor. If you are in another mode (Insert or Visual), press Esc to return to Normal mode.

Basic Search in Vim

Vim allows you to search text using two main commands:

  • / (forward slash) - search forward.
  • ? (question mark) - search backward.

To perform a search:

  1. Press / to search forward or ? to search backward.
  2. Type the search pattern.
  3. Press Enter to run the search.
Vim Search

Vim will highlight the first match it finds.

Info
Searches match patterns as substrings, not whole words. Searching for “gnu” will highlight occurrences in larger words such as “cygnus” or “magnum”.

To navigate search results:

  • Press lowercase n to search for the next occurrence.
  • Press uppercase N to search in the opposite direction.

Searching for Whole Words

To match whole words only, add word boundaries:

  • \< to mark the beginning of a word.
  • \> to mark the end of a word.

For example, to search for “gnu” only as a standalone word, you would use /\<gnu\>:

Search for Whole Word

Search the Current Word

Vim provides quick shortcuts to search for the current word (the word under your cursor).

Place the cursor on a word and:

  • Press * (asterisk) to search forward for whole-word matches.
  • Press # (hash) to search backward.

Press * or # again to find the next match.

Using Vim Search History

Vim keeps track of all the search operations performed during the current session. To access the search history:

  1. Press / or ?.
  2. Use the arrow up/down keys to browse previous searches.
  3. Press Enter to run the search. You can edit the pattern before performing the operation.

Search Highlighting

By default, Vim highlights all matches when you perform a search. You can control this behaviour with a few options.

To enable search highlighting for the current session, run:

vi
:set hlsearch

To clear the current highlights without disabling the setting, use the :nohlsearch command (or its abbreviation :noh):

vi
:noh

To turn off search highlighting entirely for the current session:

vi
:set nohlsearch

To make either setting permanent, add it to your ~/.vimrc file:

~/.vimrcvim
set hlsearch

Case Sensitivity

By default, Vim searches are case sensitive; searching for “GNU” will not match “Gnu”.

To ignore case for one search, append \c after the search pattern:

vi
/Linux\c

To force case sensitivity, append uppercase \C after the pattern:

vi
/Linux\C

Override Case Sensitivity for the Current Session

To make searches case-insensitive for the current session, type :set ignorecase or :set ic in the Vim command line.

To return to case-matching mode, type :set noignorecase or :set noic.

Smartcase

The smartcase option is a useful complement to ignorecase. When both are enabled, Vim ignores case if the pattern is all lowercase, but switches to case-sensitive matching as soon as you type any uppercase letter.

vi
:set ignorecase
:set smartcase

With this combination, /linux matches “Linux”, “LINUX”, and “linux”, while /Linux matches only “Linux”.

Permanently Override Case Sensitivity

To set ignore case and smartcase as default options, add them to your ~/.vimrc file:

~/.vimrcvim
set ignorecase
set smartcase

Quick Reference

CommandDescription
/patternSearch forward for pattern
?patternSearch backward for pattern
nJump to the next match
NJump to the previous match
*Search forward for the word under cursor
#Search backward for the word under cursor
/\<word\>Search for whole word only
/pattern\cCase-insensitive search (one-off)
/pattern\CCase-sensitive search (one-off)
:set ignorecaseIgnore case for all searches
:set smartcaseCase-insensitive unless pattern has uppercase
:set hlsearchHighlight all matches
:nohClear current search highlights

For a printable quick reference, see the Vim cheatsheet .

Troubleshooting

Search highlights are not clearing Type :noh in Normal mode to clear highlights from the current search without disabling hlsearch. If you want highlights off permanently, add set nohlsearch to ~/.vimrc.

Search is matching parts of words unexpectedly By default, Vim searches for substrings. Use word boundaries to restrict matching: /\<word\> will only match the exact word, not occurrences inside larger words.

Case-sensitive search is not working as expected Check whether ignorecase is set with :set ignorecase?. If it is on and you need a case-sensitive search, either append \C to the pattern or run :set noignorecase to disable it for the session.

Search wraps around the file unexpectedly Vim wraps around the end of the file by default. To disable wrap-around, run :set nowrapscan.

FAQ

How do I search for a word in Vim? Press / in Normal mode, type the word, and press Enter. Use n to jump to the next match and N to go back. To search for the word under the cursor directly, press *.

How do I search for the next match in Vim? Press n to move to the next match in the same direction as the current search, and N to move in the opposite direction.

How do I do a case-insensitive search in Vim? Append \c to the search pattern (for example, /linux\c) to ignore case for that one search. To ignore case for all searches in the session, run :set ignorecase. Add it to ~/.vimrc to make it permanent.

What is the difference between / and ? in Vim? / searches forward from the cursor position toward the end of the file. ? searches backward toward the beginning. Both wrap around the file by default.

How do I turn off search highlighting in Vim? Run :noh to clear the current highlights. To disable highlighting entirely, run :set nohlsearch or add set nohlsearch to your ~/.vimrc.

Can I search using regular expressions in Vim? Yes. Vim supports regular expressions in search patterns. For example, /^foo matches lines starting with “foo” and /foo.*bar matches “foo” followed by anything and then “bar”. For a full reference, see the Find and Replace in Vim guide which covers regex patterns in detail.

Conclusion

Vim’s search commands are fast and flexible once you know the key strokes. Use / and ? for forward and backward search, n and N to navigate matches, and * to instantly search the word under the cursor. For more control over your editing workflow, see the Find and Replace in Vim guide.

Tags

Linuxize Weekly Newsletter

A quick weekly roundup of new tutorials, news, and tips.

About the authors

Dejan Panovski

Dejan Panovski

Dejan Panovski is the founder of Linuxize, an RHCSA-certified Linux system administrator and DevOps engineer based in Skopje, Macedonia. Author of 800+ Linux tutorials with 20+ years of experience turning complex Linux tasks into clear, reliable guides.

View author page