もた日記

くだらないことを真面目にやる

Linuxメモ : icdiffコマンドで横並び、文字単位で差分を表示

icdiff


icdiff: side-by-side highlighted command line diffs

github.com

2つのファイルを横並び(サイドバイサイド)で比較する場合、diff -yを使う、vimdiffを使う、WindowsならWinMergeなどのソフトを使う方法があるが、今回はicdiffコマンドを試してみる。
icdiffを使うとコマンドラインで図のように横並び、文字単位で差分を表示することができる。icdiffはImproved Colored Diffの略だと思われる。

f:id:wonder-wall:20170808212122p:plain

インストール


pipでインストールできるので下記コマンドを実行。

$ pip install icdiff

またはここからバイナリをダウンロードしてパスが通っているディレクトリに置く。
ヘルプメッセージは以下の通り。

$ icdiff --help
Usage: icdiff [options] left_file right_file

Show differences between files in a two column view.

Options:
  --version             show program's version number and exit
  -h, --help            show this help message and exit
  --cols=COLS           specify the width of the screen. Autodetection is Unix
                        only
  --encoding=ENCODING   specify the file encoding; defaults to utf8
  --head=HEAD           consider only the first N lines of each file
  -H, --highlight       color by changing the background color instead of the
                        foreground color.  Very fast, ugly, displays all
                        changes
  -L LABELS, --label=LABELS
                        override file labels with arbitrary tags. Use twice,
                        one for each file
  -N, --line-numbers    generate output with line numbers
  --no-bold             use non-bold colors; recommended for with solarized
  --no-headers          don't label the left and right sides with their file
                        names
  --output-encoding=OUTPUT_ENCODING
                        specify the output encoding; defaults to utf8
  --recursive           recursively compare subdirectories
  --show-all-spaces     color all non-matching whitespace including that which
                        is not needed for drawing the eye to changes.  Slow,
                        ugly, displays all changes
  --tabsize=TABSIZE     tab stop spacing
  -u, --patch           generate patch. This is always true, and only exists
                        for compatibility
  -U NUM, --unified=NUM, --numlines=NUM
                        how many lines of context to print; can't be combined
                        with --whole-file
  -W, --whole-file      show the whole file instead of just changed lines and
                        context


使い方


diffと同じように2つのファイルを指定すればよい。図のように追加、削除、変更が色分けして表示される。

f:id:wonder-wall:20170808214756p:plain

文字色ではなく背景色を変更したい場合は-Hを指定し、行番号を表示したい場合は-Nを指定する。差分付近だけを表示したい場合は-Uに続けて行数を指定する。

f:id:wonder-wall:20170808214811p:plain

git diffで使う



git diffで使う場合は、

$ git difftool --extcmd icdiff

とするか、git-icdiffをパスが通っているディレクトリに置いて(pipでインストールした場合は使えるようになっているはず)、

$ git icdiff

を実行する。