vimdiff:跳转到行内的下一个差异?

sle*_*ske 47 diff vim comparison vimdiff

vimdiff对于比较文件非常方便。但是,我经常在行长且行内差异相对较小的文件上使用它。

vimdiff 将正确突出一行内的差异(整行粉红色,不同字符红色)。在这些情况下,能够跳转到行内的下一个差异会很好。

您可以跳转到“下一个差异”( ]c),但这将跳转到下一个有差异的行。

有没有办法转到当前行内的下一个不同字符?

小智 18

这是一个简单的解决方法:

您可以使用set wrap.

如果差异导致文本以不相等的行数换行,这将产生问题。

  • 又快又脏,但它为我完成了这项工作。 (3认同)

Luc*_*tte 11

我看到两种解决方案:

  1. 您必须测试当前的语法突出显示才能跳转到行中的红色部分。
  2. 您必须在两个缓冲区中提取当前行并找到第一个不同的字符以正确定位光标

这两种解决方案都需要在 ]c 之后执行,并且需要 vim 脚本。

编辑:这是一个似乎有效的初稿:

nnoremap <expr> <silent> <F3>   (&diff ? "]c:call \<sid>NextDiff()\<cr>" : ":cn\<cr>")

function! s:GotoWinline(w_l)
  normal! H
  while winline() < a:w_l
    normal! j
  endwhile
  " todo: beware of cases where the window is too little
endfunction

" Better ]c, [c jump
function! s:NextDiff()
  if ! &diffopt =~ 'filler' | return | endif

  let ignore_blanks = &diffopt =~ 'iwhite'

  " Assert: called just after a ]c or a [c
  " Forces the cursos to be synchronized in all synced windows
  " let diff_l = line()
  try 
    let foldenable = &foldenable
    set nofoldenable

    let w_l = winline() " problematic with enabled lines (from diff...)
    " echomsg w_l.'|'.line('.').'|'.getline('.')

    let lines = {}
    windo if &diff | call <sid>GotoWinline(w_l) | let lines[winnr()]={'text':getline('.'), 'number':line('.')} | endif
  finally
    let &foldenable = foldenable
  endtry

  " echomsg string(lines)
  if len(lines) < 2 | return | endif

  let indices = repeat([0], len(lines))
  let tLines = values(lines)
  let found = 0
  " infinite loop on two empty texts...
  while ! found
    let c = ''
    let next_idx = []
    let i = 0
    while i != len(indices)
      let crt_line = tLines[i].text
      let n = indices[i]
      if len(crt_line) == n
    let found = 1
    break
      endif

      let c2 = (len(crt_line) == n) ? 'EOL' : crt_line[n]
      if empty(c) 
    let c = c2
      endif

      " checks match
      let n += 1
      if c =~ '\s'
    if (c2 != c) && (ignore_blanks && c2 !~ '\s')
      let found = 1
      break
    else " advance
      while ignore_blanks && (n == len(crt_line) || crt_line[n] =~ '\s')
        let n += 1
      endwhile
    endif
      else
    if c2 != c
      let found = 1
      break
    endif
      endif
      let next_idx += [n]

      let i += 1
    endwhile
    if found | break | endif

    let indices = next_idx
  endwhile

  " now goto the right column
  let windows = keys(lines)
  " Assert len(windows) == len(indices)
  let w = 0
  while w != len(windows)
    " echomsg 'W#'.windows[w].' -> :'(tLines[w].number).'normal! '.(indices[w]+1).'|'
    exe windows[w].'wincmd w'
    silent! exe (tLines[w].number).'normal! 0'.(indices[w]).'l'
    let w += 1
  endwhile
  " echomsg string(indices)
endfunction
Run Code Online (Sandbox Code Playgroud)

  • 从那时起,我将代码放入这个脚本 http://code.google.com/p/lh-vim/source/browse/misc/trunk/plugin/searchfile.vim?r=320(这做了一些其他事情),不幸的是,我观察到了一个不时发生的无限循环。我最终会修复它。 (2认同)