Vim Tips Wiki
m (How do I pipe the output from ex commands into the text buffer? moved to Capture ex command output: Page moved by JohnBot to improve title)
(Uploaded by JohnBot from a locally edited file)
Line 1: Line 1:
 
{{review}}
 
{{review}}
  +
{{TipImported
{{Tip
 
 
|id=95
 
|id=95
  +
|previous=94
|title=How do I pipe the output from ex commands into the text buffer?
 
  +
|next=96
|created=August 7, 2001 10:56
+
|created=August 7, 2001
 
|complexity=intermediate
 
|complexity=intermediate
 
|author=Anonymous
 
|author=Anonymous
 
|version=6.0
 
|version=6.0
 
|rating=88/31
 
|rating=88/31
|text=
 
This is a *request* for a tip. I need to be able to pipe the output of a :blah ex command into the vim text buffer for editing. I wanted to do this many times for different reasons and could never find a way!
 
 
 
 
I would just love to be able to do :hi --> textBuffer and examine the output at my own leasure scrolling up and down and using vim search commands on it. Same thing for :set all, and other things. Considering that cut and paste is horrible in windows, I can't for example do :set guioptions? then cut and paste! So I have to retype it, or cut and paste from the help manual. I really want to be able to pipe the output of ex commands into the text buffer. Can someone help me?
 
 
}}
 
}}
 
This is a *request* for a tip. I need to be able to pipe the output of a :blah ex command into the Vim text buffer for editing. I wanted to do this many times for different reasons and could never find a way!
   
 
I would just love to be able to do :hi --> textBuffer and examine the output at my own leasure scrolling up and down and using Vim search commands on it. Same thing for :set all, and other things. Considering that cut and paste is horrible in windows, I can't for example do :set guioptions? then cut and paste! So I have to retype it, or cut and paste from the help manual. I really want to be able to pipe the output of ex commands into the text buffer. Can someone help me?
== Comments ==
 
   
 
==Comments==
You can use the :redir command to redirect the output of an ex command to
+
You can use the :redir command to redirect the output of an ex command to
a register and then paste the contents of the register into a Vim buffer.
+
a register and then paste the contents of the register into a Vim buffer.
For example:
+
For example:
   
  +
<pre>
:redir --AT--a
+
:redir @a
:set all
+
:set all
:redir END
+
:redir END
  +
</pre>
   
Now, register 'a' will have the output of the "set all" ex command. You
+
Now, register 'a' will have the output of the "set all" ex command. You can paste this into a Vim buffer. You can also write a Vim function to do the above.
can paste this into a Vim buffer. You can also write a Vim function
 
to do the above.
 
   
  +
See {{help|:redir}}.
For more information, read [http://vimplugin.sf.net/cgi-bin/help?tag={{urlencode:redir}} :help redir]
 
   
 
Yegappan
 
, August 7, 2001 11:45
 
 
----
 
----
 
This may be obvious to experts, but it took me a very long time to figure it out, because Google searches on terms like 'pipe', 'buffer', 'shell', etc never brought it to my attention. However, you can pipe the contents of the file currently being edited (the current buffer) to a shell command, and replace the current file/buffer with the _output_ of that command, using this:
Wow!!! That's awesome!! Exactly what I want!
 
   
 
:%! [cmd]
'''Anonymous'''
 
, August 7, 2001 14:13
 
----
 
This may be obvious to experts, but it took me a very long time to figure it out, because Google searches on terms like 'pipe', 'buffer', 'shell', etc never brought it to my attention. However, you can pipe the contents of the file currently being edited (the current buffer) to a shell command, and replace the current file/buffer with the _output_ of that command, using this:
 
 
:%! [cmd]
 
   
 
ie, if you didn't know the :retab command (as for a long time I didn't), you could expand tabs using basic unix commands like ":%! expand -t 4". Wish I'd known this a long time ago, so I'm posting it here in the hopes that others might find it :-)
 
ie, if you didn't know the :retab command (as for a long time I didn't), you could expand tabs using basic unix commands like ":%! expand -t 4". Wish I'd known this a long time ago, so I'm posting it here in the hopes that others might find it :-)
   
mark--AT--zieg.com
 
, July 25, 2002 11:28
 
 
----
 
----
The answer is (for ex.):
+
The answer is (for ex.):
   
:read !ls ~
+
:read !ls ~
   
and :help :read for more info :-)
+
and :help :read for more info
 
aniou--AT--root.pl
 
, February 18, 2004 14:01
 
----
 
Thanks Anonymous and Yegappan, I've long wanted to do this too, but never known how. Great initiative Anonymous!
 
   
Grateful
 
, September 27, 2004 12:10
 
 
----
 
----
Here's a function that pipes the output of a command into a new tab (Vim 7.0):
+
Here's a function that pipes the output of a command into a new tab (Vim 7.0):
   
  +
<pre>
function! TabMessage(cmd)
+
function! TabMessage(cmd)
redir =&gt; message
+
redir =&gt; message
silent execute a:cmd
+
silent execute a:cmd
redir END
+
redir END
tabnew
+
tabnew
silent put=message
+
silent put=message
set nomodified
+
set nomodified
endfunction
+
endfunction
command! -nargs=+ -complete=command TabMessage call TabMessage(&lt;q-args&gt;)
+
command! -nargs=+ -complete=command TabMessage call TabMessage(&lt;q-args&gt;)
  +
</pre>
   
Example usage:
+
Example usage:
:TabMessage highlight
+
:TabMessage highlight
   
Another alternative is to use Dredir function in the Decho script, http://vim.sourceforge.net/scripts/script.php?script_id=120
+
Another alternative is to use Dredir function in the Decho script {{script|id=120}}.
   
'''Anonymous'''
 
, September 27, 2006 3:16
 
 
----
 
----
<!-- parsed by vimtips.py in 0.473584 seconds-->
 

Revision as of 10:37, 30 October 2007

Tip 95 Printable Monobook Previous Next

created August 7, 2001 · complexity intermediate · author Anonymous · version 6.0


This is a *request* for a tip. I need to be able to pipe the output of a :blah ex command into the Vim text buffer for editing. I wanted to do this many times for different reasons and could never find a way!

I would just love to be able to do :hi --> textBuffer and examine the output at my own leasure scrolling up and down and using Vim search commands on it. Same thing for :set all, and other things. Considering that cut and paste is horrible in windows, I can't for example do :set guioptions? then cut and paste! So I have to retype it, or cut and paste from the help manual. I really want to be able to pipe the output of ex commands into the text buffer. Can someone help me?

Comments

You can use the :redir command to redirect the output of an ex command to a register and then paste the contents of the register into a Vim buffer. For example:

:redir @a
:set all
:redir END

Now, register 'a' will have the output of the "set all" ex command. You can paste this into a Vim buffer. You can also write a Vim function to do the above.

See :help :redir.


This may be obvious to experts, but it took me a very long time to figure it out, because Google searches on terms like 'pipe', 'buffer', 'shell', etc never brought it to my attention. However, you can pipe the contents of the file currently being edited (the current buffer) to a shell command, and replace the current file/buffer with the _output_ of that command, using this:

:%! [cmd]

ie, if you didn't know the :retab command (as for a long time I didn't), you could expand tabs using basic unix commands like ":%! expand -t 4". Wish I'd known this a long time ago, so I'm posting it here in the hopes that others might find it :-)


The answer is (for ex.):

:read !ls ~

and :help :read for more info


Here's a function that pipes the output of a command into a new tab (Vim 7.0):

function! TabMessage(cmd)
 redir => message
 silent execute a:cmd
 redir END
 tabnew
 silent put=message
 set nomodified
endfunction
command! -nargs=+ -complete=command TabMessage call TabMessage(<q-args>)

Example usage:

:TabMessage highlight

Another alternative is to use Dredir function in the Decho script script#120.