I often want to compare directories. I need to know which files only exist in one of them and which files differ. If any files differ, I sometimes want to see the actual change so I need an efficient way to do so.

For starters, I am using this drq alias to diff directories:

$ ls -1 A B # show both folder's content with ls


$ drq A B # diff both folders
Only in A: aonly
Only in B: bonly
Files A/contentmatters and B/contentmatters differ

The alias drq expands to diff -r -q which executes a recursive (-r) diff that only lists changes (-q, i.e. quiet).

In order to inspect file changes, I use vimdiff. By default, one could simply run vimdiff file1 file2. However, when I don’t know in advance whether the files do have differences I don’t want to actually fire up Vim unless they do. Thus, my ~/.functions contains the following definition:

vd () {
    diff $@ > /dev/null
    if [[ $? -eq 1 ]]
        vimdiff -c 'windo set syntax=off' $@

This simply first diffs the files in question and opens Vim only if diff finds any differences . Running vd file1 file1 will just return without starting vim. This approach causes the two files to be diff’ed twice but I rarely experience any waiting times. (I turn off the syntax in diff view to not get distracted with windo set syntax=off.)

Finally, I have another seemingly strange function defined:

Files () {
    vd $1 $3

Files is now a function which runs my previously mentioned vd on its first and third argument. Go back to drq and guess ;-).

Found it? Well, once the recursive diff told me that two files differ and I want to compare them, I can simply copy the whole line with the two files, paste it and execute it as a new command. The Files function will forward its “arguments” to vimdiff as needed.

Using Tmux in visual mode, this looks like this:

Tmux visual mode copy and paste

After executing the drq A B, I have to type <prefix>[k<Space>0<Enter><prefix>] (prefix is the Tmux prefix, of course):

         Move     Jump to
         up       Line Start  Paste
         |        |           |
    |        |       |
Open Tmux    Start   Copy
Visual Mode  Selection

This is much faster than you would expect. Hitting Enter at this point will open up Vim and show the changes. Instead of pressing k, you can also jump higher with 3k for example.

You might wonder why Files uses my custom vd instead of plain vimdiff. The only reason for that is that sometimes the files in questions have changed after the recursive diff’s execution and this makes sure Vim is not started for no reason.