Skip to content

Instantly share code, notes, and snippets.

@tyru
Created April 16, 2010 11:01
Show Gist options
  • Save tyru/368306 to your computer and use it in GitHub Desktop.
Save tyru/368306 to your computer and use it in GitHub Desktop.
:Capture - Capture output of ex command.
" Capture {{{
function! s:get_output(...) "{{{
" a:000 = [['echo', '"foo"'], 'messages', ...]
"
" TODO nested :redir ?
redir => output
for cmd_args in a:000
if type(cmd_args) == type([])
silent execute join(cmd_args)
else
silent execute cmd_args
endif
endfor
redir END
return substitute(output, '^\n\n', '', '')
endfunction "}}}
AlterCommand cap[ture] Capture
command!
\ -nargs=+ -complete=command
\ Capture
\ call s:cmd_capture(<q-args>)
function! s:cmd_capture(q_args) "{{{
let out = s:get_output(a:q_args) " NOTE: Execute ex command in current buffer.
New " Change as you like. for e.g., :new instead.
silent file `=printf('[Capture: %s]', a:q_args)`
setlocal buftype=nofile bufhidden=unload noswapfile nobuflisted
call setline(1, split(out, '\n'))
endfunction "}}}
" }}}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment