Vim

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:

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:

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:

Search command formats using {offset}:

Where {offset} can be specified as:

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

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:

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.

Tools

Web

Languages

Data