bat --diff for two files

Created on 27 Apr 2020  路  3Comments  路  Source: sharkdp/bat

The new --diff feature looks very cool.

I could not figure out how to use it with two files. Is there a way yet? Or a workaround?
bat --diff a.txt b.txt does not work.

question

Most helpful comment

Unfortunately, the --diff feature is only intended to filter out lines that haven't been changed since the last time the file was committed to the git index.

 -d, --diff
      Only show lines that have been added/removed/modified with respect
       to the Git index. Use --diff-context=N to control how much context
       you want to see.

If you're looking to highlight a diff between two files, you might want to consider using delta (diff -u a.txt b.txt | delta), or diff a.txt b.txt | bat -ldiff.

All 3 comments

Unfortunately, the --diff feature is only intended to filter out lines that haven't been changed since the last time the file was committed to the git index.

 -d, --diff
      Only show lines that have been added/removed/modified with respect
       to the Git index. Use --diff-context=N to control how much context
       you want to see.

If you're looking to highlight a diff between two files, you might want to consider using delta (diff -u a.txt b.txt | delta), or diff a.txt b.txt | bat -ldiff.

You could technically work around it by creating a new git repository and artificially creating a difference between the index and the workspace, but it's not pretty and has some noticeable overhead:

#!/usr/bin/env bash
TEMP="$(mktemp -d)"
FILENAME="$(basename "$1")"
(cd "$TEMP" && git init -q)
cp "$1" "$TEMP/$FILENAME"
(cd "$TEMP" && git add "$FILENAME" && git commit -q -m "Temporary")
cp "$2" "$TEMP/$FILENAME"
(cd "$TEMP" && bat --diff "$FILENAME")
rm -rf "$TEMP"

Which you would then call with ./script.sh a.txt b.txt.


Overhead:

$ time ./script.sh a.txt b.txt
Executed in  306.34 millis    fish           external
   usr time   98.97 millis  172.00 micros   98.80 millis
   sys time   55.54 millis  527.00 micros   55.01 millis


$ time diff -u test.sh test.sass | delta --paging=never
Executed in   74.56 millis    fish           external
   usr time   66.00 millis  230.00 micros   65.77 millis
   sys time    8.03 millis  802.00 micros    7.22 millis

Okay, so there is no solution now.
I use diff-so-fancy at the moment. But it seems that bat can't replace that at the moment

Thanks a lot! :)

Was this page helpful?
0 / 5 - 0 ratings

Related issues

rien333 picture rien333  路  3Comments

antoinemadec picture antoinemadec  路  3Comments

adamtabrams picture adamtabrams  路  3Comments

scalp42 picture scalp42  路  3Comments

yum-feng picture yum-feng  路  3Comments