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.
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
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
redirhas 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
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_tois a lot easier than the complicatedappend()expression. Thanks, Bill!PPPS
I’ve written a snippet to make this stuff more convenient. It declares a function
Redir(command, target)and a commandR.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
Rand before the last space.EG
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.