如何在 vim 中退出 vimdiff 模式,特别是对于 Fugitive?

发布于 2024-08-29 17:38:25 字数 980 浏览 4 评论 0原文

我使用带有 逃逸扩展 的 vim。它有一个 :Gdiff 命令,可以让您进入 vimdiff 模式,但是关闭/退出 vimdiff 模式的正确/快速方法是什么?

即,假设我正在 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>

更新2:我当前的映射(仅关闭差异窗口!)

"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

另外,请帮助我决定以下内容是否应该是一个答案:https://stackoverflow.com/a/15975201/275980

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(16

梦过后 2024-09-05 17:38:25

您可以执行windo set nodiff noscrollbind,然后关闭第二个窗口。

更新:有一个diffoff命令。使用 windo diffoff,而不是我在上一行中编写的内容。

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.

豆芽 2024-09-05 17:38:25

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

关闭另一个窗口。如果您尚未将焦点转移到它,最简单的方法是 ,这意味着“使该窗口成为唯一的窗口。”

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

街角卖回忆 2024-09-05 17:38:25

我对 diffoff 没有什么运气,但我刚刚了解到,不带参数的 :Gedit 会将您带回文件的工作目录版本,而不是之前的一些版本您正在审查的版本。

由于 q (不需要 :q)将关闭 diff 侧边栏,因此您可以执行 q ,然后执行 :Gedit 删除侧边栏,然后返回到文件的当前版本。

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.

浅忆流年 2024-09-05 17:38:25

上述解决方案都不适合我。最终改为这样做:

nnoremapD :Gedith :qk

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

幻想少年梦 2024-09-05 17:38:25

这对我来说效果很好,结合了这里的一些现有想法:

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>

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>
随心而道 2024-09-05 17:38:25

如果您有多个窗口,则 的替代方法是移动到另一个 diff 窗口并执行 c,这会关闭只有一扇窗户。

如果您关闭了错误的 diff 窗口,请执行 :Gedit

请小心,不要将 c< 混淆/代码>

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>

时光暖心i 2024-09-05 17:38:25

我为此找到了一个简单的解决方案。您可以在这里查看: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

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
endif
热情消退 2024-09-05 17:38:25

在此处检查 vimdiffdiffthisdiffoff 之间切换
在此页面

代码:

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

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

Check the vimdiff toggling between diffthis and diffoff here
at this page.

The code:

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

function! DiffToggle()
    if &diff
        diffoff
    else
        diffthis
    endif
:endfunction
給妳壹絲溫柔 2024-09-05 17:38:25

方法 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>

我来说效果很好。只需按 ,q 即可退出 vimdiff,因为通常您的光标位于旧文件中。

Method 1:

  • open a compare by:

:windo diffthis

  • close it by:

:windo diffoff

Method 2:

I recommend just using the most simple command: :q<CR>

when you want to do it quickly, add the mapping:

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

and

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

It works well for me. Exit vimdiff just by ,q, because normally your cursor in the old file.

清风无影 2024-09-05 17:38:25

这就是我使用 :Gdiff 后必须离开 vimdiff 窗口的内容

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

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

nnoremap gD :q!<CR> :Gedit!<CR>
若水般的淡然安静女子 2024-09-05 17:38:25

noremapdo :diffoff \| windows if & diff \|隐藏\| endif

完全 diff 模式并关闭其他 diff 窗口。 (注意:逃亡者将自动删除其隐藏缓冲区。)

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

瞳孔里扚悲伤 2024-09-05 17:38:25

我的函数可以在 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>

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
    else
        echo 'Error: Not in diff or file'
    endif
endfunction

Add a key binding:

nnoremap <silent> <leader>gD :Gdiffoff<CR>
沩ん囻菔务 2024-09-05 17:38:25

还有另一种方式。我在 fugitive.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
...

Yet another way. What I have in fugitive.vim - first save some info (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."\<C-W>w", 'n')
    endif
...
endfunction

and later when leaving 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
...
复古式 2024-09-05 17:38:25

基于 https://stackoverflow.com/a/15113951/10999673 使用下面的代码:

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

但它给了我一个错误“E93:多于一个匹配...”在三向差异中,所以我改为使用 https:// stackoverflow.com/a/4867969/10999673 终于有了这个:

function! GetBufferList()
    return filter(range(1,bufnr('

我刚刚调整、复制代码并将其粘贴到我的 .vimrc 中

)), '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 中

Was using the code below based on https://stackoverflow.com/a/15113951/10999673 :

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

but it gave me an error "E93: more than one match for ..." in a 3 way diff, so i instead used the answer from https://stackoverflow.com/a/4867969/10999673 and finally have this:

function! GetBufferList()
    return filter(range(1,bufnr('

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

)), '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

昔梦 2024-09-05 17:38:25

合并到满意程度后运行 :Gwrite 除了更新 git 缓存以将文件标记为已合并之外,还将关闭其他两个差异窗格。

Running :Gwrite after merging to your satisfaction will close the other two diff panes in addition to updating the git cache to mark the file as merged.

撞了怀 2024-09-05 17:38:25

我采用以下方法:

vim.keymap.set("n", "gx", ":Git<CR><C-w><C-o>:bd<CR>")

无论您是否站在差异视图中,这都有效。它首先运行 :Git ,它将带你到逃亡的 git 缓冲区(如果你站在 diff 视图中)。然后它会使用 Cw Co 关闭所有其他缓冲区。最后,我们使用“:bd”卸载逃逸的 git 缓冲区,这将带您回到之前的位置,类似于当所有其他缓冲区都已关闭时站在 git 缓冲区中键入 gq 。

理想情况下,我希望它覆盖逃亡者的 gq 并使用快捷方式 gq 而不是 gx,但是我不知道如何做到这一点。我还希望它仅在处于 diff 缓冲区或 git 缓冲区之一时才起作用。

编辑:刚刚发现,如果您已经打开了多个缓冲区并且您想打开逃亡并执行一些 vim 操作,则此方法效果不佳。因此,更新了我的“打开逃亡者”快捷方式以在新选项卡中打开,所以现在我的关闭热键将我带回上一个选项卡,其中包含所有缓冲区,就像打开逃亡者之前一样。

-- Leader+gs to open fugitive in new tab (and go down five lines for convenience)
vim.keymap.set("n", "<leader>gs", ":tab Git<CR>5j")

编辑2:
现在,我在新选项卡中打开 git fugitive 窗口,我完全放弃了关闭热键,而是为关闭选项卡创建了绑定。

I went with the following:

vim.keymap.set("n", "gx", ":Git<CR><C-w><C-o>:bd<CR>")

This works whether you're standing in the diff view or not. It first runs :Git which takes you to the fugitive git buffer (in case you we're standing in the diff view). Then it closes all other buffers with C-w C-o. Finally we unload the fugitive git buffer with ":bd" which will take you back to where you were before, similar to typing gq when standing in the git buffer when all other buffers have been closed.

Ideally I want this to override the fugitive's gq and make the shortcut gq instead of gx, however I couldn't figure out how to do that. I also wanted this to work only when standing in either one of the diff buffers or the git buffer.

EDIT: Just found out that this works poorly if you have several buffers already open and you want to open fugitive and do some vim stuff. So updated my "open fugitive" shortcut to open in a new tab, so now my closing hotkey takes me back to the previous tab with all my buffers as they were prior to opening fugitive.

-- Leader+gs to open fugitive in new tab (and go down five lines for convenience)
vim.keymap.set("n", "<leader>gs", ":tab Git<CR>5j")

EDIT 2:
Now that I open the git fugitive window in a new tab I completely ditched my closing hotkey all together and instead made a binding for closing tabs.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文