2

I'm still kinda new in vim so i hope this question isn't too basic, but I couldn't find an answer in fugitive's DOCs.. im looking to add a toggling function that does the following:

if :Gblame buffer is open:
     close buffer
else 
     execute "normal :Gblame<CR>"
endif

map gb :call (the above function)<CR>

I was thinking of adding a variable that changes between 1 or 0 depending on the number of times i used the "gb" mapping.
But what i would really want is a way to check if the buffer is open. is that possible?

Amichai
  • 174
  • 2
  • 11

1 Answers1

7

The Fugitive plugin sets a custom 'filetype' in the sidebar blame buffer (to fugitiveblame; queried via :setlocal filetype?, or in Vimscript via the &l:filetype special variable). This only works when you're currently in the sidebar. To make this work for the main window, too, you'd have to check for the buffer name (e.g. via bufwinnr('fugitiveblame') != -1), and then also go back to that window before calling :close (or using :bdelete with the buffer number obtained via bufnr('fugitiveblame')).

function! s:ToggleBlame()
    if &l:filetype ==# 'fugitiveblame'
        close
    else
        Gblame
    endif
endfunction

nnoremap gb :call <SID>ToggleBlame()<CR>

Notes

  • :Gblame is a (custom) Ex command; you can just invoke it as-is in Vimscript; no need for :normal here.
  • Closing the sidebar is just :close when you're in it, as mentioned above.
  • I've defined a script-local function; cp. :help <SID>.
  • You should use :noremap; it makes the mapping immune to remapping and recursion.
Ingo Karkat
  • 167,457
  • 16
  • 250
  • 324
  • 1
    Might want to use `:normal gq` inside the `fugitiveblame` window as it will close the blame window and then restore the file to the working tree (like `:Gedit`). See `:h fugitive-:Gblame`. However, I think I would just make a mapping to `:Gblame` and learn to use `gq` inside the blame window. – Peter Rincker Nov 26 '18 at 16:42