Skip to content

Instantly share code, notes, and snippets.

@atomize
Created November 15, 2017 17:07
Show Gist options
  • Save atomize/bca56a9a346a9f02b904622e04cf901d to your computer and use it in GitHub Desktop.
Save atomize/bca56a9a346a9f02b904622e04cf901d to your computer and use it in GitHub Desktop.
.vimrc function to run script in VIM and display output in split window
function! s:ExecuteInShell(command)
let command = join(map(split(a:command), 'expand(v:val)'))
let winnr = bufwinnr('^' . command . '$')
silent! execute ':w'
silent! execute winnr < 0 ? 'vnew ' . fnameescape(command) : winnr . 'wincmd w'
setlocal buftype=nowrite bufhidden=wipe nobuflisted noswapfile nowrap number
silent! execute 'silent %!'. command
silent! redraw
silent! execute 'au BufUnload <buffer> execute bufwinnr(' . bufnr('#') . ') . ''wincmd w'''
silent! execute 'nnoremap <silent> <buffer> <LocalLeader>r :call <SID>ExecuteInShell(''' . command . ''')<CR>'
silent! execute 'wincmd w'
" echo 'Shell command ' . command . ' executed.'
endfunction
command! -complete=shellcmd -nargs=+ Shell call s:ExecuteInShell(<q-args>)
cabbrev shell Shell
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment