Vim Tips Wiki
Explore
Main Page
All Pages
Community
Interactive Maps
Community portal
To do
FANDOM
Fan Central
BETA
Games
Anime
Movies
TV
Video
Wikis
Explore Wikis
Community Central
Start a Wiki
Don't have an account?
Register
Sign In
Sign In
Register
Vim Tips Wiki
1,649
pages
Explore
Main Page
All Pages
Community
Interactive Maps
Community portal
To do
Editing
Searching
Back to page
Edit
Edit source
View history
Talk (0)
Edit Page
Searching
We recommend that you
log in
before editing. This will allow other users to leave you a message about your edit, and will let you track edits via your
Watchlist
.
Creating an account
is quick and free.
The edit appears to have already been undone.
Anti-spam check. Do
not
fill this in!
{{TipImported |id=1 |previous= |next=2 |created=2001 |complexity=basic |author= |version=6.0 |rating=5333/1765 |category1=Getting started |category2=Searching }} This tip shows how to search using Vim, including use of <code>*</code> (the ''super star'') to search for the current word. Search options are described, and the [[#See also|see also]] section links to other useful searching tips. ==Basic searching== In normal mode you can search forwards by pressing <code>/</code> (or <code><kDivide></code>) then typing your search pattern. Press Esc to cancel or press Enter to perform the search. Then press <code>n</code> to search forwards for the next occurrence, or <code>N</code> to search backwards. Type <code>ggn</code> to jump to the first match, or <code>GN</code> to jump to the last. Note: <code>ggn</code> skips first match if it is at row 1 column 1. Type <code>Gn</code> to jump to the real first match. For this, <code>'wrapscan'</code> must be on (default). Search backwards by pressing <code>?</code> then typing your search pattern. Pressing <code>n</code> searches in the same direction (backwards), while <code>N</code> searches in the opposite direction (forwards). ==Searching for the current word== In normal mode, move the cursor to any word. Press <code>*</code> to search forwards for the next occurrence of that word, or press <code>#</code> to search backwards. Using <code>*</code> (also <kMultiply>, <S-LeftMouse>) or <code>#</code> (also <S-RightMouse>) searches for the exact word at the cursor (searching for ''rain'' would not find ''rainbow''). Use <code>g*</code> or <code>g#</code> if you don't want to search for the exact word. ===Using the mouse=== With appropriate settings, you can search for an exact word using the mouse: Shift-LeftClick a word to search forwards, or Shift-RightClick to search backwards. This needs a GUI version of Vim (gvim), or a console Vim that accepts a mouse. You may need the following line in your [[vimrc]] to enable mouse searches: <pre> :set mousemodel=extend </pre> In gvim, click the Edit menu, then Global Settings, then the "tear off" bar at the top. That will show a floating Global Settings menu with useful ''Toggle Pattern Highlight'' and ''Toggle Ignore-case'' commands. ==More searching== Vim maintains a [[VimTip45|search history]]. Type <code>/</code> or <code>?</code> and use the arrow up/down keys to recall previous search patterns. You can edit a pattern, and press Enter to search for something different. Suppose the cursor is on a word, and you want to search for a similar word. Press <code>/</code> then Ctrl-r then Ctrl-w to copy the current word to the command line. You can now edit the search pattern and press Enter. Use Ctrl-r Ctrl-w for a <cword>, or Ctrl-r Ctrl-a for a <cWORD>. After searching, press Ctrl-o to [[VimTip10|jump back]] to your previous position (then Ctrl-i will jump forwards). After searching, an empty search pattern will repeat the last search. This works with <code>/</code>, <code>:s</code> and <code>:g</code>. So, after searching for a word, use <code>:%s//new/g</code> to change all occurrences to 'new', or <code>:g/</code> to list all lines containing the word. See [[VimTip1501|substitute last search]]. You can enter a count before a search. For example <code>3/pattern</code> will search for the third occurrence of ''pattern'', and <code>3*</code> will search for the third occurrence of the current word. You can highlight all search matches (and quickly turn highlighting off), and you can use a mapping to highlight all occurrences of the current word without moving (see [[VimTip14|highlight matches]]). A search can include an offset to position the cursor. For example, searching with <code>/green</code> positions the cursor at the beginning of the next "green", while <code>/green/e</code> positions the cursor at the end of the next "green". {{help|search-offset}} A search pattern can include characters with codes specified in decimal or hex. For example, searching with <code>/\%d65</code> or with <code>/\%x41</code> both find '<code>A</code>' (character decimal 65 = hex 41), and searching with <code>/\%d8211</code> or with <code>/\%u2013</code> both find '<code>β</code>' (a Unicode en dash). {{help|/\%d}} ==Case sensitivity== By default, searching is case sensitive (searching for "the" will not find "The"). With the following in your vimrc (or entered via a [[VimTip920|toggle mapping]]), searching is not case sensitive: <pre> :set ignorecase </pre> Now the command <code>/the</code> will find "the" or "The" or "THE" etc. You can use <code>\c</code> to force a pattern to be case insensitive, or <code>\C</code> to force a pattern to be case sensitive. For example, the search <code>/the\c</code> is always case insensitive, and <code>/the\C</code> is always case sensitive, regardless of the <code>'ignorecase'</code> option. If <code>'ignorecase'</code> is on, you may also want: <pre> :set smartcase </pre> When <code>'ignorecase'</code> and <code>'smartcase'</code> are both on, if a pattern contains an uppercase letter, it is case sensitive, otherwise, it is not. For example, <code>/The</code> would find only "The", while <code>/the</code> would find "the" or "The" etc. The <code>'smartcase'</code> option only applies to search patterns that you type; it does not apply to <code>*</code> or <code>#</code> or <code>gd</code>. If you press <code>*</code> to search for a word, you can make <code>'smartcase'</code> apply by pressing <code>/</code> then up arrow then Enter (to repeat the search from history). When programming, there is generally no reason to want <code>'smartcase'</code> to apply when you press <code>*</code>. For other situations, use: <pre> :nnoremap * /\<<C-R>=expand('<cword>')<CR>\><CR> :nnoremap # ?\<<C-R>=expand('<cword>')<CR>\><CR> </pre> With these mappings, if <code>'smartcase'</code> is on and you press <code>*</code> while on the word "The", you will only find "The" (case sensitive), but if you press <code>*</code> while on the word "the", the search will not be case sensitive. The mapping for <code>*</code> uses <code>/</code> to start a search; the pattern begins with <code>\<</code> and ends with <code>\></code> so only whole words are found; <code><C-R>=</code> inserts the expression register to evaluate <code>expand('<cword>')</code> which inserts the current word (similar to Ctrl-R Ctrl-W but avoiding an error when used on a blank line). ==Show the next match while entering a search== To move the cursor to the matched string, while typing the search pattern, set the following option in your [[vimrc]]: <pre> :set incsearch </pre> Complete the search by pressing Enter, or cancel by pressing Esc. When typing the search pattern, press Ctrl-L ({{help|c_CTRL-L}}) to insert the next character from the match or press Ctrl-R Ctrl-W ({{help|c_CTRL-R_CTRL-F}}) to complete the current matching word. ==Other search options== By default, the <code>'wrapscan'</code> option is on, which means that when "search next" reaches end of file, it wraps around to the beginning, and when "search previous" reaches the beginning, it wraps around to the end. These examples show how to set <code>'wrapscan'</code> (abbreviated as <code>'ws'</code>): <pre> :set nowrapscan " do not wrap around :set wrapscan " wrap around :set wrapscan! " toggle wrap around on/off :set ws! ws? " toggle and show value </pre> By default, search hits may occur in lines at the top or bottom of the window. The [[Make search results appear in the middle of the screen|<code>'scrolloff'</code> option]] controls whether context lines will be visible above and below the line containing the search hit. If your text is folded, you probably want folds to automatically open to reveal search hits. To achieve that, the <code>'foldopen'</code> option should include "search" (check by entering :set fdo?). {{help|'foldopen'}} Conversely, this option can be used to [[Search only in unfolded text]]. ==See also== ;Searching *[[VimTip14|Highlighting search matches]] *[[VimTip171|Search for visually selected text]] *[[VimTip188|Search patterns]] regex tutorial with useful searches *[[VimTip798|Search for current word in new window]] *[[VimTip528|Make search results appear in the middle of the screen]] *[[VimTip31|Search and replace]] using <code>:s</code> to substitute text *Browse the [[:Category:Searching|searching category]] for more. ;Searching in multiple files: *[[VimTip1543|Find in files within Vim]] ;Finding a file from its name: *[[VimTip1146|Project browsing using find]] *[[VimTip1234|Find files in subdirectories]] ==References== *{{help|*}} *{{help|tag=%3CS-LeftMouse%3E|label=<S-LeftMouse>}} *{{help|:<cword>|:<cword>}} *{{help|jump-motions}} *{{help|'iskeyword'}} controls which characters <code>*</code> considers make a word ==Comments== On UK keyboards, <Shift-3> produces β€ but works just like <code>#</code> for searching backwards.
Summary:
Please note that all contributions to the Vim Tips Wiki are considered to be released under the CC-BY-SA
Cancel
Editing help
(opens in new window)
Templates used on this page:
Template:Help
(
view source
)
Template:Navigation
(
view source
)
Template:TipImported
(
view source
)
Follow on IG
TikTok
Join Fan Lab