Take the 2-minute tour ×

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: http://stackoverflow.com/a/15975201/275980

share|improve this question
gDWorks for me without the :diffoff!<cr>. –  blueyed Apr 4 '11 at 13:02
Just focusing on the diff window and :q closing it seems to do the trick. It'd be nice if there were an easy way to do it from either window though. –  Aaron Gibralter Feb 20 at 16:43

11 Answers 11

up vote 18 down vote accepted

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.

share|improve this answer
also :diffoff! do the same, right? I was pointed to this command by Tony M. from vim_use group. –  wik Apr 25 '10 at 13:11
That won't close the window that was opened by :Gdiff, does it? –  blueyed Apr 4 '11 at 13:03
@blueyed Yes, it won't. I don't use fugitive, but diff mode created with VCSVimDiff (VCSCommand plugin) is replaced with non-diff mode (that was active before VCSVimDiff was run) if you run :bw` on the second buffer. It is very convenient, but it is VCSCommand plugin's own feature, not something that vim provides out of the box. You can create an autocommand with similar functionality in your vimrc if you want. –  ZyX Apr 4 '11 at 15:21
@BjornTipling What does not work? diffoff disables vimdiff mode no matter which plugin launched it. –  ZyX Oct 14 '12 at 13:50
@BjornTipling what I personally don't like about <C-W><C-O> is that it closes all windows, including git status window which is useful to when you doing some deeper investigation, i.e. there is <S-D> map in git status window that also opening diff mode on highlighted file –  wik Mar 5 '13 at 16:32

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."

share|improve this answer
IMHO this should be the chosen Answer –  lkraav Feb 11 '12 at 21:22
@lkraav: Agreed! –  Jonas Due Vesterheden May 24 '12 at 8:17
This only works for fugitive, right? –  idbrii Aug 3 '12 at 21:45
This would cause problems if you had multiple windows open before entering Gdiff, though--Gdiff only adds a buffer next to the original one, without closing any others. You'd have to re-open everything else you were working on. –  matthias Aug 13 '12 at 23:01
which isn't quite useful when you do diffs from git status window, since it closing all windows... –  wik Mar 11 '13 at 16:14

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.

share|improve this answer
q will start recording. So not sure what kind of 'q' you're using. Maybe that's a gui vim thing or something. –  Bjorn Tipling Oct 14 '12 at 17:22
Some plugins remap q, in a given buffer only (map <buffer> q ZZ or whatever), to close that buffer. For me, that happens in the Fugitive git blame "sidebar". Don't remember anymore if that's also used in any diff views. –  Henrik N Oct 16 '12 at 6:30
The default behaviour of :Gedit may be changed. Now it will edit the corespoding file in the index instead of the working tree file. –  Bohr Jun 20 '13 at 1:32

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."

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

None of the above solutions worked for me. Ended up doing this instead:

nnoremap <Leader>D :Gedit<CR><C-w>h :q<CR><C-w>k

share|improve this answer
Just figured this: nnoremap g<C-q> ZZ:Gedit<CR> (see my own question of today). I think it's somewhat better because it avoids moving between windows. –  sehe Nov 5 '13 at 15:10

I've found a simple solution for this. You can check it here: https://gist.github.com/radmen/5048080

" Simple way to turn off Gdiff splitscreen
" works only when diff buffer is focused
if !exists(":Gdiffoff")
  command Gdiffoff diffoff | q | Gedit
share|improve this answer
I'm liking this one. Ex commands FTW again. I think I'll be using this –  sehe Nov 5 '13 at 15:13

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|improve this answer
That's similar to what I ended up doing in my gD mapping: nnoremap <Leader>gD <c-w>h:bd<cr> –  wik Apr 13 '13 at 22:28
But thanks, <c-w>h<c-w>c looks nicer :) –  wik Apr 13 '13 at 22:31

this is what I have to leave the vimdiff windows after using :Gdiff

nnoremap gD :q!<CR> :Gedit!<CR>
share|improve this answer
I believe this will close the actual file and have you end up editing the temporary diff file, where you'll end up losing all of your changes. That is if you never moved your cursor over to the diff. –  Bjorn Tipling Oct 14 '12 at 17:19

Check the vimdiff toggling between diffthis and diffoff here at this page http://www.bestofvim.com/tip/diff-diff/

the code: nnoremap df :call DiffToggle()

function! DiffToggle() if &diff diffoff else diffthis endif :endfunction

share|improve this answer

noremap <leader>do :diffoff \| windo if &diff \| hide \| endif<cr>

Quite diff mode and close other diff windows. (Note: fugitive will auto delete its hidden buffers.)

share|improve this answer

My function will work both from diff window and file window. But probably won't handle itself with multiple diffs opened. For that you'll need to use fugitive#buffer(n).path() to scan and match.

command! Gdiffoff call Gdiffoff()
function! Gdiffoff()
    let diffbufnr = bufnr('^fugitive:')
    if diffbufnr > -1 && &diff
        diffoff | q
        if bufnr('%') == diffbufnr | Gedit | endif
        setlocal nocursorbind
        echo 'Error: Not in diff or file'

Add a key binding:

nnoremap <silent> <leader>gD :Gdiffoff<CR>
share|improve this answer

Your Answer


