Taking the difference between two named files in vim

In my project regression settings, the output file has an instruction like

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

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

Currently, I need to open the output file in vim first. Then I need to select the line defining the diff found. after returning to the shell. then take vimdiff b / w these files.

+4
source share
2 answers

If you did not have an open file or unmodified buffer:

  :edit file1.txt :vert diffsplit file2.txt 

To open diff in a new tab,

  :tabedit file1.txt :vert diffsplit file2.txt 

it would be very convenient


To automate the work, I would think

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

Notes:

  • does not work for file names with special characters (especially spaces).
  • To open all these vims at once, simply add & : gvim -d "$file1" "$file2"&

You can also get all the differences to open in separate tabs in one vim:

  gvim --servername GVIM --remote-silent +"tabedit $file1" +"vert diffsplit $file2" 
+1
source

You can do this without opening a new instance of vim 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 :<Cu>call <SID>OpenDiff()<CR> 

If you add set bufhidden=wipe after each of the execute , you can get rid of open buffers by running :tabclose .

+2
source

All Articles