How To Search In Vim / Vi - Linuxize
Có thể bạn quan tâm
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 basics can be extremly useful when your preferred editor isn’t available.
This guide walks you through the basic and advanced Vim search commands, making it easy for beginners to follow along.
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:
- Press / to search forward or ? to search backward.
- Type the search pattern.
- Press Enter to run the search.

Vim will highlight the first match it finds.
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 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:
- Press / or ?.
- Use the arrow up/down keys to browse previous searches.
- Press Enter to run the search. You can edit the pattern before performing the operation.
Case sensitivity #
By default, Vim searches are case sensitive; searching for “GNU” will not match “Gnu”.
Override Case Sensitivity Per Search #
To ignore case for one search append ,\c after the search pattern:
/Linux\cTo force case sensitivity, append uppercase \C after the pattern:
/Linux\COverride Case Sensitivity on 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.
Permanently Override Case Sensitivity #
To set the ignore case to be a default option , add :set ignorecase to your ~/.vimrc file.
~/.vimrc:set ignorecaseConclusion #
Searching is one of the most essential skills in Vim.
To search in Vim/Vi:
- Type / or ? to start a search.
- Use n and N to navigate results.
- Use \< and \> to match whole words.
- Use * and # to search the word under the cursor.
- Add : set ignorecase to ignore case sensitivity.
Mastering Vim search will save you time and make your work more efficient.
Feel free to leave a comment if you have questions!
terminal vimTừ khóa » Vi Find Backwards
-
How To Find A Word In Vim Or Vi Text Editor - NixCraft
-
Vim: Searching Upward/downward In File From Specific Point/position
-
How Can I Switch From Forward To Backward Search In Vim?
-
How To Search A Character Backwards From The End Of Line In Vim
-
Searching And Replacing With Vi
-
How To Search To Find A Word In Vim Or Vi Text Editor - PhoenixNAP
-
How To Search In Vim - Linux Hint
-
How To Search In Vim/Vi? - LinuxTect
-
Vim Tips And Tricks: Searching
-
Searching - Vim Tips Wiki - Fandom
-
How To Search In VI Editor - Fedingo
-
[PDF] Vi Editor “Cheat Sheet”
-
Vi Help - Searching For Text - UCSB Physics
-
Unix - How To Read Backward From The End Of File In Less Or More?