Vim Tips Wiki
(begin cleanup/comment merge)
(make the tab portion of the tip more obvious, put in tabs category)
Line 9: Line 9:
 
|version=6.0
 
|version=6.0
 
|rating=88/31
 
|rating=88/31
|category1=
+
|category1=Tabs
 
|category2=
 
|category2=
 
}}
 
}}
Line 25: Line 25:
 
Now, register 'a' will have the output of the "set all" ex command. You can paste this into a Vim buffer, using <tt>"ap</tt>.
 
Now, register 'a' will have the output of the "set all" ex command. You can paste this into a Vim buffer, using <tt>"ap</tt>.
   
  +
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 [[Introduction to using tab pages|using tabs]], 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:
You can also write a Vim function to do the above.
 
 
For example, here's a function that pipes the output of a command into a new tab (requires Vim 7.0 or higher for tab support):
 
   
 
<pre>
 
<pre>

Revision as of 03:39, 30 November 2009

Tip 95 Printable Monobook Previous Next

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


Ever want to capture the output of an ex command like :set all into a Vim text buffer for easy viewing? This is actually a very easy thing to accomplish!

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, using "ap.

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 tabs, 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:

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

Note that :redir can use a variable instead of a register, as shown above.

References

Related scripts

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:

:%! [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