所以我现在主要使用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代码执行此操作,而不是bash代码.该函数已经执行常规shell命令.我没有使用Shell来运行$python script.py,而是希望它直接运行代码:’<,’>:w!python会.
我不知道足够的Vimscript修改Shell以包含一个选择,我不能为我的生活弄清楚如何至少把:’<,’>:w!python放入它自己的窗口而不是使用临时文件,这对我来说似乎没用.有任何想法吗?提示?
最佳答案 您可以使函数接受范围并测试范围是否通过:
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