当我有一个差异时,我如何给它上色,使它看起来更好?

我想它的命令行,所以请没有GUI解决方案。


当前回答

实际上,似乎还有另一个选择(我最近才注意到,当遇到上面描述的问题时):

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

如果您身边有Git(您可能已经在使用它了),那么您将能够使用它进行比较,即使文件本身不受版本控制。如果默认情况下没有启用,那么在这里启用颜色支持似乎比前面提到的一些变通方法要容易得多。

其他回答

彩色,字级差异输出

下面是你可以用下面的脚本和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

(这要归功于@retracile对sed高亮的回答)

使用bat命令:

diff file1 file2 | bat -l diff

diff的手册页没有建议从内部进行着色的解决方案。请考虑使用色差。它是diff的包装器,产生与diff相同的输出,除了它使用彩色语法高亮显示来增强输出以增加可读性:

diff old new | colordiff

或者是:

colordiff old new

安装:

Ubuntu/Debian: sudo apt-get install colordiff OS X: brew install colordiff或port install colordiff

字符级色差: 安装ccdiff

ccdiff -r /usr/share/dict/words /tmp/new-dict

对我来说,我找到了一些解决方案:这是一个有效的解决方案

@echo off
Title a game for YouTube
explorer "https://thepythoncoding.blogspot.com/2020/11/how-to-echo-with-different-colors-in.html"
SETLOCAL EnableDelayedExpansion
for /F "tokens=1,2 delims=#" %%a in ('"prompt #$H#$E# & echo on & for %%b in (1) do rem"') do (
  set "DEL=%%a"
)
echo say the name of the colors, don't read

call :ColorText 0a "blue"
call :ColorText 0C "green"
call :ColorText 0b "red"
echo(
call :ColorText 19 "yellow"
call :ColorText 2F "black"
call :ColorText 4e "white"

goto :Beginoffile

:ColorText
echo off
<nul set /p ".=%DEL%" > "%~2"
findstr /v /a:%1 /R "^$" "%~2" nul
del "%~2" > nul 2>&1
goto :eof

:Beginoffile