Vim Tips Wiki
(Move categories to tip template)
No edit summary
Tag: Visual edit
 
(14 intermediate revisions by 7 users not shown)
Line 1: Line 1:
{{review}}
 
 
{{TipImported
 
{{TipImported
 
|id=1063
 
|id=1063
 
|previous=1061
 
|previous=1061
|next=1064
+
|next=1065
|created=December 1, 2005
+
|created=2005
 
|complexity=basic
 
|complexity=basic
 
|author=zzapper
 
|author=zzapper
|version=5.7
+
|version=6.0
 
|rating=38/16
 
|rating=38/16
|category1=
+
|category1=Searching
 
|category2=
 
|category2=
 
}}
 
}}
Suppose you are using <tt>g/pattern/</tt> to display all lines containing some pattern in your file. When you are satisfied with the regex you are using for the pattern, you can press F3 to output the <tt>g/pattern/</tt> results to a new window.
+
After searching for text, you can use <code>:g//</code> to list all lines containing the pattern you last searched for. Or, just type <code>g/pattern/</code> 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==
To achieve this, you need the following map (place in your vimrc):
 
  +
The following addition for your [[vimrc]] defines the <code>:Filter</code> command:
 
<pre>
 
<pre>
  +
command! -nargs=? Filter let @a='' | execute 'g/<args>/y A' | new | setlocal bt=nofile | put! a
nmap &lt;F3&gt; :redir @a&lt;CR&gt;:g//&lt;CR&gt;:redir END&lt;CR&gt;:new&lt;CR&gt;:put! a&lt;CR&gt;&lt;CR&gt;
 
  +
  +
 
</pre>
  +
  +
After searching for some text, enter <code>:Filter</code> (or just type <code>:F</code> 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, <code>:Filter red\|blue</code> lists all lines that contain "red" or "blue".
  +
  +
The command accepts zero or one argument (<code>-nargs=?</code>), and the argument replaces <code><args></code> where it occurs in the following. Register <code>a</code> is cleared (<code>let @a='&#39;</code>), then each matching line is appended to that register (<code>y A</code> performed on all matching lines by <code>g/pattern/</code>). A new window with a scratch buffer is created (<code>new | setlocal bt=nofile</code>), and the register is pasted before the blank line in the scratch buffer (<code>put! a</code>). When <code>:g/pattern/</code> is used, all following text is taken as a command to be executed on matching lines. Since only <code>y A</code> is wanted, the <code>execute</code> command is used as it stops at the bar.
  +
  +
Another way of working with search results is using <code>:vimgrep</code>. 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 <code>:copen/:cclose</code> 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" : <code>:vimgrep</code> -> <code>:lvimgrep</code>, <code>:copen</code> -> <code>:lopen</code> 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 <code>:g/pattern/</code> results to a new window.
 
<pre>
  +
nnoremap <silent> <F3> :redir @a<CR>:g//<CR>:redir END<CR>:new<CR>:put! a<CR>
 
</pre>
 
</pre>
   
Line 28: Line 46:
 
</pre>
 
</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.
==Comments==
 
Send output of last g// to a named file (and open):
 
 
 
<pre>
 
<pre>
:nmap &lt;F4&gt; :redir! &gt;/aaa/xz&lt;CR&gt;:g//&lt;CR&gt;:redir END&lt;CR&gt;:new /aaa/xz&lt;CR&gt;&lt;CR&gt;
+
nnoremap <silent> <F4> :redir >>matches.tmp<CR>:g//<CR>:redir END<CR>:new matches.tmp<CR>
 
</pre>
 
</pre>
   
  +
==See also==
----
 
  +
*[[VimTip227|Power of g]] for more on the <code>:g//</code> command
To not get line numbers printed if line numbers are turned on:
 
  +
*[[VimTip478|Copy the search results into clipboard]] for other methods to capture search hits
   
 
==Comments==
<pre>
 
" before g//, store line number state and turn off line numbers; after g//, restore the previous state
 
:nmap &lt;F3&gt; :let @b=&amp;number&lt;CR&gt;:set nonumber&lt;CR&gt;:redir @a&lt;CR&gt;:g//&lt;CR&gt;:redir END&lt;CR&gt;:let &amp;number=@b&lt;CR&gt;:new&lt;CR&gt;:put! a&lt;CR&gt;&lt;CR&gt;
 
</pre>
 
 
----
 

Latest revision as of 12:36, 29 February 2024

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[]