airblade/vim-gitgutter

GitGutterDiffOrig toggle

ds2606 opened this issue ยท 7 comments

What is the latest commit SHA in your installed vim-gitgutter?
2f35907
What vim/nvim version are you on?
8.2

Repeated invocations of :GitGutterDiffOrig keep opening more splits. To close the split, I manually move to the diff'ed split and close the buffer. (:only doesn't work in the case that there is originally >1 split before opening the diff).

Can we add a :GitGutterDiffToggle that just closes the GitGutter diff buffer if it's already open?

Vim's own :+clo should do the trick.

Hey @airblade how can I use :+clo in neovim?

It shows me an error

E16: Invalid range

Update:

I was able to close the window by using :clo without + sign.

The + means apply the clo[se] command to the next window. It's a {count}. See :help edit-window for possible values you can use.

If your 'splitright' is off, you need to use :-clo instead or you'll get E16: Invalid range.

Nifty, just learned more about count specifiers, thanks!

The only kink is that I have :GGDiffOrig mapped to <leader>gd: it'd be nice just to be able to just invoke that same keybinding again to close the just-opened diff buffer. I'm going to just implement a short function to take care of this, but seems like something that could be handy for users in general?

EDIT:
OK, so I just implemented this in a super basic way. I'm new to vimscript and not sure if this is hacky or not though

let g:gitgutter_diff_open = 0
function! GitGutterDiffOrigToggle()
   if ! g:gitgutter_diff_open
       GitGutterDiffOrig
   else
       +clo
    endif
    let g:gitgutter_diff_open = !g:gitgutter_diff_open
endfunction
nnoremap <silent> <leader>gd :call GitGutterDiffOrigToggle()<CR>

OK, so I just implemented this in a super basic way. I'm new to vimscript and not sure if this is hacky or not though

Looks good to me.

You could perhaps avoid using a variable to track the state by looking for a side-effect of the command. For example:

function! GitGutterDiffOrigToggle()
  if &diff
    +clo
  else
    GitGutterDiffOrig
  endif
endfunction

You could collapse this further:

nnoremap <silent> <expr> <leader>gd &diff ? ':+clo<CR>' : ':GitGutterDiffOrig<CR>'

Much better/cleaner, thanks :-)