vim Tips and tricks to boost productivity Search within a function block


Example

To search for text foo within a {} block surrounding the cursor use the following command (<ESC> - escape key, <CR> - enter key) :

vi{<ESC>/\%Vfoo<CR>

now you can jump between the matches within the block by pressing n and p. If you have hlsearch option enabled this will highlight all the matches. \%V is a special search pattern part, that tells vim to search only in the visually selected area. You can also make a mapping like this:

:vnoremap g/ <ESC>/\%V

After this the above command is shortened to the following:

vi{g/foo<CR>

Another useful trick is to print all the lines containing the pattern:

vi{
:'<,'>g/foo/#

The '<,'> range is inserted automatically.

See :help range and :help :g.