Vim Tips Wiki
Advertisement
Tip 1063 Printable Monobook Previous Next

created 2005 · complexity basic · author zzapper · version 6.0


After searching for text, you can use :g// to list all lines containing the pattern you last searched for. Or, just type g/pattern/ to display all lines containing pattern. This tip shows how to capture the result (the list of all lines that contain the pattern).

User command[]

The following addition for your vimrc defines the :Filter command:

command! -nargs=? Filter let @a='' | execute 'g/<args>/y A' | new | setlocal bt=nofile | put! a

After searching for some text, enter :Filter (or just type :F then press Tab for command completion). Entering this command will open a new scratch window listing all lines that contain the text that was last searched for.

You can also type the search pattern on the command line. For example, :Filter red\|blue lists all lines that contain "red" or "blue".

The command accepts zero or one argument (-nargs=?), and the argument replaces <args> where it occurs in the following. Register a is cleared (let @a=''), then each matching line is appended to that register (y A performed on all matching lines by g/pattern/). A new window with a scratch buffer is created (new | setlocal bt=nofile), and the register is pasted before the blank line in the scratch buffer (put! a). When :g/pattern/ is used, all following text is taken as a command to be executed on matching lines. Since only y A is wanted, the execute command is used as it stops at the bar.

Another way of working with search results is using :vimgrep. After the searching, do the following:

 :vimgrep // %

The limitation of this approach is that you must be editing an existing file. However, the results will appear in a quick list. QuickList is a special vim buffer containing a list of file positions, with possibility to jump there. Its original purpose was to display the output of compilation, with possibility to jump to errors. Working with QuickLists is a whole separate topic on its own, so only a very quick intro here. QuickLists can be augmented, stacked and so on. QuickList window can be opened/closed with :copen/:cclose commands. Finally, QuickList can only be one for the whole vim session, while each window can have its own local location lists. Commands that work with local lists are very similar to quicklist commands, but usually start with "l" : :vimgrep -> :lvimgrep, :copen -> :lopen and so on ...

Redirecting output[]

This mapping shows an alternative method using redirection. Put the following in your vimrc. Then, when you are satisfied with the regex you last used for searching, press F3 to output the :g/pattern/ results to a new window.

nnoremap <silent> <F3> :redir @a<CR>:g//<CR>:redir END<CR>:new<CR>:put! a<CR>

Explanation:

:redir @a         redirect output to register a
:g//              repeat last global command
:redir END        end redirection
:new              create new window
:put! a           paste register a into new window

The following variation uses redirection to append matching lines to a file (which will be created if it does not exist). That might be useful to keep a temporary copy of matches for several different patterns.

nnoremap <silent> <F4> :redir >>matches.tmp<CR>:g//<CR>:redir END<CR>:new matches.tmp<CR>

See also[]

Comments[]

Advertisement