Reputation: 10076
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.
Upvotes: 7
Views: 8263
Reputation: 11
In your vimrc or init.vim file
set statusline=%F%r%h%w%=(%{&ff}/%Y)\ (line\ %l\/%L,\ col\ %c)\
if exists("*fugitive#statusline")
set statusline+=%{fugitive#statusline()}
endif
Upvotes: 1
Reputation: 45147
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.
Upvotes: 8
Reputation: 1851
I think I have worked out how to achieve this. The check for existence of fugitive#statusline()
needs to happen after the plugins are loaded. Until then, no fugitive related variables or functions are loaded.
Add this code file in the $VIMRUNTIME/after/plugin
path:
" $VIMRUNTIME/after/plugin/fugitive-statusline.vim
if has("statusline") && exists('*fugitive#statusline')
" git fugitive statusline
set statusline=%<%f\ %h%m%r%{fugitive#statusline()}%=%-14.(%l,%c%V%)\ %P
" show statusline always
set laststatus=2
" turn off ruler
set noruler
endif
Upvotes: 4
Reputation: 14897
This one would be a shortcut:
set statusline+=%{exists('g:loaded_fugitive')?fugitive#statusline():''}
Upvotes: 11
Reputation: 5229
You could try checking the loaded variable of the fugitive plugin?
if exists('g:loaded_fugitive')
set statusline=%<\ %f\ %{fugitive#statusline()} ... (other stuff)
endif
although if the existance of the fugitive#statusline isn't working this might not be that effective!
Upvotes: 1