Skip to content

Instantly share code, notes, and snippets.

@romainl romainl/redir.vim
Last active Sep 27, 2019

Embed
What would you like to do?
Redirect the output of a Vim or external command into a scratch buffer
function! Redir(cmd)
for win in range(1, winnr('$'))
if getwinvar(win, 'scratch')
execute win . 'windo close'
endif
endfor
if a:cmd =~ '^!'
let cmd = a:cmd =~' %' ? substitute(a:cmd, ' %', ' ' . expand('%:p'), '') : a:cmd
let output = system(matchstr(cmd, '^!\zs.*'))
else
redir => output
execute a:cmd
redir END
endif
vnew
let w:scratch = 1
setlocal buftype=nofile bufhidden=wipe nobuflisted noswapfile
call setline(1, split(output, "\n"))
endfunction
command! -nargs=1 -complete=command Redir silent call Redir(<q-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
@dougpagani

This comment has been minimized.

Copy link

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

commented Jan 20, 2018

@dougpagani, glad it helped.

@koepnick

This comment has been minimized.

Copy link

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

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

commented Oct 12, 2018

Ooops, sorry for the copy-pasta.

@3N4N

This comment has been minimized.

Copy link

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

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

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

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

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

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

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.