设置运行时路径,从 vim 中的表达式添加目录?

发布于 2024-10-10 01:11:47 字数 517 浏览 0 评论 0原文

Inn ~/script.vim,我有:

set runtimepath+=string(substitute(expand("%:p"), 'script\.vim', '', 'g'))

我在 .bashrc 中有一个别名:

alias vimscript="vim -S ~/script.vim"

运行 string(substitute(expand("%:p"), ' script\.vim', '', 'g')) 按预期工作。

问题是,当在设置的运行时路径表达式中使用它时,当我在调用 script.vim 的终端中调用 vimscript 时,它不起作用。当我在 vimscript 调用后在 vim 中运行 set rtp 来检查运行时路径时,未显示所需的附加字符串(但其他字符串都在那里)。

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(substitute(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).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

贱人配狗天长地久 2024-10-17 01:11:47

我对 @Laurence Gonsalves 的回答做了一些补充:

  1. 还有“concat and allocate”运算符:.=,所以

    let foo=foo.bar
    

    可以重写为

    let foo.=bar
    
  2. Code

    let &runtimepath.=','.string(path)
    

    会将 ,'/some/path' 附加到 &runtimepath,而您可能需要 ,/some/path

  3. 我猜您想将脚本的路径附加到运行时路径。如果这是真的,那么你的代码应该写成

    let &runtimepath.=','.escape(expand(':p:h'), '\,')
    

    在脚本内,或者

    let &runtimepath.=','.escape(expand('%:p:h'), '\,')
    

    来自当前编辑会话(假设您正在当前缓冲区中编辑脚本)。

I have some additions to @Laurence Gonsalves answer:

  1. There is also «concat and assign» operator: .=, so

    let foo=foo.bar
    

    can be rewritten as

    let foo.=bar
    
  2. Code

    let &runtimepath.=','.string(path)
    

    will append ,'/some/path' to &runtimepath, while you probably need ,/some/path.

  3. 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).

我偏爱纯白色 2024-10-17 01:11:47

set 命令的右侧位置不是表达式,而是文字字符串。

您可以使用 let 并在选项名称前添加 & 来操作选项(set 集的事物)。例如:

let &runtimepath=substitute(expand("%:p"), 'script\.vim', '', 'g')

要使用 let 附加到 runtimepath,您可以执行以下操作:(

let &runtimepath=&runtimepath . ',' . substitute(expand("%:p"), 'script\.vim', '', 'g')

. 是字符串连接运算符。)

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.)

九厘米的零° 2024-10-17 01:11:47

到 2022 年,我使用了这个:

if ( isdirectory($some_shell_variable)  )
    set runtimepath+=$vi
endif

您也可以使用普通的 vim 变量或其他任何变量。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文