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

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


当前回答

diff——颜色选项(添加到GNU diffutils 3.4在2016-08-08)

这是大多数发行版的默认差异实现,很快就会得到它。

Ubuntu 18.04 (Bionic Beaver)有困难3.6,因此有它。

在3.5版本中是这样的:

测试:

diff --color -u \
  <(seq 6 | sed 's/$/ a/') \
  <(seq 8 | grep -Ev '^(2|3)$' | sed 's/$/ a/')

显然是在提交c0fa19fe92da71404f809aafb5f51cfd99b1bee2(2015年3月)中添加的。

也能进行比较

像diff-highlight。这似乎是不可能的,但有一个功能请求:https://lists.gnu.org/archive/html/diffutils-devel/2017-01/msg00001.html

相关问题:

使用'diff'(或其他任何东西)来获取文本文件之间的字符级差异 https://unix.stackexchange.com/questions/11128/diff-within-a-line https://superuser.com/questions/496415/using-diff-on-a-long-one-line-file

不过Ydiff就是这么做的。见下文。

Ydiff并排字级别差异

https://github.com/ymattw/ydiff

这是涅槃吗?

python3 -m pip install --user ydiff
diff -u a b | ydiff -s

结果:

如果行太窄(默认为80列),适合屏幕:

diff -u a b | ydiff -w 0 -s

测试文件内容:

a

1
2
3
4
5 the original line the original line the original line the original line
6
7
8
9
10
11
12
13
14
15 the original line the original line the original line the original line
16
17
18
19
20

b

1
2
3
4
5 the original line the original line the original line the original line
6
7
8
9
10
11
12
13
14
15 the original line the original line the original line the original line
16
17
18
19
20

西cyff退出

ydiff与Git集成,无需任何配置。

在Git存储库中,你可以做的不是Git diff,而是:

ydiff -s

而不是git log:

ydiff -ls

请参见:当我执行“git diff”时,我如何获得一个并排diff ?

在Ubuntu 16.04 (Xenial Xerus), Git 2.18.0和ydiff 1.1上测试。

其他回答

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

@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

到目前为止还没有人提到delta。它支持带有语法高亮的语法彩色差异视图。

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

使用bat命令:

diff file1 file2 | bat -l diff

我最喜欢的选择是vdiff <file1> <file2>函数(我忘记从哪里得到它)。

它将在Vim中并排打开两个窗口,以清楚地看到两个文件之间的差异。

vdiff () {
    if [ "${#}" -ne 2 ] ; then
        echo "vdiff requires two arguments"
        echo "  comparing dirs:  vdiff dir_a dir_b"
        echo "  comparing files: vdiff file_a file_b"
        return 1
    fi

    local left="${1}"
    local right="${2}"

    if [ -d "${left}" ] && [ -d "${right}" ]; then
        vim +"DirDiff ${left} ${right}"
    else
        vim -d "${left}" "${right}"
    fi
}

将这个脚本放在您的(.alias)或(.zshrc)中,然后使用 Vdiff <file1> <file2>. Vdiff <file1>。

例子

结果如下: