Skip to content

Instantly share code, notes, and snippets.

@hotoo
Created August 29, 2010 23:31
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hotoo/556810 to your computer and use it in GitHub Desktop.
Save hotoo/556810 to your computer and use it in GitHub Desktop.
function! Jump2PrevDiffText()
let line=line(".")
let idx=col(".")-1
if synIDattr(diff_hlID(line, idx), "name")=="DiffChange" || synIDattr(diff_hlID(line, idx), "name")=="DiffText"
while idx>0
if synIDattr(diff_hlID(line, idx), "name")=="DiffText" && synIDattr(diff_hlID(line, idx-1), "name")!="DiffText"
call setpos(".", [0,line,idx])
break
elseif idx==1
let line=line(".")
let cols=col(".")
exec "normal [c"
if line==line(".") && cols==col(".")
return
elseif synIDattr(diff_hlID(".", 1), "name")=="DiffChange" || synIDattr(diff_hlID(".", 1), "name")=="DiffText"
call setpos(".", [0,line("."),col("$")-1])
call Jump2PrevDiffText()
endif
break
else
let idx = idx-1
endif
endwhile
else
let line=line(".")
let cols=col(".")
exec "normal [c"
if line==line(".") && cols==col(".")
return
elseif synIDattr(diff_hlID(".", 1), "name")=="DiffChange" || synIDattr(diff_hlID(".", 1), "name")=="DiffText"
call setpos(".", [0,line("."),col("$")-1])
call Jump2PrevDiffText()
endif
endif
endfunction
function! Jump2NextDiffText()
if synIDattr(diff_hlID(".", col(".")), "name")=="DiffChange" || synIDattr(diff_hlID(".", col(".")), "name")=="DiffText"
let line=line(".")
let cols=col("$")-1
let idx=col(".")+1
while idx<=cols
if synIDattr(diff_hlID(line, idx), "name")=="DiffText" && synIDattr(diff_hlID(line,idx-1), "name")!="DiffText"
call setpos(".", [0,line,idx])
echo line.",".idx.",".cols
break
elseif idx==cols
let line=line(".")
let cols=col(".")
exec "normal ]c"
if line==line(".") && cols==col(".")
return
elseif synIDattr(diff_hlID(".", 1), "name")=="DiffChange" || synIDattr(diff_hlID(".", 1), "name")=="DiffText"
echoerr "inner"
call Jump2NextDiffText()
endif
break
else
let idx = idx+1
endif
endwhile
else
let line=line(".")
let cols=col(".")
exec "normal ]c"
if line==line(".") && cols==col(".")
return
elseif synIDattr(diff_hlID(".", 1), "name")=="DiffChange" || synIDattr(diff_hlID(".", 1), "name")=="DiffText"
call Jump2NextDiffText()
endif
endif
endfunction
" @see http://vim.wikia.com/wiki/Selecting_changes_in_diff_mode
if &diff
nmap <F7> :call Jump2PrevDiffText()<cr>
nmap <F8> :call Jump2NextDiffText()<cr>
else
map <F7> :cp<cr>
map <F8> :cn<cr>
endif
@hotoo
Copy link
Author

hotoo commented Aug 29, 2010

Diff 模式下,让光标直接跳转到被修改的文本处。

v1: 仅支持跳转到 DiffChange 的第一个 DiffText
v2: 支持 DiffChange 行多个 DiffText

http://hotoo.github.com/blog/jump-to-difftext.html

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment