简体   繁体   English

vim:加载插件后启动命令

[英]vim: launch command after load plugins

with vim, I can launch a command when vim is open, example: open vim and create a split 使用vim,我可以在vim打开时启动一个命令,例如:打开vim并创建一个分割

vim +sp

I use vim-fugitive plugin, is I use 我使用vim-fugitive插件,是我用的

vim +Gstatus

I get 我明白了

E492: No es una orden del editor: Gstatus

maybe because fugitive not are loaded when vim launch Gstatus 也许是因为当vim启动Gstatus时,没有加载逃犯

when I launch the vim from terminal, how I can execute a command after the load of plugins ? 当我从终端启动vim时,如何在加载插件后执行命令?

In particular, How I can launch vim from terminal with Gstatus preloaded. 特别是,如何从Gstatus预装的终端启动vim。

:Gstatus is a buffer specific command. :Gstatus是一个特定于缓冲区的命令。 So the command will not exist unless you open a file in the repo. 因此,除非您在仓库中打开文件,否则该命令将不存在。 Read more here: :h :command-buffer and the first paragraph here :h fugitive-commands 在这里阅读更多:h :command-buffer和第一段:h fugitive-commands

Examples: 例子:

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)

The answer your general question is contained in :help startup . 您的一般问题的答案包含在:help startup Here are some relevant parts: 以下是一些相关部分:

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.

It is sort of a cheat, and will not work with vim in a terminal, but you can put commands in your gvimrc file and they will be run after all the plugins are loaded. 它有点像作弊,并且不能在终端中使用vim,但你可以将命令放在你的gvimrc文件中,它们将在加载所有插件后运行。 More reliable, as @Peter Rincker suggested in the comments after his answer, is to use a VimEnter autocommand. 更可靠,正如@Peter Rincker在回答后的评论中所建议的那样,是使用VimEnter自动命令。

For your specific question, fugitive uses a VimEnter autocommand, defined in its plugin file, to define :Gstatus and other commands. 为了您的具体问题,逃犯采用了VimEnter自动命令,在它的插件文件中定义,定义:Gstatus等命令。 If you want to do :Gstatus automatically, you should use a similar autocommand and make sure it is defined after fugitive's, so that yours will be executed after fugitive's. 如果你想自动执行:Gstatus ,你应该使用类似的自动命令,并确保它在逃犯后定义,以便你的将在逃犯后执行。 For example, put this line (untested) in ~/.vim/after/plugin/myfugitive.vim or some such: 例如,将此行(未经测试)放在~/.vim/after/plugin/myfugitive.vim或其中一些内容中:

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

That will test whether the command has been defined; 这将测试命令是否已定义; if so, it will invoke the command. 如果是这样,它将调用该命令。

Fugitive automatically runs :Gstatus after opening the .git/index file of your target repository. 在打开目标存储库的.git/index文件后,Fugitive会自动运行:Gstatus Instead of trying to manually run Gstatus , use this command: 而不是尝试手动运行Gstatus ,使用此命令:

vim .git/index

Note: 注意:
If you like to invoke Gstatus as the OP suggested ( $ vim +Gstatus ), you can add following to your vimrc : 如果你Gstatus OP建议的那样调用Gstatus$ vim +Gstatus ),你可以在你的vimrc添加以下内容:

command Gstatus edit .git/index

However , this works only if you are in the root directory of your git repository. 但是 ,仅当您位于git存储库的根目录中时,此方法才有效。

The fugitive plugin defines the Gstatus command with command! 逃逸插件使用command!定义Gstatus command! . This means fugitive silently overwrites this command definition. 这意味着逃犯会无声地覆盖此命令定义。

One thing you could consider is setting up an alias such as 你可以考虑的一件事是建立一个别名,如

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

this will open :Gstatus without a file (as suggested by derenio) however you do not need to be in git's root directory for this to work. 这将打开:没有文件的Gstatus(由derenio建议)但是你不需要在git的根目录中工作。

All of the suggestions regarding opening the .git/index file are essentially correct, but one needs to be careful regarding the actual location of that file. 关于打开.git/index文件的所有建议都是正确的,但是需要注意该文件的实际位置。 Technically the correctest invocation is: 从技术上讲,correctest调用是:

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

A sufficient invocation is: 足够的调用是:

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

This properly accounts for things like worktrees where the index is stored in a subdirectory of the root repository (not in the worktree). 这适当地考虑了诸如工作树之类的事情,其中​​索引存储在根存储库的子目录中(不在工作树中)。

My preferred method of doing this is through a git alias defined as follows: 我这样做的首选方法是通过如下定义的git别名:

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

The purpose of the cd ${GIT_PREFIX} is because aliases are always run from the base of the repository, so this ensures we return to the directory we called it from. cd ${GIT_PREFIX}的目的是因为别名总是从存储库的基础运行,所以这确保我们返回到我们从中调用它的目录。 The ped ${GIT_DIR}/index loads the index in the preview window (which is what :Gstatus does), and the winc P sets focus to the preview window. ped ${GIT_DIR}/index在预览窗口中加载索引(这就是:Gstatus所做的), winc P将焦点设置到预览窗口。 The setl fdl=1 is just to undo folds because I have them enabled by default but don't want them in the status window. setl fdl=1只是为了撤消折叠,因为我默认启用它们但不希望它们在状态窗口中。

The ${1:+'winc p'} $* means that any arguments passed to git vim are then also passed to vim , and I am assuming that if there are arguments one of them is likely to be a file that we want to interact with, so the winc p returns focus to the previous window (the first loaded file) only if there are arguments. ${1:+'winc p'} $*表示传递给git vim任何参数都会传递给vim ,我假设如果有参数,其中一个可能是我们想要的文件与之交互,因此只有存在参数时, winc p将焦点返回到上一个窗口(第一个加载的文件)。

Note that you could replace the '+ped ${GIT_DIR}/index' with \\"+ped $(git rev-parse --git-path index)\\" if you want to be completely future proof. 请注意,如果您想要完全面向未来,可以将'+ped ${GIT_DIR}/index'替换为\\"+ped $(git rev-parse --git-path index)\\"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM