Technical Leader

Vim Search

I assume that most people that use Vim know how to use the basic search functionality, such as / and ?. I am wanting to elaborate on those a bit.

<CR> represents the actual enter key. If /<CR> is used then the search is executed again using the previous pattern, just hitting 'n' also does the same thing. If you need to go in the opposite direction then use 'N'. Another quick way to search is by using '*'. If your cursor is underneath the word you want to find, then just hit '*' to go to the next occurrence, this is also in a Vim tip, can't remember which one however (:he * will get you there also).

The pound sign, '#', can also be used to search for a word closest to the cursor, however instead of searching forwards, it will search backwards.

When using '*' and '#' the search will only match words, an example is the word foo will not match foobar. If you precede those with a 'g' then this will also find matches that are not a word, so using 'g*' as foo as the word, then foobar will be a match.

Programmers will be interested in :he gd and also :he gD. I will not discuss these here (possibly will in the future).

I also recommend turning the increment search on in your vimrc file (:he incsearch).

While searching it is by default that your search will wrap. Meaning that once your search hits the end of your buffer, then the search will continue at the beginning of the buffer. In order to turn this feature off and to stop searching once the end of the buffer is found turn wrapscan off, :help wrapscan will tell you more about it or just do ':set nowrapscan'.

To find the carrot symbol '^' you need to escape it, such as '/foo^bar'. This will find 'foo^bar'. The carrot symbol is also used to match at the beginning of a line (hence the need to escape it), so to find 'foo^bar' at the beginning of a line the search pattern will be '/^foo^bar'.