Vim:加载插件后启动命令

3

使用 Vim,我可以在打开 Vim 时执行一个命令,例如:打开 Vim 并创建一个分屏。

vim +sp

我使用vim-fugitive插件,这是我的使用方式

vim +Gstatus

我明白了

E492: No es una orden del editor: Gstatus

可能因为当vim启动时,fugitive没有被加载 Gstatus

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

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

5个回答

5

关于您的一般问题的答案包含在: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文件中,它们将在所有插件加载后运行。更可靠的方法是使用VimEnter自动命令,正如@Peter Rincker在他的答案之后的评论中建议的那样。
对于您的特定问题,fugitive使用VimEnter自动命令,在其插件文件中定义:Gstatus和其他命令。如果您想自动执行:Gstatus,则应使用类似的自动命令,并确保它在fugitive之后定义,以便在fugitive之后执行。例如,将此行(未经测试)放入~/.vim/after/plugin/myfugitive.vim或其他位置。
:au VimEnter * if exists(':Gstatus') | Gstatus | endif

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


感谢您提供答案,它与Vim的vDebug完美地配合使用 :) - Benyamin Limanto

3

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

如果我在一个 Git 仓库中打开 vim,并且没有打开任何文件,那么我可以使用 Gstatus 命令。 - JuanPablo
这是因为Fugitive使用VimEnter自动命令来嗅探.git目录并可能附加Fugitive命令。 VimEnter在所有启动项(包括-c cmd)之后触发。您可以通过执行vim -c“autocmd VimEnter * Gstatus”来与此进行良好的互动。有关更多信息,请参见:h VimEnter - Peter Rincker
@PeterRincker:也许我们使用的是不同版本的fugitive插件,但我的版本使用BufNewFile,BufReadPost自动命令来定义:Gstatus和其他命令。在执行:verbose command Gstatus之后,我知道要查找fugitive/plugin/fugitive.vim文件。然后需要一些工作来确定顺序:自动命令调用s:Detect()函数来决定是否调用s:commands()函数,该函数定义了列表s:command中的所有命令。 - benjifisher
@benjifisher 它还通过VimEnter自动命令执行。请查看plugin/fugitive.vim文件。 - Peter Rincker
@PeterRincker:好的,我现在看到了。这是在没有指定文件名启动 vim 时触发的那个。 - benjifisher

1

在打开目标仓库的.git/index文件后,Fugitive会自动运行:Gstatus。因此,不用手动运行Gstatus命令,请使用以下命令:

vim .git/index

注意:
如果您想像OP建议的那样调用Gstatus$ vim +Gstatus),您可以将以下内容添加到您的vimrc中:

command Gstatus edit .git/index

然而,只有当您在git存储库的根目录中时才有效。
fugitive插件使用“command!”定义Gstatus命令。这意味着fugitive会默默地覆盖此命令定义。

1

你可以考虑设置别名,例如

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

这将打开 :Gstatus 而不需要文件(如 derenio 所建议),但是您不需要在 git 的根目录中才能使用此功能。

1
所有关于打开.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)\",如果您想完全具备未来性。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接