0

I've established that it's possible to take the contents of a buffer (even one that isn't saved anywhere) and pipe it to a command.

like so: :w ! petit --wordcount

But! Is it possible to take the output of said command and pipe it back out to a new empty buffer in vim, without writing the output to a file...just writing back into a new buffer...using ex?

1

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_%.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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