Vim Tips Wiki
m
 
Tag: Visual edit
 
(15 intermediate revisions by 6 users not shown)
Line 1: Line 1:
  +
{{TipImported
 
{{review}}
 
{{Tip
 
 
|id=1063
 
|id=1063
  +
|previous=1061
|title=Send results of a g// to a new window
 
  +
|next=1065
|created=December 1, 2005 2:02
+
|created=2005
 
|complexity=basic
 
|complexity=basic
 
|author=zzapper
 
|author=zzapper
|version=5.7
+
|version=6.0
 
|rating=38/16
 
|rating=38/16
  +
|category1=Searching
|text=
 
  +
|category2=
" .vimrc
 
 
}}
  +
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==
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;
 
  +
The following addition for your [[vimrc]] defines the <code>:Filter</code> 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 <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.
How to Use:-
 
 
perfect your g/regexp/
 
 
The when you are satisfied hit &lt;F3&gt;
 
 
 
 
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
 
 
 
 
Idea by Peppe (map by me zzapper)
 
 
 
}}
 
 
== Comments ==
 
" send output of last g// to a named file (and open)
 
: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;
 
 
You will be able to think of your own variants
 
   
  +
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==
zzapper
 
  +
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.
, December 1, 2005 2:08
 
  +
<pre>
----
 
  +
nnoremap <silent> <F3> :redir @a<CR>:g//<CR>:redir END<CR>:new<CR>:put! a<CR>
To not get line numbers printed if line numbers are turned on:
 
  +
</pre>
   
 
Explanation:
" before g//, store line number state and turn off line numbers; after g//, restore the previous state
 
  +
<pre>
 
: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
  +
</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.
:nmap &lt;F3&gt; :let --AT--b=&amp;number&lt;CR&gt;:set nonumber&lt;CR&gt;:redir --AT--a&lt;CR&gt;:g//&lt;CR&gt;:redir END&lt;CR&gt;:le
 
  +
<pre>
t &amp;number=--AT--b&lt;CR&gt;:new&lt;CR&gt;:put! a&lt;CR&gt;&lt;CR&gt;
 
  +
nnoremap <silent> <F4> :redir >>matches.tmp<CR>:g//<CR>:redir END<CR>:new matches.tmp<CR>
  +
</pre>
   
  +
==See also==
  +
*[[VimTip227|Power of g]] for more on the <code>:g//</code> command
  +
*[[VimTip478|Copy the search results into clipboard]] for other methods to capture search hits
   
 
==Comments==
sascha.ruppert--AT--gmx.de
 
, December 3, 2005 4:42
 
----
 
<!-- parsed by vimtips.py in 0.460156 seconds-->
 

Latest revision as of 09:39, 17 June 2021

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