3

I use tpope/vim-fugitive and airblade/vim-gitgutter and I want to selectively enable gitgutter only in git repositories.

Since vim-fugitive provides FugitiveIsGitDir which returns 0 or 1, this could be achieved as such (snippet from .vimrc):

call plug#begin()

Plug 'tpope/vim-fugitive'
Plug 'airblade/vim-gitgutter'

call plug#end()

let g:gitgutter_enabled = FugitiveIsGitDir()

However, I am getting

E117: Unknown function: FugitiveIsGitDir

since the plugin files of vim-fugitive are sourced after that line is executed. Here's an excerpt from :scriptnames:

  1: /etc/xdg/nvim/sysinit.vim
  2: /usr/share/nvim/archlinux.vim

  " here's where I define 
  " let g:gitgutter_enabled = FugitiveIsGitDir()
  3: ~/.config/nvim/init.vim

  4: ~/.local/share/nvim/site/autoload/plug.vim
  5: /usr/share/nvim/runtime/filetype.vim

  8: ~/.local/share/nvim/plugged/vim-fugitive/ftdetect/fugitive.vim
 
  " here's where FugitiveIsGitDir is sourced
 20: ~/.local/share/nvim/plugged/vim-fugitive/plugin/fugitive.vim

My question is: can I somehow make use of FugitiveIsGitDir() in this case? Maybe forcefully loading vim-fugitive earlier?

2 Answers 2

5

All directories in your Vim runtimepath can have a directory called after which can contain directories like plugin and ftplugin but get sourced after the regular ol' plugin and ftplugin directories are sourced.

# ~/.vim/after/plugin/enable_git_gutter.vim
let g:gitgutter_enabled = FugitiveIsGitDir()

See :help after-directory for more.

1
  • 1
    While it may work, I find changing a global option that is likely to be read when the plugin is started quite fishy. Instead, it's likely better (from an after plugin file) to execute a command or a function, instead of setting a enable global variable too late (as the plugin as already started doing its stuff). Aug 24, 2021 at 22:17
2

If you look at :h starting, you'll see that plugin files are sourced after .vimrc is entirely processed.

Functions defined in autoload plugin files could be used directly as long as the 'runtimepath' option as been updated (by plugin managers). However if you need to use a function defined in a plugin file, then you'll need to source that file explicitly as you've guessed.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.