Vim Tips Wiki
We recommend that you log in before editing. This will allow other users to leave you a message about your edit, and will let you track edits via your Watchlist. Creating an account is quick and free.

The edit can be undone. Please check the comparison below to verify that this is what you want to do, and then publish the changes below to finish undoing the edit.

Latest revision Your text
Line 1: Line 1:
 
{{review}}
 
{{review}}
  +
{{Tip
{{TipImported
 
 
|id=95
 
|id=95
  +
|title=How do I pipe the output from ex commands into the text buffer?
|previous=94
 
 
|created=August 7, 2001 10:56
|next=96
 
|created=2001
 
 
|complexity=intermediate
 
|complexity=intermediate
|author=
+
|author=Anonymous
|version=7.0
+
|version=6.0
 
|rating=88/31
 
|rating=88/31
  +
|text=
|category1=Tabs
 
  +
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!
|category2=
 
  +
  +
  +
  +
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?
 
}}
 
}}
Ever want to capture the output of an ex command like <code>:set all</code> into a Vim text buffer for easy viewing? This is actually a very easy thing to accomplish!
 
   
 
== Comments ==
You can use the <code>:redir</code> command to redirect the output of an ex command to a register and then paste the contents of the register into a Vim buffer.
 
   
 
You can use the :redir command to redirect the output of an ex command to
For example:
 
  +
a register and then paste the contents of the register into a Vim buffer.
<pre>
 
 
For example:
:redir @a
 
:set all
 
:redir END
 
</pre>
 
   
  +
:redir --AT--a
Now, register 'a' will have the output of the "set all" ex command. You can paste this into a Vim buffer, using <code>"ap</code>.
 
 
:set all
 
:redir END
   
 
Now, register 'a' will have the output of the "set all" ex command. You
You can also write a Vim function to do the above. Since you probably don't want your command output to mess up your carefully constructed window layout, this function will pipe the output of a command into a new tab, allowing you to simply close the tab when done. If you don't like [[using tab pages]], or you don't have tab support because you didn't compile with it or your Vim version is less than 7.0, you could modify this function to use a new split window instead:
 
  +
can paste this into a Vim buffer. You can also write a Vim function
<pre>
 
  +
to do the above.
function! TabMessage(cmd)
 
redir => message
 
silent execute a:cmd
 
redir END
 
if empty(message)
 
echoerr "no output"
 
else
 
" use "new" instead of "tabnew" below if you prefer split windows instead of tabs
 
tabnew
 
setlocal buftype=nofile bufhidden=wipe noswapfile nobuflisted nomodified
 
silent put=message
 
endif
 
endfunction
 
command! -nargs=+ -complete=command TabMessage call TabMessage(&lt;q-args>)
 
</pre>
 
   
  +
For more information, read [http://vimplugin.sf.net/cgi-bin/help?tag={{urlencode:redir}} :help redir]
Example usage:
 
<pre>:TabMessage highlight</pre>
 
   
Note that <code>:redir</code> can use a variable instead of a register, as shown above.
 
   
  +
Yegappan
Note also that <code>:redir</code> will capture silenced messages as well. While this won't be problematic with most builtin commands that echo stuff that we are interested in, this is quite problematic when we execute a sequence of several commands. Since version 7.4-2008, Vim provides an <code>execute()</code> function that'll simplify things and avoid side-effects.
 
  +
, August 7, 2001 11:45
  +
----
  +
Wow!!! That's awesome!! Exactly what I want!
   
  +
'''Anonymous'''
==References==
 
  +
, August 7, 2001 14:13
*{{help|:redir}}
 
  +
----
*{{help|execute()}}
 
 
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]
==Related scripts==
 
*{{script|id=120|text=Decho}}
 
   
  +
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 :-)
==Comments==
 
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:
 
<pre>
 
:%! [cmd]
 
</pre>
 
   
  +
mark--AT--zieg.com
That is, if you didn't know about the <code>:retab</code> command, you could expand tabs using basic Unix commands like <code>:%! expand -t 4</code>.
 
  +
, July 25, 2002 11:28
  +
----
 
The answer is (for ex.):
  +
 
:read !ls ~
  +
 
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!
The answer is (for example):
 
<pre>
 
:read !ls ~
 
</pre>
 
   
  +
Grateful
and {{help|:read}} for more information.
 
  +
, September 27, 2004 12:10
 
----
 
----
Here is a function that inserts the output of an Ex command into a split window:
+
Here's a function that pipes the output of a command into a new tab (Vim 7.0):
  +
function! OutputSplitWindow(...)
 
 
function! TabMessage(cmd)
" this function output the result of the Ex command into a split scratch buffer
 
 
redir =&gt; message
let cmd = join(a:000, ' ')
 
 
silent execute a:cmd
let temp_reg = @"
 
 
redir END
redir @"
 
  +
tabnew
silent! execute cmd
 
 
silent put=message
redir END
 
  +
set nomodified
let output = copy(@")
 
 
endfunction
let @" = temp_reg
 
 
command! -nargs=+ -complete=command TabMessage call TabMessage(&lt;q-args&gt;)
if empty(output)
 
  +
echoerr "no output"
 
 
Example usage:
else
 
 
:TabMessage highlight
new
 
  +
setlocal buftype=nofile bufhidden=wipe noswapfile nobuflisted
 
  +
Another alternative is to use Dredir function in the Decho script, http://vim.sourceforge.net/scripts/script.php?script_id=120
put! =output
 
  +
endif
 
  +
'''Anonymous'''
endfunction
 
  +
, September 27, 2006 3:16
command! -nargs=+ -complete=command Output call OutputSplitWindow(<f-args>)
 
Example: :Output echo strftime("%H:%M")
 
:I think I incorporated the useful stuff out of this script into the tip. I did not see much in the way of important differences, and in some ways the tip was better. --[[User:Fritzophrenic|Fritzophrenic]] ([[User talk:Fritzophrenic|talk]]) 18:00, June 3, 2015 (UTC)
 
 
----
 
----
  +
<!-- parsed by vimtips.py in 0.473584 seconds-->
Please note that all contributions to the Vim Tips Wiki are considered to be released under the CC-BY-SA
Cancel Editing help (opens in new window)