Vim Tips Wiki

Editing

Capture ex command output

1
  • The edit can be undone. Please check the comparison below to verify that this is what you want to do, and then save the changes below to finish undoing the edit. If you are undoing an edit that is …
Latest revision Your text
Line 12: Line 12:
 
|category2=
 
|category2=
 
}}
 
}}
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!
+
Ever want to capture the output of an ex command like <tt>:set all</tt> into a Vim text buffer for easy viewing? This is actually a very easy thing to accomplish!
   
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 <tt>:redir</tt> 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:
 
For example:
Line 23: Line 23:
 
</pre>
 
</pre>
   
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>.
+
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 [[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:
 
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:
Line 46: Line 46:
 
<pre>:TabMessage highlight</pre>
 
<pre>:TabMessage highlight</pre>
   
Note that <code>:redir</code> can use a variable instead of a register, as shown above.
+
Note that <tt>:redir</tt> can use a variable instead of a register, as shown above.
   
 
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.
 
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.
  Loading editor
Below are some commonly used wiki markup codes. Simply click on what you want to use and it will appear in the edit box above.

View this template