Vim: Search
Searching allows you to quickly navigate around your file. It’s easy to learn the basics and be productive, and there is depth and nuance when you’re ready to go exploring.
The search command takes a [count]
which, if omitted, defaults to 1. Many Vim
commands take [count]
s and you can learn more here.
Search commands:
/{pattern}[/]<CR>
search forward for the[count]
‘th match of{pattern}
?{pattern}[?]<CR>
ditto, only backwards/[/]<CR>
search forward using previous{pattern}
?[?]<CR>
ditto, only backwardsn
jump to the next match in the search directionN
jump to the previous match (opposite search direction)*
search for the word under the cursor (full word targets)g*
search for the word under the cursor (partial word targets)[I
display matching lines (useful in large files to avoid unnecessary jumps)
When you start a search it is displayed on the command line (the last line of
the screen). The trailing /
or ?
is optional, but required if you want to
use an {offset}
or one of the regular expression {options}
(more on these
below).
Useful Options
I set these options in vimrc
:
:set incsearch
highlight search matches as you type:set ignorecase
case insensitive searching:set smartsearch
case sensitive if caps entered:set shortmess-=S
show search counts on command line
I used to set hlsearch
to highlight all search matches, but the majority of
the time I would then just issue the :nohl
command (short for :nohlsearch
)
to remove the highlights. So, I gave that up (I can still enable it temporarily
if needed).
Offset
The default cursor location is on the first character of the target search.
Supplying an {offset}
, which is a number, [num]
, will adjust the cursor
position relative to the found match. There are two types of offsets:
- linewise will adjust
[num]
lines downwards or upwards and leave the cursor at the start of the line. - charwise will move the cursor
[num]
positions to the right or left of the match.
Search command formats using {offset}
:
/{pattern}/{offset}<CR>
search forward for the[count]
‘th match of{pattern}
and offset by the supplied{offset}
amount?{pattern}?{offset}<CR>
ditto, only backwards
Where {offset}
can be specified as:
[num]
lines downwards, in column 1+[num]
lines downwards, in column 1-[num]
lines upwards, in column 1e+[num]
chars to the right of the end of the matche-[num]
chars to the left of the end of the matchs+[num]
chars to the right of the start of the matchs-[num]
chars to the right of the start of the matchb+[num]
chars to the right of the start of the matchb-[num]
chars to the right of the start of the match;{pattern}
perform another search
If [num]
is omitted with +
or -
a count of one is assumed.
Performing another search is like doing a nested search in a single atomic unit and can also be used as a single motion after an operator.
Offset examples
/{pattern}/0<CR>
jump to the start of the line where the match is found/{pattern}/-1<CR>
jump to the start of the line before the line where the match is found/{pattern}/e+1<CR>
jump to one character past the end of the match/entry/;/active<CR>
jump to the start of the word active as the first match after the first match of the word entry. This might be useful in a css file when looking for the active link definition within the entry class
Search Patterns Using Regular Expressions Most frequently, I search for
simple character strings. This is useful, however, Vim also allows for full regular expression matching which, when you need it, is invaluable. Regular expressions are complicated. I cover a few of the basics below and for more detail you can read my Vim RegEx Patterns page.
The general command format is:
/{pattern}/{options}
Where {pattern}
can contain:
^
start of line$
end of line
Regular Expressions Search Examples
Status Line
There are a couple of things I have done in order to display the status of searches. I’m not sure how useful this stuff is, but I’m sticking with it for now.
:set shortmess-=S
will display the current match number and the total number
of matches on the right side of the command line.
I added search information to my status line using the information found in
:help searchcount()
. This displayed the last search term, but the counts were
zero until I enabled the shortmess setting above.
As a side note, the status line counts only seem to get updated when you perform a search in that window. If you move to a new window the search counts are from the last window/time you performed a search and are not nexessarily accurate for the currently active window.
I’m not 100% comfortable with what is happening right now. I suspect I will use the command line counts and just display the last search term on the status line (no counts) as the counts do not seem to get refreshed when moving to different windows. Anyway, I’ll leave it for now. As if often the case with new (to me) Vim features, I am mightily confused until I “get it” and then I think it’s the greatest thing ever.
Search Command Line and Search Command Line Window
<Up>
and <Down>
on the search command line will cycle through previous
searches. Hitting <CR>
will execute the search again.
Entering <C-f>
on the search command line will bring up the search command
line window. Alternatively, the q/
command from normal mode will also bring
up the search command line window.
The search command window works in the same way as the regular command window, except it is restricted to previous search commands.
You can move and edit this window like a regular buffer.
When you hit <CR>
that search line is executed.
You can exit the search command line window using <C-c>
.
You can also exit the search command line using <C-c>
.
Resources
:help search-commands
is a great place place to start exploring.
:help search-offset
for details on search offsets.
:help search-pattern
for details on search patterns.
:help searchcount()
for information on setting up the status line with search
details.