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.
相关问题
- DBGrid - How to set an individual background color
- Why should we check WIFEXITED after wait in order
- softlinks atime and mtime modification
- UNIX Bash - Removing double quotes from specific s
- Get unexpanded argument from bash command line
相关文章
- Emacs/xterm color annoyance on Linux
- Making new files automatically executable?
- Reverse four length of letters with sed in unix
- Compile and build with single command line Java (L
- Extracting columns from text file using Perl one-l
- matplotlib bwr-colormap, always centered on zero
- Problem with piping commands in C
- How to update command line output?
Man pages for
diff
suggest no solution for colorization from within itself. Please consider usingcolordiff
. It's a wrapper arounddiff
that produces the same output as diff, except that it augments the output using colored syntax highlighting to increase readability:or just:
Installation:
sudo apt-get install colordiff
brew install colordiff
orport install colordiff
I use
grc
(Generic Colouriser), which allows you to colour the output of a number of commands includingdiff
.It is a python script which can be wrapped around any command. So instead of invoking
diff file1 file2
, you would invokegrc diff file1 file2
to see colourised output. I have aliaseddiff
togrc diff
to make it easier.diff --color
option was added to GNU diffutils 3.4 (2016-08-08)This is the default
diff
implementation on most distros, which will soon be getting it.Ubuntu 18.04 has
diffutils
3.6 and therefore has it.On 3.5 it looks like this:
Tested:
Apparently added in commit c0fa19fe92da71404f809aafb5f51cfd99b1bee2 (Mar 2015).
I have also requested word level diff as from
diff-highlight
at: https://lists.gnu.org/archive/html/diffutils-devel/2017-01/msg00001.htmlColoured, word-level
diff
ouputHere's what you can do with the the below script and diff-highlight:
(Credit to @retracile's answer for the
sed
highlighting)And for those occasions when a
yum install colordiff
or anapt-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:Throw that in a shell script and pipe unified diff output through it.
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 space2 changes more readily apparent than colordiff can.
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. 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. Apparently tabs don't get their background set, at least in my xterm. It does make tab vs space changes stand out a bit though.
Use Vim:
Or better still, VimDiff (or
vim -d
, which is shorter to type) will show differences between two, three or four files side-by-side.Examples: