Vim: Conditionally use the hidden # status function in vimrc

I use the same vimrc on many machines, some of which have fugitive.vim, and some of them do not. I like to include fugitive#statusline() in my status line, but on computers that do not have the plugin installed, an error occurs.

Is there a way to verify the existence of this function before calling set statusline ? I tried using exists y, but for some reason it doesn't work (boot order?)

 if exists("*fugitive#statusline") set statusline=%<\ %f\ %{fugitive#statusline()} ... (other stuff) endif 

I also tried disabling the error prefix command silent! but that doesn't work either.

+6
source share
4 answers

This will be the shortcut:

 set statusline+=%{exists('g:loaded_fugitive')?fugitive#statusline():''} 
+9
source

As fugitive#statusline in the fugitive#statusline directory does not define as autoload , unfortunately, the use of the silent! call technique cannot be sniffed silent! call silent! call / exists (thanks @ Christopher). However, there are several alternatives:

  • Put the triple branch in your 'statusline' option as suggested by @tungd.
  • Set 'statusline' in the after/plugin file, for example, as Christopher says. This solves the problem, but means that your status line is defined in a very unlikely place, so it is probably best to add a good comment to your ~/.vimrc file.
  • Just define the function in your ~/.vimrc file.

Example:

 if !exists('*fugitive#statusline') function! fugitive#statusline() return '' endfunction endif 
  • Another option is to use the Fugitive autocmd event, which defines Fugitive. Please note that this will only work when Fugitive detects the 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's solution is the easiest. However, simply defining a dummy function will be my next choice. Fugitive will override it if Fugitive is installed. The best part is to keep the 'statusline' options neat and clean.

+6
source

I think I have developed how to achieve this. Checking for fugitive#statusline() should happen after plugins are loaded. Until then, no hidden bound variables or functions are loaded.

Add this code file to 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 
+4
source

Can you try checking the loaded hidden plugin variable?

 if exists('g:loaded_fugitive') set statusline=%<\ %f\ %{fugitive#statusline()} ... (other stuff) endif 

although if the existence of a running status bar # does not work, it may prove to be ineffective!

+1
source

Source: https://habr.com/ru/post/887963/


All Articles