Run visual selection in the outer shell, send output to a new window

So I mainly use Vim to work on python at the moment, and I recently discovered two great strategies for running code from the outside and returning it to Vim. Firstly, using a function Shellfrom Vim pages :

function! s:ExecuteInShell(command)
  let command = join(map(split(a:command), 'expand(v:val)'))
  let winnr = bufwinnr('^' . command . '$')
  silent! execute  winnr < 0 ? 'botright new ' . fnameescape(command) : winnr . 'wincmd w'
  setlocal buftype=nowrite bufhidden=wipe nobuflisted noswapfile nowrap number
  echo 'Execute ' . command . '...'
  silent! execute 'silent %!'. command
  silent! execute 'resize ' . line('$')
  silent! redraw
  silent! execute 'au BufUnload <buffer> execute bufwinnr(' . bufnr('#') . ') . ''wincmd w'''
  silent! execute 'nnoremap <silent> <buffer> <LocalLeader>r :call <SID>ExecuteInShell(''' . command . ''')<CR>'
  echo 'Shell command ' . command . ' executed.'
endfunction
command! -complete=shellcmd -nargs=+ Shell call s:ExecuteInShell(<q-args>)

which allows you to run something like :Shell nosetestsand see the results in a new window:

  • Permanent (no "press enter" to make it disappear)
  • Uses a temporary buffer (not a temporary file)
  • And most importantly, re-executing the command simply updates the current window, it does not open every time every time.

Then I use this little stone:

:'<,'>:w !python

, .

, , , - . :

  • Shell,
  • .
  • EDIT: Python, bash. . , Shell $ python script.py, , , :'<,'>:w !python.

Vimscript, Shell, , , :'<,'>:w !python , . ? ?

+4
2

, :

function! s:ExecuteInShell(command) range
  let lines = []
  if (a:firstline != a:lastline)
    let lines=getline(a:firstline, a:lastline)
  endif
  let command = join(map(split(a:command), 'expand(v:val)'))
  let winnr = bufwinnr('^' . command . '$')
  silent! execute  winnr < 0 ? 'botright new ' . fnameescape(command) : winnr . 'wincmd w'
  setlocal buftype=nowrite bufhidden=wipe nobuflisted noswapfile nowrap number
  echo 'Execute ' . command . '...'
  if (len(lines))
    silent! call append(line('.'), lines)
    silent! 1d
    silent! redir => results
    silent! execute '1,$w !' . command
    silent! redir end
    silent! %d
    let lines = split(results, '\r')
    for line in lines[:1]
        call append(line('$'), line[1:])
    endfor
    silent! 1d
  else
    silent! execute 'silent %!'. command
  endif
  silent! execute 'resize ' . line('$')
  silent! redraw
  silent! execute 'au BufUnload <buffer> execute bufwinnr(' . bufnr('#') . ') . ''wincmd w'''
  silent! execute 'nnoremap <silent> <buffer> <LocalLeader>r :call <SID>ExecuteInShell(''' . command . ''')<CR>'
  echo 'Shell command ' . command . ' executed.'
endfunction
command! -range -complete=shellcmd -nargs=+ Shell <line1>,<line2>call s:ExecuteInShell(<q-args>)

:

:Shell echo 'no range supplied, but a command (echo) is.'

, ( "'<,' > " , ":", (, , ):

:'<,'>Shell python
+2

, :

function! s:ExecuteInShell(command) range
  let lines = []
  if (!len(a:command))
    let lines=getline(a:firstline, a:lastline)
  endif
  let command = join(map(split(a:command), 'expand(v:val)'))
  let winnr = bufwinnr('^' . command . '$')
  silent! execute  winnr < 0 ? 'botright new ' . fnameescape(command) : winnr . 'wincmd w'
  setlocal buftype=nowrite bufhidden=wipe nobuflisted noswapfile nowrap number
  echo 'Execute ' . command . '...'
  if (len(lines))
    for line in lines
        silent! execute 'silent r! '. line
    endfor
    silent! 1d
  else
    silent! execute 'silent %!'. command
  endif
  silent! execute 'resize ' . line('$')
  silent! redraw
  silent! execute 'au BufUnload <buffer> execute bufwinnr(' . bufnr('#') . ') . ''wincmd w'''
  silent! execute 'nnoremap <silent> <buffer> <LocalLeader>r :call <SID>ExecuteInShell(''' . command . ''')<CR>'
  echo 'Shell command ' . command . ' executed.'
endfunction
command! -range -complete=shellcmd -nargs=* Shell <line1>,<line2>call s:ExecuteInShell(<q-args>)

:

:Shell echo 'no range supplied, but a command (echo) is.'

, ( "'<,' > " , ":", ( , ):

:'<,'>Shell
0

All Articles