Vimdiff:显示更改总数

Pri*_*ash 17 vim diff

在(g)vim中区分两个文件时,是否可以显示更改总数?我想这相当于计算折叠次数,但我不知道该怎么做.

理想情况下,我想要一条消息,其中包含"Change 1 of 12"哪些内容会在我循环更改时更新]c.

我将办公室的一些成员转变为Vim的奇迹,取得了巨大的成功,但Vimdiff是一个坚持不懈的小熊.

Pri*_*ash 2

这是一个稍微更精致的解决方案。它使用与我之前的答案相同的技术来计算差异,但它将每个块的第一行存储在分配给全局变量的列表中g:diff_hunks。然后通过查找列表中行号的位置即可找到光标下方的块数。另请注意,我在最后设置nocursorbind并重置了它们,以确保我们不会破坏差异窗口中的鼠标滚动。noscrollbind

function! UpdateDiffHunks()
    setlocal nocursorbind
    setlocal noscrollbind
    let winview = winsaveview() 
    let pos = getpos(".")
    sil exe 'normal! gg'
    let moved = 1
    let hunks = []
    while moved
        let startl = line(".")
        keepjumps sil exe 'normal! ]c'
        let moved = line(".") - startl
        if moved
            call add(hunks,line("."))
        endif
    endwhile
    call winrestview(winview)
    call setpos(".",pos)
    setlocal cursorbind
    setlocal scrollbind
    let g:diff_hunks = hunks
endfunction
Run Code Online (Sandbox Code Playgroud)

UpdateDiffHunks()每当修改 diff 缓冲区时就应该更新该函数,但我发现将其映射到CursorMoved和就足够了BufEnter

function! DiffCount()
    if !exists("g:diff_hunks") 
        call UpdateDiffHunks()
    endif
    let n_hunks = 0
    let curline = line(".")
    for hunkline in g:diff_hunks
        if curline < hunkline
            break
        endif
        let n_hunks += 1
    endfor
    return n_hunks . '/' . len(g:diff_hunks)
endfunction
Run Code Online (Sandbox Code Playgroud)

的输出DiffCount()可以在状态行中使用,或与命令绑定。