FANDOM


(begin cleanup/comment merge)
(Add a note about the recent `execute()` function.)
 
(7 intermediate revisions by 4 users not shown)
Line 4: Line 4:
 
|previous=94
 
|previous=94
 
|next=96
 
|next=96
|created=August 7, 2001
+
|created=2001
 
|complexity=intermediate
 
|complexity=intermediate
|author=Anonymous
+
|author=
|version=6.0
+
|version=7.0
 
|rating=88/31
 
|rating=88/31
|category1=
+
|category1=Tabs
 
|category2=
 
|category2=
 
}}
 
}}
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!
+
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!
   
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.
+
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.
For example:
 
   
  +
For example:
 
<pre>
 
<pre>
 
:redir @a
 
:redir @a
Line 22: 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 <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 <code>"ap</code>.
 
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):
 
   
  +
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:
 
<pre>
 
<pre>
 
function! TabMessage(cmd)
 
function! TabMessage(cmd)
redir => message
+
redir => message
silent execute a:cmd
+
silent execute a:cmd
redir END
+
redir END
tabnew
+
if empty(message)
silent put=message
+
echoerr "no output"
set nomodified
+
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
 
endfunction
 
command! -nargs=+ -complete=command TabMessage call TabMessage(&lt;q-args>)
 
command! -nargs=+ -complete=command TabMessage call TabMessage(&lt;q-args>)
Line 39: Line 41:
 
<pre>:TabMessage highlight</pre>
 
<pre>:TabMessage highlight</pre>
   
Note that <tt>:redir</tt> can use a variable instead of a register, as shown above.
+
Note that <code>:redir</code> 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.
   
 
==References==
 
==References==
* {{help|:redir}}
+
*{{help|:redir}}
  +
*{{help|execute()}}
   
 
==Related scripts==
 
==Related scripts==
* {{script|id=120|text=Decho}}
+
*{{script|id=120|text=Decho}}
   
 
==Comments==
 
==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:
+
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]
+
:%! [cmd]
  +
</pre>
   
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 :-)
+
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>.
   
 
----
 
----
The answer is (for ex.):
+
The answer is (for example):
+
<pre>
:read !ls ~
+
:read !ls ~
+
</pre>
and :help :read for more info
 
   
  +
and {{help|:read}} for more information.
  +
----
  +
Here is a function that inserts the output of an Ex command into a split window:
  +
function! OutputSplitWindow(...)
  +
" this function output the result of the Ex command into a split scratch buffer
  +
let cmd = join(a:000, ' ')
  +
let temp_reg = @"
  +
redir @"
  +
silent! execute cmd
  +
redir END
  +
let output = copy(@")
  +
let @" = temp_reg
  +
if empty(output)
  +
echoerr "no output"
  +
else
  +
new
  +
setlocal buftype=nofile bufhidden=wipe noswapfile nobuflisted
  +
put! =output
  +
endif
  +
endfunction
  +
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)
 
----
 
----

Latest revision as of 14:47, January 3, 2017

Tip 95 Printable Monobook Previous Next

created 2001 · complexity intermediate · version 7.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 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:

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(<q-args>)

Example usage:

:TabMessage highlight

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

Note also that :redir 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 execute() function that'll simplify things and avoid side-effects.

ReferencesEdit

Related scriptsEdit

CommentsEdit

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]

That is, if you didn't know about the :retab command, you could expand tabs using basic Unix commands like :%! expand -t 4.


The answer is (for example):

:read !ls ~

and :help :read for more information.


Here is a function that inserts the output of an Ex command into a split window:

function! OutputSplitWindow(...)
  " this function output the result of the Ex command into a split scratch buffer
  let cmd = join(a:000, ' ')
  let temp_reg = @"
  redir @"
  silent! execute cmd
  redir END
  let output = copy(@")
  let @" = temp_reg
  if empty(output)
    echoerr "no output"
  else
    new
    setlocal buftype=nofile bufhidden=wipe noswapfile nobuflisted
    put! =output
  endif
endfunction
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. --Fritzophrenic (talk) 18:00, June 3, 2015 (UTC)

Community content is available under CC-BY-SA unless otherwise noted.