How to exit vimdiff mode in vim, in particular for Fugitive?

I am using vim with a runaway extension . He has a command: Gdiff, which puts you in vimdiff mode, but what is the right / fast way to close / exit vimdiff mode?

That is, let's say I am editing the FooBar.txt file in the Git repository. I run: Gdiff, look at my changes in vimdiff, and then want to go back and continue editing FooBar.txt or any other file :)

UPDATE 1: I'm going to try these quick combinations the next business 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><cw>h:bd<cr> 

UPDATE 2: My current mappings (closes only the comparison window!)

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

Also, please help me decide if the next one should be a responder: https://stackoverflow.com/a/167298/

+56
vim vimdiff vim-fugitive
Apr 24 '10 at 7:45
source share
15 answers

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

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

+42
Apr 24 '10 at 8:24
source share

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

Close another window. The easiest way to do this if you haven't switched focus to it is <CW><CO> , which means "make this window the only one."

+36
Oct 14 '11 at 2:59 a.m.
source share

I was not lucky with diffoff , but I just found out that :Gedit with no argument will return you back to the working directory version of the file, unlike the previous version you were viewing.

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

+12
Feb 18 2018-11-18T00:
source share

This works great 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> 
+4
Apr 04 2018-11-11T00:
source share

I found a simple solution for this. You can check it out 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 endif 
+4
Feb 27 '13 at 14:00
source share

None of the above solutions worked for me. It ended with:

nnoremap <Leader>D :Gedit<CR><Cw>h :q<CR><Cw>k

+3
Mar 24 '11 at 9:15
source share

An alternative to <CW><CO> , if you have multiple windows, will move to another diff window and make <CW>c that close only one window.

If you close the wrong diff window, run :Gedit

Be careful not to confuse <CW>c with <CW><CC>

+2
Apr 12 '13 at 15:22
source share

this is what i have to leave vimdiff windows after using: gdiff

 nnoremap gD :q!<CR> :Gedit!<CR> 
+1
Jul 05 2018-12-12T00:
source share

Check out the vimdiff switch between diffthis and diffoff here on this page .

The code:

 nnoremap <silent> <Leader>df :call DiffToggle()<CR> function! DiffToggle() if &diff diffoff else diffthis endif :endfunction 
0
Jan 10 '13 at 11:12
source share

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

Completely mismatched mode and close other windows. (Note: the runaway will automatically delete hidden buffers.)

0
Jun 20 '13 at 2:02
source share

My function will work both from the diff window and from the file window. But probably he will not cope with the opening of several diff. To do this, you will need to use fugitive#buffer(n).path() for scanning and matching.

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

Add Key Binding:

 nnoremap <silent> <leader>gD :Gdiffoff<CR> 
0
Aug 27 '14 at 15:16
source share

Another way. What I have in fugitive.vim is to first save some information (s: gitbufname) when diff starts:

 function! s:Diff(vert,...) abort call sy#toggle() let s:startcol = winwidth(0) let &columns=(winwidth(0) * 2 - 20) ... if getwinvar('#', '&diff') let s:gitbufname = bufname("%") wincmd p call feedkeys(winnr."\<CW>w", 'n') endif ... endfunction 

and then when you exit the buffer switch window to the saved buffer and restore:

 augroup fugitive_diff autocmd! autocmd BufWinLeave * \ if s:can_diffoff(+expand('<abuf>')) && s:diff_window_count() == 2 | \ if exists('s:gitbufname') && winnr() != bufwinnr(s:gitbufname) | \ let nr = bufnr("%") | exe bufwinnr(s:gitbufname).'wincmd w' | exe 'buf'.nr | \ endif | \ call s:diffoff_all(getbufvar(+expand('<abuf>'), 'git_dir')) | \ call sy#toggle() | \ call airline#load_theme() | call airline#update_statusline() | \ let &columns=s:startcol | \ endif ... 
0
Apr 03 '15 at 0:03
source share

Method 1:

  • open comparison:

:windo diffthis

  • close it:

:windo diffoff

Method 2:

I recommend just using the simplest command :q<CR>

when you want to do it fast add a mapping:

 " Set mapleader let mapleader = "," let g:mapleader = "," 

and

 " Quickly close the current window nnoremap <leader>q :q<CR> 

This works well for me. Exit vimdiff only on ,q , because usually your cursor is in the old file.

0
Oct. 19 '16 at 6:50
source share

Used the code below, based on https://stackoverflow.com/a/3/9/16/16/16/16/16/16/16/16/16/64/16/16/16/16/16/16/64/64/16/16/64/

  if !exists(":Gdiffoff") command Gdiffoff bw! fugitive://* endif 

but this gave me the error "E93: more than one match for ..." in a 3-way difference, so I used the answer from https://stackoverflow.com/a/26626/ ... and finally got this:

 function! GetBufferList() return filter(range(1,bufnr('$')), 'buflisted(v:val)') endfunction function! GetMatchingBuffers(pattern) return filter(GetBufferList(), 'bufname(v:val) =~ a:pattern') endfunction function! WipeMatchingBuffers(pattern) let l:matchList = GetMatchingBuffers(a:pattern) let l:count = len(l:matchList) if l:count < 1 echo 'No buffers found matching pattern ' . a:pattern return endif if l:count == 1 let l:suffix = '' else let l:suffix = 's' endif exec 'bw ' . join(l:matchList, ' ') echo 'Wiped ' . l:count . ' buffer' . l:suffix . '.' endfunction command! -nargs=1 Gdiffoff call WipeMatchingBuffers('fugitive://') 

I just tweaked, copied and pasted the code into my .vimrc

0
Feb 01 '19 at 7:27
source share

Launch :Gwrite after merging at your request will close the other two difference panels in addition to updating the git cache to mark the file as merged.

0
Apr 3 '19 at 8:07
source share



All Articles