Wikia

Vim Tips Wiki

Changes: Jump to file from CVSDiff output

Edit

Back to page

(Remove html character entities)
(change vim.org/scripts URL to use template + minor manual clean)
 
Line 4: Line 4:
 
|previous=490
 
|previous=490
 
|next=493
 
|next=493
|created=June 24, 2003
+
|created=2003
 
|complexity=basic
 
|complexity=basic
 
|author=daniel kullmann
 
|author=daniel kullmann
|version=5.7
+
|version=6.0
 
|rating=1/1
 
|rating=1/1
 
|category1=
 
|category1=
Line 19: Line 19:
   
 
Pressing Enter will execute that script.
 
Pressing Enter will execute that script.
 
 
<pre>
 
<pre>
 
function! DiffJumpToFile()
 
function! DiffJumpToFile()
Line 39: Line 38:
   
 
==Comments==
 
==Comments==
Some time ago I wrote ftplugin which does exactly that. It can jump exactly to the place where you have been looking at the diff. It supports unified diffs only so far. It supports mercurials a/file b/file. http://www.vim.org/scripts/script.php?script_id=1888
+
Some time ago I wrote ftplugin which does exactly that. It can jump exactly to the place where you have been looking at the diff. It supports unified diffs only so far. It supports Mercurial's a/file b/file. {{script|id=1888}}
   
 
----
 
----

Latest revision as of 10:55, August 11, 2011

Tip 492 Printable Monobook Previous Next

created 2003 · complexity basic · author daniel kullmann · version 6.0


I use :CVSDiff from cvscommand.vim quite often to get an overview of the changes I made to a file.

I always want to jump from the diff to the corresponding line in the original file.

So I wrote a small script that does that, and put it in .vim/after/ftplugin/diff.vim

Pressing Enter will execute that script.

function! DiffJumpToFile()
  let a=line(".") " current line number
  let b=search("^\\(---\\|\\*\\*\\*\\) ", "b") " search for line like *** 478,489 ***
  let c=getline(b) " get this line as string
  let d=strpart(c, 4, match(c, ",")-4) " get the first line number (478) from that string
  let f=search("^\\(---\\|\\*\\*\\*\\) .*\\t", "b") " search for line like *** fileincvs.c ....
  let g=getline(f) " get this line as string
  let h=match (g, "\\t", 4) " look for end of filename (terminated by tab) in string
  let i=strpart(g, 4, h-4) " get the filename
  execute ":b " . i | " change to that file
  execute "normal " . (d+a-b-1) . "G" | " go to right line number
endfunction
nmap <buffer> <CR> :call DiffJumpToFile()<CR>

For the reverse operation (get a diff for the current buffer), see VimTip1030.

CommentsEdit

Some time ago I wrote ftplugin which does exactly that. It can jump exactly to the place where you have been looking at the diff. It supports unified diffs only so far. It supports Mercurial's a/file b/file. script#1888


Around Wikia's network

Random Wiki