所以我目前主要使用 Vim 在 python 中工作,我最近发现了 2 个很好的策略,可以在外部运行代码并将其带回 Vim。第一个是使用Shell
从Vim 页面调用的函数:
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>)
它允许一个人运行类似的东西:Shell nosetests
并在一个新窗口中查看结果:
- 持久(没有“点击进入”让它消失)
- 使用临时缓冲区(不是临时文件)
- 最重要的是,再次运行该命令只会刷新当前窗口,它不会每次都打开一个新窗口。
然后我也使用这个小宝石:
:'<,'>:w !python
这让我使用我当前缓冲区中的一个选择,但在按下回车后它就消失了。
我不知道该怎么做是将两者结合起来。我想要的是:
- 该命令的所有窗口属性
Shell
,但 - 从屏幕上的选择运行它的能力。
- 编辑:对选定的 python 代码执行此操作,而不是 bash 代码。该函数已经执行了常规的 shell 命令。我不想使用 Shell 来运行,
$ python script.py
而是希望它直接运行代码:'<,'>:w !python
。
我不知道足够的 Vimscript 来修改Shell
以包含选择,而且我一生都无法弄清楚如何在:'<,'>:w !python
不使用临时文件的情况下至少将其放入它自己的窗口中,这对我来说似乎没有必要。有任何想法吗?提示?