所以我现在主要使用Vim在python中工作,我最近发现了两个很好的策略,用于在外部运行代码并将其带回Vim。第一个是使用Vim pages中的Shell
函数:
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
的内容,并在新窗口中查看结果:
然后我也使用this小宝石:
:'<,'>:w !python
让我使用当前缓冲区中的一个选项,但在输入后会消失。
我无法弄清楚怎么做就是将两者结合起来。我想要的是:
Shell
命令的所有窗口属性,但$ python script.py
,而是希望它像:'<,'>:w !python
那样直接运行代码。 我不知道足够的Vimscript来修改Shell
以包含一个选择,我不能在我的生活中弄清楚如何至少将:'<,'>:w !python
放入它自己的窗口而不是使用临时文件,这对我来说似乎没用。有任何想法吗?提示?
答案 0 :(得分: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.'
要在选择行时使用(不要键入“'&lt;,'&gt;”部分,因为按“:”会将其放在那里(作为所选行提供的命令由命令解释) :
:'<,'>Shell python
答案 1 :(得分:0)
这是一个使用缺少命令来处理一系列行的版本:
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.'
要在选择行时使用(不要键入“'&lt;,'&gt;”部分,因为按“:”会将其放在那里(没有提供命令,因为所选行提供命令):
:'<,'>Shell