我正在使用vim与 逃亡扩展. 。它具有一个:GDIFF命令,它使您进入Vimdiff模式,但是关闭/退出Vimdiff模式的正确/快速方法是什么?

即,假设我正在git存储库下编辑foobar.txt的文件。我开火:GDIFF,查看我对Vimdiff的更改,然后我想返回并继续编辑foobar.txt或任何其他文件:)

Update1: 下一天工作的一天,我将尝试这些快速的连击:)

"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 然后关闭第二个窗口。

更新:有一个 diffoff 命令。采用 windo diffoff, ,不是我在上一行中写的。

其他提示

根据: https://github.com/tpope/vim-fugitive/issues/36

关闭另一个窗口。如果您没有转移重点是,最简单的方法是 <C-W><C-O>, ,这意味着“使此窗口成为唯一的窗口”。

我没有运气 diffoff, ,但我只是了解到 :Gedit 没有参数会让您回到文件的工作目录版本,而不是您正在审查的一些较早版本。

并作为 q (无需 :q)将关闭差异侧边栏,您可以做 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>, ,如果您有多个窗口,将移至另一个差异窗口并执行 <C-W>c, ,只关闭一个窗口。

如果您关闭了错误的差异窗口 :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窗口和文件窗口中起作用。但是可能不会通过打开多个差异来处理自己。为此您需要使用 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>

另一种方式。我在fugivity.vim中拥有的东西 - diff开始时首先保存一些信息(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:不止一个匹配...”以3方式差异,因此我使用了答案 https://stackoverflow.com/a/4867969/10999673 最后有这个:

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 Cache以标记合并的文件外,还将关闭其他两个Diff Panes。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top