bash 设置运行时路径,从vim中的表达式添加目录?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4597919/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Set runtimepath, adding a directory from an expression in vim?
提问by Somebody still uses you MS-DOS
Inn ~/script.vim, I have:
客栈~/script.vim,我有:
set runtimepath+=string(substitute(expand("%:p"), 'script\.vim', '', 'g'))
I have an alias in .bashrc:
我有一个别名.bashrc:
alias vimscript="vim -S ~/script.vim"
Running string(substitute(expand("%:p"), 'script\.vim', '', 'g'))works as intended.
运行string(substitute(expand("%:p"), 'script\.vim', '', 'g'))按预期工作。
The problem is when using it in the set runtimepath expression, it doesn't work when I call vimscriptin terminal which calls script.vim. When I run set rtpin vim after being called by vimscript to check the runtimepath, the desired appended string isn't showed (but the other ones are there).
问题是在 set runtimepath 表达式中使用它时,当我调用vimscript调用script.vim. 当我set rtp在被 vimscript 调用以检查运行时路径后在 vim 中运行时,未显示所需的附加字符串(但其他字符串在那里)。
回答by ZyX
I have some additions to @Laurence Gonsalves answer:
我对@Laurence Gonsalves 的回答有一些补充:
There is also ?concat and assign? operator:
.=, solet foo=foo.barcan be rewritten as
let foo.=barCode
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).
还有?concat 和assign?运营商:
.=,所以let foo=foo.bar可以改写为
let foo.=bar代码
let &runtimepath.=','.string(path)将附加
,'/some/path'到 &runtimepath,而您可能需要,/some/path.我猜您想将脚本的路径附加到运行时路径。如果是真的,那么你的代码应该写成
let &runtimepath.=','.escape(expand('<sfile>:p:h'), '\,')在脚本中,或
let &runtimepath.=','.escape(expand('%:p:h'), '\,')从当前编辑会话(假设您正在当前缓冲区中编辑脚本)。
回答by Laurence Gonsalves
The right hand site of a setcommand is not an expression, it's a literal string.
set命令的右侧位置不是表达式,而是文字字符串。
You can manipulate options (the things setsets) by using letand prefixing the option name with an &. eg:
您可以set通过使用let选项名称并在其前面加上前缀来操作选项(事物集)&。例如:
let &runtimepath=substitute(expand("%:p"), 'script\.vim', '', 'g')
To append to runtimepathwith a letyou can do something like:
要附加到runtimepathalet您可以执行以下操作:
let &runtimepath=&runtimepath . ',' . substitute(expand("%:p"), 'script\.vim', '', 'g')
(The .is the string concatenation operator.)
(这.是字符串连接运算符。)

