I use the same vimrc across many machines, some of which have fugitive.vim installed and some of which don't. I like including fugitive#statusline()
in my statusline, but on machines that don't have the plugin installed this raises an error.
Is there a way to check for this function's existence before calling set statusline
? I've tried using exists
y, but it doesn't work for some reason (load order?)
if exists("*fugitive#statusline")
set statusline=%<\ %f\ %{fugitive#statusline()} ... (other stuff)
endif
I've also tried silencing the error by prefixing the command with silent!
but that doesn't seem to work either.
This one would be a shortcut:
set statusline+=%{exists('g:loaded_fugitive')?fugitive#statusline():''}
As fugitive does not define fugitive#statusline
in an autoload
directory it sadly can not be sniffed use silent! call
/ exists
technique (Thank you @Christopher). There are however some alternatives:
'statusline'
option as @tungd suggested.'statusline'
in an after/plugin
file like @Christopher suggests. This solves the problem but means your statusline is defined in an rather unlikely place so it would probably be best to put in a nice comment in your ~/.vimrc
file.~/.vimrc
file.Example:
if !exists('*fugitive#statusline')
function! fugitive#statusline()
return ''
endfunction
endif
Fugitive
autocmd
event that Fugitive defines. Note that this will only fire when Fugitive detects a git directory.Put something like this in your ~/.vimrc
file:
augroup fugitive_status
autocmd!
autocmd user Fugitive set statusline=%<\ %f\ %{fugitive#statusline()} ...
augroup END
Personally I think @tungd solution is the simplest. However just define a dummy function would be my next choice. Fugitive will override it if Fugitive is installed. The best part is this keeps your 'statusline'
option neat and clean.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With