如何在命令行上爲diff着色?

本文翻譯自:How to colorize diff on the command line?

When I have a diff, how can I colorize it so that it looks good? 當我有一個差異時,如何給它上色以使其看起來不錯? I want it for the command line, so please no GUI solutions. 我希望在命令行使用它,因此請不要使用GUI解決方案。


#1樓

參考:https://stackoom.com/question/avQo/如何在命令行上爲diff着色


#2樓

And for those occasions when a yum install colordiff or an apt-get install colordiff is not an option due to some insane constraint beyond your immediate control, or you're just feeling crazy , you can re-invent the wheel with a line of sed: 對於那些由於無法立即控制的瘋狂限制而無法執行yum install colordiffapt-get install colordiff情況, 或者您只是感到瘋狂 ,可以用sed線重新發明輪子:

sed 's/^-/\x1b[41m-/;s/^+/\x1b[42m+/;s/^@/\x1b[34m@/;s/$/\x1b[0m/'

Throw that in a shell script and pipe unified diff output through it. 將其扔到一個shell腳本中,並通過它通過管道傳遞統一的diff輸出。

It makes hunk markers blue and highlights new/old filenames and added/removed lines in green and red background, respectively. 它使大塊標記變爲藍色,並分別以綠色和紅色背景突出顯示新的/舊的文件名以及添加/刪除的行。 1 And it will make trailing space 2 changes more readily apparent than colordiff can. 1它將使尾隨空間2的變化比colordiff更加容易顯現。


1 Incidentally, the reason for highlighting the filenames the same as the modified lines is that to correctly differentiate between the filenames and the modified lines requires properly parsing the diff format, which is not something to tackle with a regex. 1順便說一句,突出顯示文件名與修改後的行相同的原因是,要正確區分文件名和修改後的行需要正確地解析diff格式,而使用正則表達式則無法解決。 Highlighting them the same works "well enough" visually and makes the problem trivial. 突出顯示它們的效果在視覺上“足夠好”,並使問題變得微不足道。 That said, there are some interesting subtleties . 也就是說,有一些有趣的微妙之處

2 But not trailing tabs. 2但不要尾隨製表符。 Apparently tabs don't get their background set, at least in my xterm. 顯然,至少在我的xterm中,選項卡沒有設置背景。 It does make tab vs space changes stand out a bit though. 它確實使製表符與空間的更改脫穎而出。


#3樓

You can change the subversion config to use colordiff 您可以更改subversion配置以使用colordiff

~/.subversion/config.diff 〜/ .subversion / config.diff

 ### Set diff-cmd to the absolute path of your 'diff' program.
 ###   This will override the compile-time default, which is to use
 ###   Subversion's internal diff implementation.
-# diff-cmd = diff_program (diff, gdiff, etc.)
+diff-cmd = colordiff

via: https://gist.github.com/westonruter/846524 通過: https : //gist.github.com/westonruter/846524


#4樓

Actually there seems to be yet another option (which I only noticed recently, when running into the problem described above): 實際上,似乎還有另一種選擇(當遇到上述問題時,我最近才注意到):

git diff --no-index <file1> <file2>
# output to console instead of opening a pager
git --no-pager diff --no-index <file1> <file2>

If you have Git around (which you already might be using anyway), then you will be able to use it for comparison, even if the files themselves are not under version control. 如果周圍有Git(無論如何您可能已經在使用它),那麼即使文件本身不受版本控制,也可以將其用於比較。 If not enabled for you by default, then enabling color support here seems to be considerably easier than some of the previously mentioned workarounds. 如果默認情況下未爲您啓用,那麼在這裏啓用顏色支持似乎比前面提到的某些解決方法容易得多。


#5樓

Coloured, word-level diff ouput 彩色字級 diff輸出

Here's what you can do with the the below script and diff-highlight : 這是使用以下腳本和diff-highlight可以執行的操作:

彩色差異截圖

#!/bin/sh -eu

# Use diff-highlight to show word-level differences

diff -U3 --minimal "$@" |
  sed 's/^-/\x1b[1;31m-/;s/^+/\x1b[1;32m+/;s/^@/\x1b[1;34m@/;s/$/\x1b[0m/' |
  diff-highlight

(Credit to @retracile's answer for the sed highlighting) (對sed突出顯示使用@retracile的答案


#6樓

Since wdiff accepts args specifying the string at the beginning and end of both insertions and deletions, you can use ANSI color sequences as those strings: 由於wdiff接受在插入和刪除操作的開頭和結尾都指定字符串的args,因此可以將ANSI顏色序列用作這些字符串:

wdiff -n -w $'\033[30;41m' -x $'\033[0m' -y $'\033[30;42m' -z $'\033[0m' file1 file2

For example, this is the output of comparing two CSV files: 例如,這是比較兩個CSV文件的輸出:

CSV文件的差異輸出

Example from https://www.gnu.org/software/wdiff/manual/html_node/wdiff-Examples.html 來自https://www.gnu.org/software/wdiff/manual/html_node/wdiff-Examples.html的示例

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章