我使用vim作为编写shell (bash)和Python程序的编辑器。通常,我需要将不同的参数传递给我开发的程序,以便以不同的组合进行测试/调试。对于C源代码,Makefile负责构建和运行程序,因此可以在下一个选项卡中对其进行编辑,每次我按下执行“make run”的F9时,它都会将新参数传递给程序。脚本没有Makefile这样的东西,所以我必须在Konsole的下一个选项卡中手动运行它们,更改参数,这似乎效率很低。
以Python为例,我在.vimrc中有以下设置:
autocmd FileType python call Python_source()
func! Python_source()
setlocal number cursorline
setlocal shiftwidth=2
setlocal foldmethod=indent
map <F9> :w \| :!python %<CR>
imap <F9> <Esc> :w \| :!python %<CR>
...
endfunc有没有办法将参数存储在脚本源代码中(例如,在注释中),然后将其作为参数传递给脚本,如下所示:
#vimparameter='-f -a --bus 1'在.vimrc中:
map <F9> :w \| :!python % $vimparameter<CR>
imap <F9> <Esc> :w \| :!python % $vimparameter<CR>或者其他任何合理的方法来方便地更改参数,然后将参数传递给脚本,由F9快捷方式执行?
正如我的@Matt所建议的,modelines可以用来将一些预定义的行作为参数传递给命令:
autocmd FileType python call Python_source()
...
func! LWargs()
set lw=''
doautocmd BufRead
if len(&lw) > 0 && len(&lw) < 512
return ' ' . &lw
endif
return ''
endfunc
func! Python_source()
...
map <F9> :w \| :exe '!python' '%:p' . LWargs()<CR>
imap <F9> <Esc> :w \| :exe '!python' '%:p' . LWargs()<CR>
...
endfunc然后,在源代码中,可以使用以下命令预定义参数:
# vim: lw=--bus\ 10\ -f应设置“‘modeline”。
发布于 2019-06-05 12:40:55
你可以使用"modeline“来设置一些通常不使用的选项。“‘lispwords”看起来还不错(除非你是LISP程序员):它是一个缓冲区本地字符串,只用于lisp缩进。所以你可以在python中这样做:
# vim: lw=-f\ -a\ --bus\ 1
import sys
print(sys.argv)在vimscript中类似于:
update
"ensure modeline is re-read
doautocmd BufRead
let l:args = (len(&lw) < 500) ? &lw : ''
exe '!python' shellescape(expand('%:p')) l:argshttps://stackoverflow.com/questions/56451719
复制相似问题