Bash – Comparing files with vimdiff from a script

bashshell-scriptvimvimdiff

I'm writing a script to compare two directories recursively and run vimdiff when it finds a difference:

#!/bin/bash

dir1=${1%/}
dir2=${2%/}

find "$dir1/" -type f -not -path "$dir1/.git/*" | while IFS= read line; do
    file1="$line"
    file2=${line/$dir1/$dir2}

    isdiff=$(diff -q "$file1" "$file2")

    if [ -n "$isdiff" ]; then
        vimdiff "$file1" "$file2"
    fi
done

This doesn't work because vim throws a warning: "Input is not from a terminal." I understand that I need to supply the - argument, which is kind of tricky, but I have it more or less working:

#!/bin/bash

dir1=${1%/}
dir2=${2%/}

find "$dir1/" -type f -not -path "$dir1/.git/*" | while IFS= read line; do
    file1="$line"
    file2=${line/$dir1/$dir2}

    isdiff=$(diff -q "$file1" "$file2")

    if [ -n "$isdiff" ]; then
        cat "$file1" | vim - -c ":vnew $file2 | windo diffthis"
    fi
done

The problem with this is the right side of the diff window is a new file. I want to compare the original file in dir1 to the original file in dir2. How can I do this?

Best Answer

vim and therefore vimdiff seem to assume that stdin is an tty. You can workaround this by something like this in your script:

</dev/tty vimdiff "$file1" "$file2"
Related Question