4

In my project regression settings, output file have statement like

    "diff between foo.txt and bar.txt found" 

Now I need to take vimdiff between foo.txt and bar.txt. Can I do it from output file opened in vim only?

currently I need to first open my output file in vim. Then I need to select line specifying diff found. after it return to shell. then take vimdiff b/w this files.

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
shampa
  • 1,350
  • 3
  • 13
  • 22
  • So you want vim to automatically diff the files based on the contents of the string "diff between foo.txt and bar.txt found"? – sashang Oct 13 '11 at 04:00

2 Answers2

2

You can do it without opening any new vim instance using the following function:

function s:OpenDiff()
    let line=getline('.')
    let match=matchlist(line, '\v^\ {4}\"diff\ between\ (.{-})\ and\ (.{-})\ found\"\ $')[1:2]
    if empty(match)
        throw 'Incorrect line:' line
    endif
    execute 'tabedit'   fnameescape(match[0])
    execute 'diffsplit' fnameescape(match[1])
endfunction
nnoremap ,od :<C-u>call <SID>OpenDiff()<CR>

If you add set bufhidden=wipe after each of execute statements you will be able to get rid of opened buffers by running :tabclose.

ZyX
  • 52,536
  • 7
  • 114
  • 135
1

If you had no file opened or an unmodified buffer:

 :edit file1.txt
 :vert diffsplit file2.txt

To open diffs in a new tab,

 :tabedit file1.txt
 :vert diffsplit file2.txt

would be very convenient


To get things automated, I'd consider

diffprogram | grep -w '^diff between' | grep 'found$' | 
while read diff between file1 and file2 found;
do
    gvim -d "$file1" "$file2"
done

Notes:

  • doesn't work for filenames with speciall characters (notably whitespace)
  • To open all these vims simultaneously, just add &: gvim -d "$file1" "$file2"&

You can also get all diffs to open in separate tabs in a single vim:

    gvim --servername GVIM --remote-silent +"tabedit $file1" +"vert diffsplit $file2"
sehe
  • 374,641
  • 47
  • 450
  • 633