54

How can I redirect or pipe the output of an ex command into my current buffer or a file?

For example, I want to read the contents of all the registers into the current buffer, which in ex mode is shown using :registers.

1
  • vi.stackexchange.com/a/13881/10254 might be of interest, basically get input, then run command, capture and put. With vimscript functions, Capture(excmd) PutAfterCapture(command) GetInputCommandThenCaptureAndPut()
    – qeatzy
    Oct 10, 2017 at 1:14

6 Answers 6

45
:redir >name_of_registers_file
:registers
:redir END
:r name_of_registers_file
:help redir

The last command is very useful, since there are lots of options for redirection: to variables, to registers, how to append, further cornucopia.

I still find it weird and annoying that it uses END that way, but since everything else that can follow redir has to start with a non-word-character, at least it's not ambiguous.

PS AFAIK (which is pretty far in this case) there's no way to read it directly into the buffer: you have to store it in a register or a variable first. Check the help for the various options of how to do that.

PPS If you do want to do this using a variable —maybe to encapsulate it in a function and avoid clobbering registers or global variables— you'll have to convert the multiline string that gets written to the variable into a list. EG

:call append( '.', split(variable_you_redirected_to, "\n") )

Otherwise (if you just do append('.',var)) you end up with ^@'s (nulls) instead of newlines, since that's what vimscript uses to represent newlines in String variables.

edit: as @Bill Odom mentions, using :put =variable_you_redirected_to is a lot easier than the complicated append() expression. Thanks, Bill!


PPPS

I've written a snippet to make this stuff more convenient. It declares a function Redir(command, target) and a command R.

The command parses the last series of non-space characters as a redirection target and passes that to the function, which does the boilerplate to redirect the command output to the redirection target.

The command is everything after R and before the last space.

EG

" Store the vim buffer list in buffer_list.txt
:R ls >buffer_list.txt
" Store error messages emitted by a function being debugged
"   in the 'unnamed register'
:R call FunctionBeingDebugged() @">

There are a few limitations with this: for example you won't be able to write to a filename that contains a space. The upside to this is that you don't have to quote your command. I've got it posted on gist.github.com, and I'll try to keep it updated if I end up improving it. Or you can fork it yourself</noeuphemism>!

Anyway the snippet is available here. It can be dropped into a .vimrc file or into a file in ~/.vim/plugins.

1
  • Using @intuited's Exec function from here, a mapping like :inoremap <C-S-C> <C-R>=Exec("")<Left><Left> maps Ctrl+Shift+C (for Command) to make it easier to insert the output of a command like :ls at the current point in the buffer. Feb 4, 2014 at 5:24
38

@intuited is right; the redir command is what you want. A one-liner like this will insert the output of :registers into the current buffer:

redir => m | silent registers | redir END | put=m

That's not something you'll want to type very often, however, and it's not exactly amenable to a key map. I found myself doing this fairly often, so I wrote a function and a handful of commands to make it easier. As a bonus, I can now send command output to a new window or new tab as easily as inserting it into the current buffer. Here's the code (with a few command examples at the very end):

" redir_messages.vim
"
" Inspired by the TabMessage function/command combo found
" at <http://www.jukie.net/~bart/conf/vimrc>.
"

function! RedirMessages(msgcmd, destcmd)
"
" Captures the output generated by executing a:msgcmd, then places this
" output in the current buffer.
"
" If the a:destcmd parameter is not empty, a:destcmd is executed
" before the output is put into the buffer. This can be used to open a
" new window, new tab, etc., before :put'ing the output into the
" destination buffer.
"
" Examples:
"
"   " Insert the output of :registers into the current buffer.
"   call RedirMessages('registers', '')
"
"   " Output :registers into the buffer of a new window.
"   call RedirMessages('registers', 'new')
"
"   " Output :registers into a new vertically-split window.
"   call RedirMessages('registers', 'vnew')
"
"   " Output :registers to a new tab.
"   call RedirMessages('registers', 'tabnew')
"
" Commands for common cases are defined immediately after the
" function; see below.
"
    " Redirect messages to a variable.
    "
    redir => message

    " Execute the specified Ex command, capturing any messages
    " that it generates into the message variable.
    "
    silent execute a:msgcmd

    " Turn off redirection.
    "
    redir END

    " If a destination-generating command was specified, execute it to
    " open the destination. (This is usually something like :tabnew or
    " :new, but can be any Ex command.)
    "
    " If no command is provided, output will be placed in the current
    " buffer.
    "
    if strlen(a:destcmd) " destcmd is not an empty string
        silent execute a:destcmd
    endif

    " Place the messages in the destination buffer.
    "
    silent put=message

endfunction

" Create commands to make RedirMessages() easier to use interactively.
" Here are some examples of their use:
"
"   :BufMessage registers
"   :WinMessage ls
"   :TabMessage echo "Key mappings for Control+A:" | map <C-A>
"
command! -nargs=+ -complete=command BufMessage call RedirMessages(<q-args>, ''       )
command! -nargs=+ -complete=command WinMessage call RedirMessages(<q-args>, 'new'    )
command! -nargs=+ -complete=command TabMessage call RedirMessages(<q-args>, 'tabnew' )

" end redir_messages.vim
5
  • 2
    Nice, I didn't realize you could just :put =var.
    – intuited
    Apr 12, 2010 at 2:58
  • nice, i used this to output a txt file of my keymappings for reference, but just switching it to: redir => m | silent map | redir END | put=m Apr 25, 2015 at 17:35
  • 1
    I have absolutely hijacked this into my own vim config. (With an attribution, of course, so I don't end up thinking I wrote it.) Thanks!
    – Jon Carter
    Oct 26, 2015 at 17:24
  • Very useful indeed, and just what I was looking for. - Personally I don't want it to keep asking if I want to lose unsaved changes when I close the window or tab again, II'll not be keeping the results, so I added a commented alternate version of "silent execute a:destcmd" to the code above giving that variation Feb 17, 2016 at 17:14
  • In love, and SO SO much of my vimminess is eased now. Snag'd that into my vimfiles too! (With apt credits of course, just as @JonCarter said.) 🙃
    – Cometsong
    Apr 12, 2018 at 13:35
29
:redir @a
:registers
:redir END
"ap

:redir @a redirects all messages from here on to a register named a. You follow this with your command whose output you want to capture (:registers in your case). :redir END ends the redirection. "ap means, "a uses the register named a and p puts the contents of the selected register into the current buffer.

See Capture ex command output at Vim Tips Wiki for more information :-)

2
  • This is shorter and more direct than the accepted answer. Unless there's copious output that you want to store in a file as well this is a better solution.
    – NeilG
    Feb 12, 2020 at 0:17
  • From the linked Vim Tip if your ex command is actually an external (shell) command you can just :read !<shell command> and pipe the output straight into the buffer.
    – NeilG
    Jul 15, 2021 at 23:43
11

In addition, :h :redir:

...To get the output of one command the |execute()| function can be used.

e.g.

put = execute('scriptnames')
put = execute('filter /vimfiles/ scriptnames')
1
  • 1
    This is the easiest way IMO. No saving or variable required. Jan 5, 2022 at 16:34
9

It is not necessary to use a temporary variable as long as you can save the current buffer and are ok with appending the messages to the end of the current file.

From the Vim documentation:

:redi[r] >> {file}      Redirect messages to file {file}.  Append if {file}
                        already exists.  {not in Vi}

http://vimdoc.sourceforge.net/htmldoc/various.html#:redir

So, to append the messages from :registers to the bottom of the current file, do this:

:write | redir >> % | silent registers | redir END | edit
  1. :write the file so that any changes won't be lost
  2. Begin redirecting output to %, the name of the current file.
  3. Silently run the :registers command.
  4. END redirecting to the file.
  5. :edit the file to see the new changes.

You can also forcefully truncate the current file and replace it with the output messages:

:redir! > % | silent registers | redir END | edit!

But that is probably not what you want to do.

7

Unfortunately I have no rep, so I cannot add this as a comment to 'Francis Niu'.

The execute() function looks like the easiest way to accomplish this, and it was added in vim 8 .

Listed in the new/extended features on vim's github. version8.txt

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.