Skip to content

Instantly share code, notes, and snippets.

@actionshrimp
Created September 9, 2013 09:49
Show Gist options
  • Save actionshrimp/6493611 to your computer and use it in GitHub Desktop.
Save actionshrimp/6493611 to your computer and use it in GitHub Desktop.
A vim-fugitive status window toggle (mapped to F3). Seems to work nicely
function! ToggleGStatus()
if buflisted(bufname('.git/index'))
bd .git/index
else
Gstatus
endif
endfunction
command ToggleGStatus :call ToggleGStatus()
nmap <F3> :ToggleGStatus<CR>
@ebartrum
Copy link

Thanks!

@meijieru
Copy link

meijieru commented Sep 1, 2020

Thanks!

@diegommarino
Copy link

Thanks! Worked like a charm.

@harrisoncramer
Copy link

Awesome, thank you!

@Nico-Guyon
Copy link

That's exactly what I was trying to add to my vimrc.
Many thanks !

@kenichi
Copy link

kenichi commented Aug 3, 2022

thank you for this, been using a while but now seems like this commit has changed the bufname to something like:

fugitive:///path/to/dir/with/.git

i couldn't come up with a quick fix, as the bufname changes depending on path; any ideas?

@Fymyte
Copy link

Fymyte commented Aug 17, 2022

@kenichi bufname accepts a file pattern, you could use

bufname('fugitive:///*/.git/')

to match the fugitive Git buffer
and then you can use

 execute ":bdelete" bufname('fugitive:///*/.git/') 

to delete the buffer
EDIT:
Fugitive adds a second / at the end, use

bufname('fugitive:///*/.git//$')

For thos who wants this in lua for neovim

local function showFugitiveGit()
  if vim.fn.FugitiveHead() ~= '' then
    vim.cmd [[
    Git
    " wincmd H  " Open Git window in vertical split
    " setlocal winfixwidth
    " vertical resize 31
    " setlocal winfixwidth
    setlocal nonumber
    setlocal norelativenumber
    ]]
  end
end
local function toggleFugitiveGit()
  if vim.fn.buflisted(vim.fn.bufname('fugitive:///*/.git//$')) ~= 0 then
    vim.cmd[[ execute ":bdelete" bufname('fugitive:///*/.git//$') ]]
  else
    showFugitiveGit()
  end
end
vim.keymap.set('n', '<F3>', toggleFugitiveGit, opts)

EDIT: fixed typo

@kenichi
Copy link

kenichi commented Aug 20, 2022

@Fymyte thank you very much!

side note - experimenting both with and without the '$'; while i appreciate that it mimics the original behavior closer with it, i actually kind of like it without.

@disselkamp
Copy link

@Fymyte Thanks for the lua snippet! (p.s. there is a typo, it should be if vim.fn.buflisted(vim.fn.bufname('fugitive:///*/.git//$')) ~= 0 then)

@Fymyte
Copy link

Fymyte commented Aug 20, 2022

@disselkamp thanks for the report 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment