【问题标题】:How to run vimscript from bash, and get the output in a bash script?如何从 bash 运行 vimscript,并在 bash 脚本中获取输出?
【发布时间】:2014-12-18 16:57:45
【问题描述】:

我想从命令行运行一些 vimscript。具体来说,我想检查一下我的 vim 实例上是否已经安装了 Command-T(如果没有,我想安装它)。

我可以在 vim 中运行一些命令来检测 Command-T 的存在,例如exists("CommandTFlush") 如果存在则返回 0,如果不存在则返回 2。

如何从 bash 脚本调用此函数,并在 bash 中解释结果?

【问题讨论】:

    标签: bash automation vim


    【解决方案1】:

    我最终使用了以下命令。由于 vim 使用 ncurses,您无法回显任何内容,但您可以读取 vim 进程的返回码。如果你用:cquit退出vim,vim会以返回码1退出,否则会以0退出。我们可以用它来判断函数的成功/失败。

    将以下内容添加到文件中并保存为check-command-t.vim

    :let cmdt = exists("CommandTFlush")
    :if cmdt == 2
        " It's not installed.
        :cquit
    :else
        :exit
    :endif
    

    在 bash 中,我们想使用 vim -s check-command-t.vim 告诉 vim 运行文件中的所有命令。

    if vim -s check-command-t.vim; then
        # The command exited with return code 0; command-T is installed
    else
        # not installed
    fi
    

    【讨论】:

    • 你不需要在Vimscript中的命令前加上:,规范的文件扩展名是.vim,而不是.vs
    猜你喜欢
    • 2021-07-13
    • 2020-05-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-10-20
    • 2023-04-07
    • 2022-11-25
    相关资源
    最近更新 更多