How do you exit vimdiff mode in vim, specifically, for Fugitive?

34,456

Solution 1

You can execute windo set nodiff noscrollbind and then close the second window.

Update: there is a diffoff command. Use windo diffoff, not what I wrote in previous line.

Solution 2

According to: https://github.com/tpope/vim-fugitive/issues/36

Close the other window. The easiest way to do this if you haven't shifted focus to it is <C-W><C-O>, which means "make this Window the Only window."

Solution 3

I had no luck with diffoff, but I just learned that :Gedit with no argument will bring you back to the working-directory version of the file, as opposed to some earlier version you were reviewing.

And as q (no need for :q) will close the diff sidebar, you can do q followed by :Gedit to get rid of the sidebar and then go back to the current version of the file.

Solution 4

This works fine for me, combining some of the existing ideas here:

function! MyCloseDiff()
  if (&diff == 0 || getbufvar('#', '&diff') == 0)
        \ && (bufname('%') !~ '^fugitive:' && bufname('#') !~ '^fugitive:')
    echom "Not in diff view."
    return
  endif

  " close current buffer if alternate is not fugitive but current one is
  if bufname('#') !~ '^fugitive:' && bufname('%') =~ '^fugitive:'
    if bufwinnr("#") == -1
      b #
      bd #
    else
      bd
    endif
  else
    bd #
  endif
endfunction
nnoremap <Leader>gD :call MyCloseDiff()<cr>

Solution 5

An alternative to <C-W><C-O>, if you have multiple windows, would be move to the other diff window and do <C-W>c, which close only one window.

If you close the wrong diff window do a :Gedit

Be careful and don't confuse <C-W>c with <C-W><C-C>

Share:
34,456
wik
Author by

wik

Software Craftsman and Data Science Enthusiast

Updated on April 03, 2020

Comments

  • wik
    wik about 4 years

    I am using vim with the fugitive extension. It has a :Gdiff command which brings you into vimdiff mode, but what is the right/quick way to close/quit vimdiff mode?

    I.e., let's say I am editing the file FooBar.txt under Git repository. I fire up :Gdiff, review my changes in vimdiff, and then I want to get back and continue editing FooBar.txt or any other file :)

    UPDATE1: I'm going to give these quick combos a try next working day :)

    "vimdiff current vs git head (fugitive extension)
    nnoremap <Leader>gd :Gdiff<cr> 
    "switch back to current file and closes fugitive buffer
    nnoremap <Leader>gD :diffoff!<cr><c-w>h:bd<cr>
    

    UPDATE2: My current mappings (closes diff window only!)

    "vimdiff current vs git head (fugitive extension)
    nnoremap <Leader>gd :Gdiff<cr> 
    "switch back to current file and closes fugitive buffer
    nnoremap <Leader>gD <c-w>h<c-w>c
    

    Also, please help me decide if the following should be an anwser: https://stackoverflow.com/a/15975201/275980