Reputation: 24754
with vim, I can launch a command when vim is open, example: open vim and create a split
vim +sp
I use vim-fugitive plugin, is I use
vim +Gstatus
I get
E492: No es una orden del editor: Gstatus
maybe because fugitive not are loaded when vim launch Gstatus
when I launch the vim from terminal, how I can execute a command after the load of plugins ?
In particular, How I can launch vim from terminal with Gstatus
preloaded.
Upvotes: 3
Views: 3722
Reputation: 1201
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. Technically the correctest invocation is:
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:
[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. 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. 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.
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.
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.
Upvotes: 1
Reputation: 11
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.
Upvotes: 1
Reputation: 2703
Fugitive automatically runs :Gstatus
after opening the .git/index
file of your target repository. Instead of trying to manually run Gstatus
, use this command:
vim .git/index
Note:
If you like to invoke Gstatus
as the OP suggested ($ vim +Gstatus
), you can add following to your vimrc
:
command Gstatus edit .git/index
However, this works only if you are in the root directory of your git repository.
The fugitive plugin defines the Gstatus
command with command!
. This means fugitive silently overwrites this command definition.
Upvotes: 1
Reputation: 45087
:Gstatus
is a buffer specific command. 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
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)
Upvotes: 3
Reputation: 5112
The answer your general question is contained in :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. More reliable, as @Peter Rincker suggested in the comments after his answer, is to use a VimEnter
autocommand.
For your specific question, fugitive uses a VimEnter
autocommand, defined in its plugin file, to define :Gstatus
and other commands. 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. For example, put this line (untested) in ~/.vim/after/plugin/myfugitive.vim
or some such:
:au VimEnter * if exists(':Gstatus') | Gstatus | endif
That will test whether the command has been defined; if so, it will invoke the command.
Upvotes: 5