开发者

Is there any way to queue many pairs of files for comparison with vimdiff?

I have a few dozen files I need to compare and merge with vimdiff. Is there any way to queue pairs of files for comparison so that I may open them all at once, rather th开发者_如何学Pythonan returning to the shell again and again to open each pair of files?

Could this be done by opening each pair in its own tab? My shell is Bash.


If you want to compare all the files in a directory with each other in all combinations, here's how to do that and still avoid comparing a pair twice (or a file with itself):

for f1 in *; do 
  for f2 in *; do 
    [[ $f1 < $f2 ]] || vimdiff "$f1" "$f2"
  done
done


This is doable without any shell shenanigans by setting all the diffy options globally (along with some autocommands, because vim gets angry if you try to diff more than 8 files at once)

vim -c 'windo set diff scrollbind cursorbind scrollopt+=hor nowrap foldmethod=diff foldcolumn=2 | au BufWinEnter * setlocal diff | au BufWinLeave * setlocal nodiff' -O2 your filenames here

This will open all your files in buffers, and vertically split vim into two windows (use -o2 if you want horizontal), and always be in diff mode, even if you switch between buffers or open new files.

If you're merging all your files together into one super-file, then using :bn will be fine for getting around.

If you're merging pairs of files, then you'll want some variation of :windo bn N, where N depends on how you listed your files:

  • For A1 A2 B1 B2 C1 C2, :windo bn 2 will do the right thing
  • For A1 B1 C1 A2 B2 C2, you'll need to initially go to the A2 buffer, and then :windo bn 1 will do the right thing

I pulled the settings from the help file:

In each of the edited files these options are set:

'diff'    on
'scrollbind'  on
'cursorbind'  on
'scrollopt' includes "hor"
'wrap'    off
'foldmethod'  "diff"
'foldcolumn'  value from 'diffopt', default is 2

These options are set local to the window. When editing another file they are reset to the global value.

And only discovered the need for the autocommand nonsense empirically.


Here is another answer that opens each pair in it's own tab. Save again the following in a file (do2.sh):

#!/bin/bash

files1=( file1.txt file2.txt file3.txt )
files2=( file1_.txt file2_.txt file3_.txt )

cmd="vim -c 'set diffopt=filler,vertical' -c 'edit ${files1[0]}' -c 'diffsplit ${files2[0]}' "
echo $cmd
for i in {1..2}; do
  cmd="${cmd} -c 'tabe ${files1[i]}' -c 'diffsplit ${files2[i]}' "
done

eval $cmd

when you execute it will open the pairs in their own tab.


You can't do it using a single vim command as far as I am aware. You could start vimdiff from within a for loop.

Alternatively try "meld" http://meld.sourceforge.net/


Like StephenPaulger suggested a for loop would work well for a small number of files. The way that I would do it is to create a small script (say do.sh) with the following contents:

#!/bin/bash

files1=( file1.txt file1_.txt )
files2=( file2.txt file2_.txt )
for i in {0..1} ; do
  vimdiff ${files1[i]} ${files2[i]}
done

this would allow you to not have to type anything in the shell while doing the diff for the different files, once you exit the diff for one pair of files the other shows up right away.

Another useful tip to use here is to put the following in your .vimrc

if &diff
   map <f4> :qa<cr>
   map <f5> :wqa!<cr>
   map <f6> :qa!<cr>
 endif

then if there are no changes you just press f4 to exit, if there are changes you press f5.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜