Vim-plug: On-demand loading of vim-fugitive

Created on 9 Feb 2015  路  10Comments  路  Source: junegunn/vim-plug

Using this command on my .vimrc:

Plug 'tpope/vim-fugitive', { 'on': 'Gstatus' }

will set it so that the fugitive plugin will load when I invoke that command, which, essential is akin to 'git status'

when I do that however, and run :Gstatus, vim says that it isn't a recognized command, which leads me to think that the plugin isn't loaded even when I go :Gstatus.

Any ideas?

(I'm pretty positive that it isn't a syntax error of any sorts, as i have successfully got CtrlP to do on demand loading)

question

Most helpful comment

Thanks to @blueyed's comment, here's how I lazy load fugitive with minimal code cruft:

Plug 'tpope/vim-fugitive', { 'on': [] }

command! Gstatus call LazyLoadFugitive('Gstatus')
command! Gdiff call LazyLoadFugitive('Gdiff')
command! Glog call LazyLoadFugitive('Glog')
command! Gblame call LazyLoadFugitive('Gblame')

function! LazyLoadFugitive(cmd)
  call plug#load('vim-fugitive')
  call fugitive#detect(expand('%:p'))
  exe a:cmd
endfunction

The LazyLoadFugitive function will be called only once before being replaced by the original.

All 10 comments

Are you in a git repo when you run :Gstatus?. If you aren't, vim will say Not an editor command.

I am for sure in a git repo

I can reproduce that, but I'm not sure what the issue is. I assume you got this message as well before the Not an editor command error:

Error detected while processing function <SNR>2_lod_cmd:

edit: I'm using nvim if that makes a difference.

Hey,

Yup, that's exactly what I'm getting.

You can't load vim-fugitive lazily without some massive hack. The reason is that vim-fugitive detect when you are in a git repo and only then define the commands. If you do Plug 'tpope/vim-fugitive', { 'on': 'Gstatus' }, when then you fire the :Gstatus command vim-plug load vim-fugitive and then tries to fire the real :Gstatus which do not exists because vim-fugitive couldn't do its check for the git repo and so it could not defines the commands. Bottom line, vim-fugitive is not designed to be loaded lazily, so you should not.

Aaaah, okay. That makes sense. Thanks for your clarifications.

@vheon Thanks for the explanation! :+1:

There is some way to do it, see: https://github.com/junegunn/vim-plug/issues/525#issuecomment-256169881.

Thanks to @blueyed's comment, here's how I lazy load fugitive with minimal code cruft:

Plug 'tpope/vim-fugitive', { 'on': [] }

command! Gstatus call LazyLoadFugitive('Gstatus')
command! Gdiff call LazyLoadFugitive('Gdiff')
command! Glog call LazyLoadFugitive('Glog')
command! Gblame call LazyLoadFugitive('Gblame')

function! LazyLoadFugitive(cmd)
  call plug#load('vim-fugitive')
  call fugitive#detect(expand('%:p'))
  exe a:cmd
endfunction

The LazyLoadFugitive function will be called only once before being replaced by the original.

@jeromedalbert,
It's not a big deal, but when you call one of these defined commands (Gstatus, Gdiff, Glof, GBlame), then those commands will be duplicated in suggested list in command line:
1603-gdiff

Was this page helpful?
0 / 5 - 0 ratings

Related issues

jdhao picture jdhao  路  11Comments

rosstimson picture rosstimson  路  16Comments

vicaya picture vicaya  路  23Comments

mcandre picture mcandre  路  8Comments

LemonBoy picture LemonBoy  路  8Comments