Skip to content

Instantly share code, notes, and snippets.

@intuited
Created April 11, 2010 15:07
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save intuited/362802 to your computer and use it in GitHub Desktop.
Save intuited/362802 to your computer and use it in GitHub Desktop.
" Called with a command and a redirection target
" (see `:help redir` for info on redirection targets)
" Note that since this is executed in function context,
" in order to target a global variable for redirection you must prefix it with `g:`.
" EG call Redir('ls', '=>g:buffer_list')
funct! Redir(command, to)
exec 'redir '.a:to
exec a:command
redir END
endfunct
" The last non-space substring is passed as the redirection target.
" EG
" :R ls @">
" " stores the buffer list in the 'unnamed buffer'
" Redirections to variables or files will work,
" but there must not be a space between the redirection operator and the variable name.
" Also note that in order to redirect to a global variable you have to preface it with `g:`.
" EG
" :R ls =>g:buffer_list
" :R ls >buffer_list.txt
command! -nargs=+ R call call(function('Redir'), split(<q-args>, '\s\(\S\+\s*$\)\@='))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment