Vim: Conditionally use fugitive#statusline function in vimrc
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#s开发者_如何学Gotatusline")
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:
- Put a ternary branch in your
'statusline'
option as @tungd suggested. - Set
'statusline'
in anafter/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. - Simply define the function in your
~/.vimrc
file.
Example:
if !exists('*fugitive#statusline')
function! fugitive#statusline()
return ''
endfunction
endif
- Another option is to use a
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.
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
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!
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
精彩评论