Vim Tips Wiki
(Use a more readable notation for clearing the register.)
(Some rewording and merging, and omit comment about numbered lines since that does not seem to be necessary in current Vim.)
Line 12: Line 12:
 
}}
 
}}
 
After searching for text, you can use <tt>:g//</tt> to list all lines containing the pattern you last searched for. Or, just type <tt>g/pattern/</tt> to display all lines containing ''pattern''. This tip shows how to capture the result (the list of all lines that contain the pattern).
 
After searching for text, you can use <tt>:g//</tt> to list all lines containing the pattern you last searched for. Or, just type <tt>g/pattern/</tt> 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 <tt>:Filter</tt> command:
 
<pre>
 
command! -nargs=? Filter let @a='' | execute 'g/<args>/y A' | new | setlocal bt=nofile | put! a
 
</pre>
  +
  +
After searching for some text, enter <tt>:Filter</tt> (or just type <tt>:F</tt> 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, <tt>:Filter&nbsp;red\|blue</tt> lists all lines that contain "red" or "blue".
  +
  +
The command accepts zero or one argument (<tt>-nargs=?</tt>), and the argument replaces <tt><args></tt> where it occurs in the following. Register <tt>a</tt> is cleared (<tt>let&nbsp;@a='&#39;</tt>), then each matching line is appended to that register ({{tt|y A}} performed on all matching lines by <tt>g/pattern/</tt>). A new window with a scratch buffer is created ({{tt|1=new &#124; setlocal bt=nofile}}), and the register is pasted before the blank line in the scratch buffer ({{tt|put! a}}). When <tt>:g/pattern/</tt> is used, all following text is taken as a command to be executed on matching lines. Since only {{tt|y A}} is wanted, the <tt>execute</tt> command is used as it stops at the bar.
   
 
==Redirecting output==
 
==Redirecting output==
Put the following map in your [[vimrc]]. Then, when you are satisfied with the regex you last used for searching, press F3 to output the <tt>:g/pattern/</tt> results to a new window.
+
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 <tt>:g/pattern/</tt> results to a new window.
 
<pre>
 
<pre>
nmap <F3> :redir @a<CR>:g//<CR>:redir END<CR>:new<CR>:put! a<CR><CR>
+
nnoremap <silent> <F3> :redir @a<CR>:g//<CR>:redir END<CR>:new<CR>:put! a<CR>
 
</pre>
 
</pre>
   
Line 26: Line 38:
 
:new create new window
 
:new create new window
 
:put! a paste register a into new window
 
:put! a paste register a into new window
 
</pre>
  +
  +
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.
 
<pre>
 
nnoremap <silent> <F4> :redir >>matches.tmp<CR>:g//<CR>:redir END<CR>:new matches.tmp<CR>
 
</pre>
 
</pre>
   
Line 33: Line 50:
   
 
==Comments==
 
==Comments==
Send output of last <tt>g//</tt> to a named file, and open the file:
 
<pre>
 
:nmap <F4> :redir! >/mydir/temp.txt<CR>:g//<CR>:redir END<CR>:new /mydir/temp.txt<CR><CR>
 
</pre>
 
 
----
 
To not get line numbers printed if line numbers are turned on:
 
<pre>
 
" Turn off line numbers, do g//, restore previous state.
 
:nmap <F3> :let @b=&number<CR>:set nonumber<CR>:redir @a<CR>:g//<CR>:redir END<CR>:let &number=@b<CR>:new<CR>:put! a<CR><CR>
 
</pre>
 
 
----
 
A slightly adapted version to run as a user-defined command:
 
<pre>
 
command! Filter let @a=&#39;&#39; | execute 'g//y A' | split | enew | setlocal bt=nofile | put! a
 
</pre>
 
* <code>let @a=&#39;&#39;</code> -- clear the register
 
* <code>execute 'g//y A'</code> -- copy all matching lines to the end of the register
 
* <code>split | enew | setlocal bt=nofile</code> -- create a new scratch buffer
 
* <code>put! a</code> -- paste the contents of the register into the scratch buffer
 
 
----
 

Revision as of 04:40, 12 April 2012

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.

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