How to execute file I'm editing in Vi(m)

后端 未结 13 1861
悲哀的现实
悲哀的现实 2020-12-04 05:45

How to execute file that I\'m editing in Vi(m) and get output in split window (like in SciTE)?

Of course I could execute it like that:

:!scriptname
<         


        
13条回答
  •  醉话见心
    2020-12-04 06:14

    In your .vimrc you can paste this function

    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  execute bufwinnr(' . bufnr('#') . ') . ''wincmd w'''
      silent! execute 'nnoremap   r :call ExecuteInShell(''' . command . ''')'
      silent! execute 'wincmd w'
      " echo 'Shell command ' . command . ' executed.'
    endfunction
    command! -complete=shellcmd -nargs=+ Shell call s:ExecuteInShell()
    cabbrev shell Shell
    

    After that, in vim run command :shell python ~/p.py as example. And you will get the output in splitted window. + After changes in p.py as example you will run the same command again, this function will not create new window again, it will display the result in the previous(same) splitted window.

提交回复
热议问题