VIMでVimdiffモードをどのように終了しますか?特に、逃亡者のために?

StackOverflow https://stackoverflow.com/questions/2703644

  •  01-10-2019
  •  | 
  •  

質問

私はでVIMを使用しています 逃亡拡張. 。 vimdiffモードに導入する:gdiffコマンドがありますが、vimdiffモードを閉じる/quay迅速な方法は何ですか?

つまり、gitリポジトリの下でファイルfoobar.txtを編集しているとしましょう。私は起動します:gdiff、vimdiffでの変更を確認してから、戻ってfoobar.txtまたはその他のファイルを編集し続けたいと思います:)

更新1: 私はこれらのクイックコンボを次の仕事の日を試してみます:)

"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: 私の現在のマッピング(diffウィンドウのみを閉じます!)

"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

また、以下がanwserであるべきかどうかを判断するのを手伝ってください: https://stackoverflow.com/a/15975201/275980

役に立ちましたか?

解決

実行できます windo set nodiff noscrollbind 次に、2番目のウィンドウを閉じます。

更新:aがあります diffoff 指図。使用する windo diffoff, 、前の行で書いたものではありません。

他のヒント

によると: https://github.com/tpope/vim-fugivity/issues/36

他のウィンドウを閉じます。焦点をシフトしていない場合にこれを行う最も簡単な方法は <C-W><C-O>, 、これは「このウィンドウを唯一のウィンドウにする」を意味します。

私は運がありませんでした diffoff, 、しかし、私はそれを学びました :Gedit あなたがレビューしていたいくつかの以前のバージョンとは対照的に、引数がファイルのワーキングディレクトリバージョンに戻ることはありません。

そしてとして q (必要ありません :q)DIFFサイドバーを閉じます、あなたはできます q に続く :Gedit サイドバーを取り除いてから、ファイルの現在のバージョンに戻ります。

これは私にとって正常に機能し、既存のアイデアのいくつかをここに組み合わせています。

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>

これのための簡単な解決策を見つけました。ここで確認できます: 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

上記のソリューションはどれも私のために働いていませんでした。代わりにこれを行うことになりました:

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

代替手段 <C-W><C-O>, 、複数のウィンドウがある場合、他のDiffウィンドウに移動して実行します <C-W>c, 、1つのウィンドウのみを閉じます。

間違ったdiffウィンドウを閉じる場合は、行います :Gedit

注意して混乱しないでください <C-W>c<C-W><C-C>

これは私が使用した後にvimdiffウィンドウを離れなければならないものです:gdiff

nnoremap gD :q!<CR> :Gedit!<CR>

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

非常にdiffモードと他のdiffウィンドウを閉じます。 (注:FUGITITは隠されたバッファーを自動削除します。)

私の関数は、Diffウィンドウとファイルウィンドウの両方から機能します。しかし、おそらく複数のdiffが開かれた状態でそれ自体を処理しないでしょう。そのために使用する必要があります fugitive#buffer(n).path() スキャンして一致させます。

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

キーバインディングを追加します:

nnoremap <silent> <leader>gD :Gdiffoff<CR>

さらに別の方法。 fugitive.vimで私が持っているもの - 最初にいくつかの情報(s:gitbufname)を保存します。

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."\<C-W>w", 'n')
    endif
...
endfunction

そして、後でバッファスイッチウィンドウを保存したバッファーに残して復元するとき:

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

を確認します vimdiff 間の切り替え diffthisdiffoff こここのページで.

コード:

nnoremap <silent> <Leader>df :call DiffToggle()<CR>

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

方法1:

  • 比較を開きます:

:windo diffthis

  • 閉じる:

:windo diffoff

方法2:

最も簡単なコマンドを使用することをお勧めします。 :q<CR>

すぐにやりたいときは、マッピングを追加します。

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

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

それは私にとってうまく機能します。ちょうどvimdiffを終了します ,q, 、通常、古いファイルにカーソルがあります。

以下に基づいて以下のコードを使用していました https://stackoverflow.com/a/15113951/10999673 :

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

しかし、それは私に「E93:複数のマッチのために...」というエラーを与えてくれました。 https://stackoverflow.com/a/4867969/1099673 そして最後にこれを持っています:

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://')

.vimrcにコードを微調整し、コピーし、貼り付けました

ランニング :Gwrite 満足に合わせた後、GITキャッシュを更新してファイルをマージしたとマークすることに加えて、他の2つのDiffペインを閉じます。

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top