Skip to content

Instantly share code, notes, and snippets.

@romainl

romainl/redir.md

Last active Nov 11, 2020
Embed
What would you like to do?
Redirect the output of a Vim or external command into a scratch buffer

Redirect the output of a Vim or external command into a scratch buffer

Usage (any shell)

Show full output of command :hi in scratch window:

:Redir hi

Show full output of command :!ls -al in scratch window:

:Redir !ls -al 

Additional usage (depends on non-standard shell features so YMMV)

Evaluate current line with node and show full output in scratch window:

" current line
console.log(Math.random());

" Ex command
:.Redir !node

" scratch window
0.03987581000754448

Evaluate visual selection + positional parameters with bash and show full output in scratch window:

" content of buffer
echo ${1}
echo ${2}

" Ex command
:%Redir !bash -s foo bar

" scratch window
foo
bar

My Vim-related gists.

function! Redir(cmd, rng, start, end)
for win in range(1, winnr('$'))
if getwinvar(win, 'scratch')
execute win . 'windo close'
endif
endfor
if a:cmd =~ '^!'
let cmd = a:cmd =~' %'
\ ? matchstr(substitute(a:cmd, ' %', ' ' . expand('%:p'), ''), '^!\zs.*')
\ : matchstr(a:cmd, '^!\zs.*')
if a:rng == 0
let output = systemlist(cmd)
else
let joined_lines = join(getline(a:start, a:end), '\n')
let cleaned_lines = substitute(shellescape(joined_lines), "'\\\\''", "\\\\'", 'g')
let output = systemlist(cmd . " <<< $" . cleaned_lines)
endif
else
redir => output
execute a:cmd
redir END
let output = split(output, "\n")
endif
vnew
let w:scratch = 1
setlocal buftype=nofile bufhidden=wipe nobuflisted noswapfile
call setline(1, output)
endfunction
command! -nargs=1 -complete=command -bar -range Redir silent call Redir(<q-args>, <range>, <line1>, <line2>)
@dougpagani

This comment has been minimized.

Copy link

@dougpagani dougpagani commented Dec 24, 2017

Cool, thanks. Vim's internal pager is so unusable when trying to troubleshoot mappings, highlight groups, etc.

This was a very difficult problem to google, actually.

@romainl

This comment has been minimized.

Copy link
Owner Author

@romainl romainl commented Jan 20, 2018

@dougpagani, glad it helped.

@koepnick

This comment has been minimized.

Copy link

@koepnick koepnick commented May 14, 2018

Eighteen lines of code really should not bring me this much happiness. Thank you very much!

@george-b

This comment has been minimized.

Copy link

@george-b george-b commented Sep 2, 2018

The last revision appears to have changed the call from Redir to redir#Redir which is not a function defined in the snippet as is.

@romainl

This comment has been minimized.

Copy link
Owner Author

@romainl romainl commented Oct 12, 2018

Ooops, sorry for the copy-pasta.

@3N4N

This comment has been minimized.

Copy link

@3N4N 3N4N commented Apr 9, 2019

Hi, @romainl,

I learnt about :h execute() recently and noticed that it can be used instead of :h redir. Any thoughts?

" Redirect the output of a Vim or external command into a scratch buffer
function! Redir(cmd) abort
    let output = execute(a:cmd)
    tabnew
    setlocal nobuflisted buftype=nofile bufhidden=wipe noswapfile
    call setline(1, split(output, "\n"))
endfunction
command! -nargs=1 Redir silent call Redir(<f-args>)
@romainl

This comment has been minimized.

Copy link
Owner Author

@romainl romainl commented Apr 11, 2019

@3N4N I try to share portable snippets so :help execute() is too recent for me.

@097115

This comment has been minimized.

Copy link

@097115 097115 commented Jun 23, 2019

Checking for % my be of some interest, too:

[...]
if cmd =~ '^!'
    if cmd =~' %'
        let cmd = substitute(cmd, ' %', ' ' . expand('%:p'), '')
    endif
    let output = system(matchstr(cmd, '^!\zs.*'))
else
[...]
@romainl

This comment has been minimized.

Copy link
Owner Author

@romainl romainl commented Jun 23, 2019

@097115 good point. #, %< and friends should probably be transformed, too.

By the way, this was the very first gist notification I've ever get. Did they change something? Oh, right, there's an "Unsubscribe" button, now. Good.

@yangsibai

This comment has been minimized.

Copy link

@yangsibai yangsibai commented Sep 26, 2019

Checking for % my be of some interest, too:

[...]
if cmd =~ '^!'
    if cmd =~' %'
        let cmd = substitute(cmd, ' %', ' ' . expand('%:p'), '')
    endif
    let output = system(matchstr(cmd, '^!\zs.*'))
else
[...]

Can you paste the full script? Sorry, I can't execute if after insert your snippets. Thank you.

@romainl

This comment has been minimized.

Copy link
Owner Author

@romainl romainl commented Sep 26, 2019

Checking for % my be of some interest, too:

[...]
if cmd =~ '^!'
    if cmd =~' %'
        let cmd = substitute(cmd, ' %', ' ' . expand('%:p'), '')
    endif
    let output = system(matchstr(cmd, '^!\zs.*'))
else
[...]

Can you paste the full script? Sorry, I can't execute if after insert your snippets. Thank you.

097115's snippet incorrectly uses cmd instead of a:cmd. Check the main snippet for a working version.

@yangsibai

This comment has been minimized.

Copy link

@yangsibai yangsibai commented Sep 26, 2019

Checking for % my be of some interest, too:

[...]
if cmd =~ '^!'
    if cmd =~' %'
        let cmd = substitute(cmd, ' %', ' ' . expand('%:p'), '')
    endif
    let output = system(matchstr(cmd, '^!\zs.*'))
else
[...]

Can you paste the full script? Sorry, I can't execute if after insert your snippets. Thank you.

097115's snippet incorrectly uses cmd instead of a:cmd. Check the main snippet for a working version.

It works! Thank you for this. It has wasted me several hours.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.