Set runtimepath, adding a directory from an expression in vim?
Inn ~/script.vim
, I have:
set runtimepath+=string(substitute(expand("%:p"), 'script\.vim', '', 'g'))
I have an alias in .bashrc
:
alias vimscript="vim -S ~/script.vim"
Running string(subs开发者_JAVA百科titute(expand("%:p"), 'script\.vim', '', 'g'))
works as intended.
The problem is when using it in the set runtimepath expression, it doesn't work when I call vimscript
in terminal which calls script.vim
. When I run set rtp
in vim after being called by vimscript to check the runtimepath, the desired appended string isn't showed (but the other ones are there).
I have some additions to @Laurence Gonsalves answer:
There is also «concat and assign» operator:
.=
, solet foo=foo.bar
can be rewritten as
let foo.=bar
Code
let &runtimepath.=','.string(path)
will append
,'/some/path'
to &runtimepath, while you probably need,/some/path
.I guess that you want to append path to your script to runtimepath. If it is true, then your code should be written as
let &runtimepath.=','.escape(expand('<sfile>:p:h'), '\,')
inside a script, or
let &runtimepath.=','.escape(expand('%:p:h'), '\,')
from current editing session (assuming that you are editing your script in the current buffer).
The right hand site of a set
command is not an expression, it's a literal string.
You can manipulate options (the things set
sets) by using let
and prefixing the option name with an &
. eg:
let &runtimepath=substitute(expand("%:p"), 'script\.vim', '', 'g')
To append to runtimepath
with a let
you can do something like:
let &runtimepath=&runtimepath . ',' . substitute(expand("%:p"), 'script\.vim', '', 'g')
(The .
is the string concatenation operator.)
by 2022 i used this:
if ( isdirectory($some_shell_variable) )
set runtimepath+=$vi
endif
You can use a plain vim variable or whatever else just as well.
精彩评论