Vim Tips Wiki
m (Send results of a g// to a new window moved to Send output of g search to a new window: Page moved by JohnBot to improve title)
(Change to TipImported template + severe manual clean)
Line 1: Line 1:
 
 
{{review}}
 
{{review}}
  +
{{TipImported
{{Tip
 
 
|id=1063
 
|id=1063
  +
|previous=1061
|title=Send results of a g// to a new window
 
  +
|next=1064
|created=December 1, 2005 2:02
+
|created=December 1, 2005
 
|complexity=basic
 
|complexity=basic
 
|author=zzapper
 
|author=zzapper
 
|version=5.7
 
|version=5.7
 
|rating=38/16
 
|rating=38/16
 
}}
|text=
 
  +
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.
" .vimrc
 
   
  +
To achieve this, you need the following map (place in your vimrc):
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>
 
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>
   
 
Explanation:
  +
<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>
   
 
==Comments==
 
Send output of last g// to a named file (and open):
   
  +
<pre>
How to Use:-
 
 
: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;
  +
</pre>
   
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
 
 
 
zzapper
 
, December 1, 2005 2:08
 
 
----
 
----
To not get line numbers printed if line numbers are turned on:
+
To not get line numbers printed if line numbers are turned on:
 
" before g//, store line number state and turn off line numbers; after g//, restore the previous state
 
 
: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
 
t &amp;number=--AT--b&lt;CR&gt;:new&lt;CR&gt;:put! a&lt;CR&gt;&lt;CR&gt;
 
   
  +
<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>
   
sascha.ruppert--AT--gmx.de
 
, December 3, 2005 4:42
 
 
----
 
----
<!-- parsed by vimtips.py in 0.460156 seconds-->
 

Revision as of 10:16, 20 December 2007

Tip 1063 Printable Monobook Previous Next

created December 1, 2005 · complexity basic · author zzapper · version 5.7


Suppose you are using g/pattern/ 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 g/pattern/ results to a new window.

To achieve this, you need the following map (place in your vimrc):

nmap <F3> :redir @a<CR>:g//<CR>:redir END<CR>:new<CR>:put! a<CR><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

Comments

Send output of last g// to a named file (and open):

:nmap <F4> :redir! >/aaa/xz<CR>:g//<CR>:redir END<CR>:new /aaa/xz<CR><CR>

To not get line numbers printed if line numbers are turned on:

" before g//, store line number state and turn off line numbers; after g//, restore the 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>