Vim fugitive autocmd not added

Viewed 432

I'm starting to use Vim 8 and work on setting up my .vimrc. I installed vim-fugitive using vim-plug. I can use the commands provided by the plugin. My problem is that I want to define custom commands to serve as git aliases. I followed this issue: https://github.com/tpope/vim-fugitive/issues/72 but when I try to use the command added by my autocmd call I get an error: not an editor command. I understood that it could be a timing problem between the plugin load and the command declaration but I wrote my autocmd after the plugin load by vim-plug. Here is an extract of my .vimrc:

" Installing/loading plugins
call plug#begin('~/.vim/plugged')
    [...]
    Plug 'tpope/vim-fugitive'
call plug#end()

" #########################################################
" #
" #                      Fugitive
" #
" #########################################################
augroup gitAlias
    " Remove all gitAlias command
    " Needed because they are added every time otherwise
    autocmd!
    autocmd User fugitive command! -nargs=0 Gpr :G pull --rebase
augroup END

If I run :command! -nargs=0 Gpr :G pull --rebase while in Vim, the custom command works. Perhaps the problem comes from the autocmd event but I can't find a way to make sure the event exists.

1 Answers

After discussing with Romainl the commands now work for me. It was perhaps a combination of a missing capital letter on the command event and that I was not testing at the right place.

If you look at the code here: https://github.com/tpope/vim-fugitive/blob/3eb6f316c09553989e59bb3802da100a6fb7c091/plugin/fugitive.vim#L362. The FugitiveDetect function is called only on BufNewFile,BufReadPost events. This means the commands are available only when editing an existing file or a new one in a git repo. I initially thought the command where always available.

Thanks Romainl for your help.

Related