From the gist "Redirect the output of a Vim or external command into a scratch buffer" by romainl:
function! Redir(cmd)
for win in range(1, winnr('$'))
if getwinvar(win, 'scratch')
execute win . 'windo close'
endif
endfor
if a:cmd =~ '^!'
execute "let output = system('" . substitute(a:cmd, '^!', '', '') . "')"
else
redir => output
execute a:cmd
redir END
endif
vnew
let w:scratch = 1
setlocal nobuflisted buftype=nofile bufhidden=wipe noswapfile
call setline(1, split(output, "\n"))
endfunction
command! -nargs=1 Redir silent call Redir(<f-args>)
" Usage:
" :Redir hi ............. show the full output of command ':hi' in a scratch window
" :Redir !ls -al ........ show the full output of command ':!ls -al' in a scratch window
It captures the result with execute "let output = system()"
and writes it to a new split. Untested:
:Redir !petit --wordcount %
%
is replaced with the current file name, see :help c_%
.