我的vimrc中有这些行:
let basepath=','.'~/.vim/plugged/'
" theme plugins
let &runtimepath.=basepath . 'morhetz/gruvbox'
let &runtimepath.=basepath . 'itchyny/lightline.vim'
let &runtimepath.=basepath . 'edkolev/tmuxline.vim'
有没有办法编写封装此行为的函数?我想要这样的事情:
loadplugin('somebody/plugin.vim')
这与此行具有相同的效果:
let &runtimepath.=basepath . 'somebody/plugin.vim'
答案 0 :(得分:1)
对于常见情况,即当您不关心不存在的路径时,您可以简单地执行
exe 'set rtp+='.basepath.a:plugin
对于类似的路径处理方式,我提出a function只会添加路径(如果尚未存在),并且仅在路径存在的情况下才会添加。
" autoload/lh/path.vim
" Function: lh#path#munge(pathlist, path) {{{3
function! lh#path#munge(pathlist, path) abort
if type(a:pathlist) == type('str')
let pathlist = split(a:pathlist, ',')
return join(lh#path#munge(pathlist, a:path), ',')
else
if ! empty(glob(a:path))
call lh#list#push_if_new(a:pathlist, a:path)
endif
return a:pathlist
endif
endfunction
" autoload/lh/list.vim
" Function: lh#list#push_if_new(list, value) {{{3
function! lh#list#push_if_new(list, value) abort
if index(a:list, a:value) < 0
call add (a:list, a:value)
endif
return a:list
endfunction
答案 1 :(得分:0)
在这里,我找到了答案:
function LoadPlugin(plugin)
let basepath=','.'~/.vim/plugged/'
let &runtimepath.=basepath . a:plugin
endfunction
"invoke this way
call LoadPlugin('somebody/plugin.vim')