【问题标题】:vim: launch command after load pluginsvim:加载插件后启动命令
【发布时间】:2014-04-20 00:41:09
【问题描述】:

使用 vim,我可以在打开 vim 时启动命令,例如:打开 vim 并创建一个拆分

vim +sp

我用vim-fugitive插件,我用吗

vim +Gstatus

我明白了

E492: No es una orden del editor: Gstatus

可能是因为 vim 启动时没有加载逃犯Gstatus

当我从终端启动 vim 时,如何在加载插件后执行命令?

特别是,我如何从预加载 Gstatus 的终端启动 vim。

【问题讨论】:

    标签: vim vim-fugitive


    【解决方案1】:

    您的一般问题的答案包含在:help startup 中。以下是一些相关部分:

    3. Execute Ex commands, from environment variables and/or files
    ...
          *VIMINIT* *.vimrc* *_vimrc* *EXINIT* *.exrc* *_exrc* *$MYVIMRC*
         c. Four places are searched for initializations.  The first that exists
        is used, the others are ignored.  ...
        -  The user vimrc file(s):
                "$HOME/.vimrc"  (for Unix and OS/2) (*)
    ...
                "$HOME/_vimrc"  (for MS-DOS and Win32) (*)
                "$VIM/_vimrc"   (for MS-DOS and Win32) (*)
    ...
    4. Load the plugin scripts.                 *load-plugins*
        This does the same as the command: >
            :runtime! plugin/**/*.vim
    ...
    8. Perform GUI initializations
        Only when starting "gvim", the GUI initializations will be done.  See
        |gui-init|.
    ...
    12. Execute startup commands
        If a "-t" flag was given to Vim, the tag is jumped to.
        The commands given with the |-c| and |+cmd| arguments are executed.
        The starting flag is reset, has("vim_starting") will now return zero.
        If the 'insertmode' option is set, Insert mode is entered.
        The |VimEnter| autocommands are executed.
    

    这是一种作弊,在终端中不能与 vim 一起使用,但你可以将命令放在你的 gvimrc 文件中,它们将在所有插件加载后运行。正如@Peter Rincker 在回答后在 cmets 中建议的那样,更可靠的是使用 VimEnter 自动命令。

    对于您的具体问题,fugitive 使用在其插件文件中定义的VimEnter 自动命令来定义:Gstatus 和其他命令。如果你想自动执行:Gstatus,你应该使用类似的自动命令,并确保它是在逃犯之后定义的,这样你的就会在逃犯之后执行。例如,将此行(未经测试)放入~/.vim/after/plugin/myfugitive.vim 或类似的:

    :au VimEnter * if exists(':Gstatus') | Gstatus | endif
    

    这将测试命令是否已定义;如果是,它将调用该命令。

    【讨论】:

    • 感谢您提供答案,它可以完美地使用 vDebug for Vim :)
    【解决方案2】:

    :Gstatus 是一个特定于缓冲区的命令。因此,除非您在存储库中打开文件,否则该命令将不存在。在此处阅读更多内容::h :command-buffer 和此处的第一段 :h fugitive-commands

    例子:

    vim -c Gstatus <filename>  # -c "cmd" will be executed after the first file has been read.
    vim +Gstatus <filename>    # +command is a shortcut for `-c command`
    vim .git/index             # opens :Gstatus without a file (answer by derenio)
    

    【讨论】:

    • 如果我在没有文件的情况下打开vim(在 git repo 中),我可以使用Gstatus 命令。
    • 这是因为 fugitive 使用 VimEnter 自动命令来嗅探.git 目录并可能附加 fugitive 命令。 VimEnter 在包括-c cmd 在内的所有启动内容之后触发。您可以通过 vim -c "autocmd VimEnter * Gstatus" 很好地使用此功能,请参阅 :h VimEnter 了解更多信息。
    • @PeterRincker:也许我们有不同版本的逃犯插件,但我的使用BufNewFile,BufReadPost 自动命令来定义:Gstatus 和其他命令。在:verbose command Gstatus 之后,我知道要查看fugitive/plugin/fugitive.vim。然后花了一些工作来弄清楚顺序:自动命令调用s:Detect()决定是否调用s:commands(),它定义了列表s:command中的所有命令。
    • @benjifisher 它也通过VimEnter autocmd 执行。看里面plugin/fugitive.vim
    • @PeterRincker:好的,我现在明白了,当你在没有文件名的情况下启动 vim 时会触发。
    【解决方案3】:

    Fugitive 在打开目标存储库的.git/index 文件后自动运行:Gstatus。不要尝试手动运行Gstatus,而是使用以下命令:

    vim .git/index
    

    注意:
    如果您想按照 OP 的建议 ($ vim +Gstatus) 调用 Gstatus,您可以在您的 vimrc 中添加以下内容:

    command Gstatus edit .git/index
    

    但是,这仅在您位于 git 存储库的根目录中时才有效。

    逃犯插件用command!定义了Gstatus命令。这意味着逃犯默默地覆盖了这个命令定义。

    【讨论】:

      【解决方案4】:

      您可以考虑设置一个别名,例如

      alias gst='vim $(git rev-parse --show-toplevel)/.git/index'
      

      这将在没有文件的情况下打开 :Gstatus(如 derenio 建议的那样),但是您无需位于 git 的根目录中即可。

      【讨论】:

        【解决方案5】:

        关于打开.git/index 文件的所有建议基本上都是正确的,但需要注意该文件的实际位置。从技术上讲,最正确的调用是:

        alias gst='vim $(git rev-parse --git-path index)'
        

        充分的调用是:

        alias gst='vim $(git rev-parse --git-dir)/index'
        

        这正确解释了诸如索引存储在根存储库的子目录中(而不是在工作树中)的工作树。

        我首选的方法是通过定义如下的git 别名:

        [alias]
            vim = "!_(){ cd ${GIT_PREFIX}; \
                vim '+ped ${GIT_DIR}/index' '+winc P' '+setl fdl=1' ${1:+'+winc p'} $* \
            ;};_"
        

        cd ${GIT_PREFIX} 的目的是因为别名总是从存储库的基础运行,所以这确保我们返回到我们调用它的目录。 ped ${GIT_DIR}/index 在预览窗口中加载索引(这是:Gstatus 所做的),winc P 将焦点设置到预览窗口。 setl fdl=1 只是为了撤消折叠,因为我默认启用了它们,但不希望它们出现在状态窗口中。

        ${1:+'winc p'} $* 意味着传递给git vim 的任何参数随后也会传递给vim,我假设如果有参数,其中一个很可能是我们想要与之交互的文件,所以winc p 只有在有参数的情况下才会将焦点返回到前一个窗口(第一个加载的文件)。

        请注意,如果您想完全面向未来,可以将 '+ped ${GIT_DIR}/index' 替换为 \"+ped $(git rev-parse --git-path index)\"

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2013-01-18
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2020-11-24
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多