突出显示更改的行以及每个更改行中更改的字节
开源项目 Trac 有一个出色的差异荧光笔 - 它突出显示更改的行以及每个更改行中更改的字节!请参阅此处或此处 作为示例。
有没有办法在 bash 终端、git
或 vim
中使用相同的颜色突出显示(即更改行和更改字节)用于 diff 输出(补丁文件)?
Open Source project Trac has an excellent diff highlighter — it highlights changed lines and changed bytes in each changed line! See here or here for examples.
Is there way to use the same color highlight (i.e. changed lines and changed bytes too) in bash terminal, git
, or vim
for diff output (patch-file)?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(14)
diff-highlight
Perl contrib 脚本 生成的输出与 Trac 屏幕截图非常相似,因此 Trac 很可能正在使用它:安装方式:
将文件
diff-highlight
移动到~/bin/
目录(或$PATH
所在的任何位置),然后然后将以下内容添加到您的~/.gitconfig
中:@cirosantilli 建议的单个复制粘贴安装:
The
diff-highlight
Perl contrib script produces output so similar to that of the Trac screenshots that it is likely that Trac is using it:Install with:
Move the file
diff-highlight
to the~/bin/
directory (or wherever your$PATH
is), and then add the following to your~/.gitconfig
:Single copy paste install suggested by @cirosantilli:
使用
git diff
或git log
以及其他可能的选项时,请使用选项--word-diff=color
(还有其他用于单词差异的模式顺便提一句)While using
git diff
orgit log
and possibly others, use option--word-diff=color
(there are also other modes for word diffs BTW)diff-so-fancy
是一个diff-专为人眼设计的荧光笔。
它删除了前导
+
/-
(这对于剪切/粘贴来说很烦人),并使文件之间的部分变得清晰。彩色
git
(左)与diff-so-fancy
(右 - 注意字符级突出显示):如果您想要< a href="https://github.com/so-fancy/diff-so-fancy" rel="noreferrer">
diff-so-fancy
(右侧)输出,但是不限于git
存储库中的文件,请将以下函数添加到.bashrc
中以在任何文件上使用它:例如:
字符级别突出显示和标准
diff< /code> 格式
如果您不喜欢
diff-so-fancy
的非标准格式,但仍希望字符级git
突出显示,请使用diff-highlight
它将采用git
的输出并产生非常漂亮的标准diff
-格式输出:要从
git
默认使用它,请添加到您的.gitconfig
: >[pager] 部分告诉
git
将其已经着色的输出通过管道传输到diff-highlight
,后者在字符级别着色,然后将输出分页到 less (如果需要),而不是仅仅使用默认的less
。diff-so-fancy
is adiff
-highlighter designed for human eyeballs.It removes the leading
+
/-
which are annoying for cut/paste and makes clear sections between files.Coloured
git
(left) vsdiff-so-fancy
(right - note the character-level highlights):If you want the
diff-so-fancy
(right side) output but not constrained to files in agit
repository, add the following function to your.bashrc
to use it on any files:Eg:
Character level highlighting and standard
diff
formatIf you don't like the non-standard formatting of
diff-so-fancy
, but still want character-levelgit
highlighting, usediff-highlight
which will takegit
's output and produce the really pretty standarddiff
-format output:To use it by default from
git
, add to your.gitconfig
:The
[pager]
section tellsgit
to pipe its already colourised output todiff-highlight
which colourises at the character level, and then pages the output in less (if required), rather than just using the defaultless
.自 v1.7.81 起,基于字节差异的实用程序已随官方 Git 一起分发。您只需找到它在计算机上的安装位置并启用它即可。
查找 Git 的安装位置
/usr/local/opt/git (更高版本:
/opt/homebrew/Cellar/git/VERSION
)ll $(which git)
或locate git
应该会有所帮助。将
diff-highlight
链接到您的 bin 目录,以便您的 PATH 可以找到它在 Git 配置中启用它
1 com/git/git/commit/927a13fe87e4b2e0edabb2f8615f0851f70fbbd8#diff-49404b1873b5e4ceb57f209c3ebd7762" rel="nofollow noreferrer">v1.7.8 版本,但是 从那时起已经进行了很多更改。
A utility for byte-based diffs has been distributed with official Git since v1.7.81. You just have to locate where it is installed on your machine and enable it.
Find where Git is installed
/usr/local/opt/git
(later versions:/opt/homebrew/Cellar/git/VERSION
)cd / && pwd -W
to find the install directory.ll $(which git)
orlocate git
should help.Link
diff-highlight
to your bin directory so that your PATH can find itEnable it in your Git config
1 Here is the v1.7.8 version, but lots of changes have been made since then.
您想要的行为现在可以在 git 本身中使用(正如 naught101 在评论中指出的那样)。要启用它,您需要将寻呼机设置为
/usr/share/doc/git/contrib/diff-highlight/diff-highlight
是 Ubuntu 13.10 上荧光笔脚本的位置(我没有知道为什么它位于doc
文件夹中)。如果您的系统上不存在,请尝试使用locate diff-highlight
来查找它。请注意,突出显示脚本不可执行(至少在我的机器上),因此需要perl
。要始终对各种类似 diff 的命令使用荧光笔,只需将以下内容添加到您的
~/.gitconfig
文件中:我将其添加为新答案 naught101 的评论已被隐藏,因为设置并不完全应该是微不足道的,至少在 Ubuntu 版本上,我在 README 中有说明 不 工作。
The behaviour you want is now available in git itself (as was pointed out in a comment by naught101). To enable it you need to set your pager to
where
/usr/share/doc/git/contrib/diff-highlight/diff-highlight
is the location of the highlighter script on Ubuntu 13.10 (I have no idea why it's in adoc
folder). If it isn't there on your system try usinglocate diff-highlight
to find it. Note that the highlighting script is not executable (at least on my machine), hence the requirement forperl
.To always use the highlighter for the various diff-like commands just add the following to your
~/.gitconfig
file:I added this as a new answer naught101's comment is buried and because the set up is not quite as trivial as it should be and at least on the version of Ubuntu that I have the instructions in the README don't work.
我使用
--color-words
选项,它对我来说效果很好:I use
--color-words
option and it works fine for me :正如@dshepherd 所说:
但是
diff-highlight
位于 DOC 中,无法从 shell 中获得。要将
diff-highlight
安装到您的~/bin
目录中,请按照以下步骤操作(这将节省您的输入):然后将您的
.gitconfig
配置为官方版本医生说:UPD
您也可以在不进行任何安装的情况下在最新的
git
上尝试下一步:更复杂:
as @dshepherd says:
But
diff-highlight
is located in DOC and is not available from shell.To install
diff-highlight
into your~/bin
directory follow next steps (This will save your typing):Then configure your
.gitconfig
as official doc says:UPD
Also you can try next on latest
git
without any installation:More complex:
在对类似但略有不同的问题的回答中,我建议使用。 com/dandavison/delta" rel="noreferrer">Delta,这是一种现代差异后处理工具,专门支持同时突出显示单词和行的特殊需求。
Delta 具有高度可配置性(使用
diff-highlight
和diff-so-fancy
的模拟模式),并包括许多其他工具中没有的功能:并排视图、语法突出显示和着色合并冲突和gitblame
输出。Delta 文档还有一个相关项目概述,其中提到了更多广告-可以突出显示单词和线条的特殊工具。
In an answer to a similar, but slightly different question, I suggest using Delta, which is a modern diff postprocessing tool that specifically supports the special desire for highlighting both words and lines at the same time.
Delta is highly configurable (with emulation modes for
diff-highlight
anddiff-so-fancy
) and includes many features not found in other tools: side-by-side views, syntax highlighting, and coloring of merge conflicts andgit blame
output.The Delta documentation also has an overview of related projects that mentions a few more ad-hoc tools that can highlight both words and lines.
Emacs 具有 ediff-patch-buffer 功能,应该可以满足您的需求。
在 emacs 中打开未打补丁的文件,输入 ESC-x, ediff-patch-buffer。
按照提示操作,您应该会看到文件的修补版本和原始版本的突出显示比较。
根据您的评论,以下内容将为您提供一个仅需要 dwdiff 的 bash 解决方案:
Emacs has the ediff-patch-buffer function which should fulfill your needs.
Open the un-patched file in emacs type ESC-x, ediff-patch-buffer.
Follow the prompts and you should see a highlighted comparison of the patched and original versions of your file.
As per your comment the following will will give you a bash solution requiring only dwdiff:
Diffy
GitLab 正在使用 Diffy https://github.com/samg/diffy (Ruby) 实现类似于 GitHub 和 diff-highlight 的输出:
Diffy 使用与 Git 相同的算法,并支持不同类型的输出,包括 GitLab 使用的 HTML 输出:
输出:
注意如何将
strong
添加到更改的字节中。Diffy
GitLab is using Diffy https://github.com/samg/diffy (Ruby) to achieve output similar to GitHub and diff-highlight:
Diffy makes the diff itself using the same algorithm ad Git, and supports different types of outputs, including the HTML output that GitLab uses:
Output:
Note how
strong
was added to the changed bytes.注意:这与此处找到的内容重复:如何改进 git 的差异突出显示? .不过,也在这里发布我的答案,因为它可能对一些直接找到此线程的人有帮助:)
正如之前的一些答案中所说,只有 git 的东西才有可能实现这一点。我发布此内容是因为根据您的系统,说明可能更容易遵循,但这与其他几个答案类似。
一种完全依赖 git 及其贡献的解决方案。除了 git 附带的文件之外,这不需要其他文件。所有解释均针对 Ubuntu(在 18.04LTS 上测试),在其他 Linux 系统上应该类似地工作:
在我的系统上,唯一有效的答案是:
~/.gitconfig
以获得您想要的结果,方法是添加(注意这些是 TABS,而不是 4 个空格):Note: this is a duplicate of what is found here: How to improve git's diff highlighting? . Posting my answer here too though, as it may be helpful to some people who find directly this thread :)
As said in some previous answers, this is possible with only git stuff. I post this as the instructions may be a bit easier to follow depending on your system, but this is similar to several other answers.
One solution that is purely relying on git and its contribs. This requires no additional files than what comes with git. All explanations are for Ubuntu (tested on 18.04LTS), should work similarly on other linux systems:
on my system the only valid answer is:
~/.gitconfig
to get the result you want, by adding (note these are TABS, not 4 spaces):当您预览差异时,我的 vim 插件 vim-gitgutter 会执行此操作。您可以在自述文件的屏幕截图中看到一个简单的示例。
计算行内差异的代码在这里< /a>.
My vim plugin vim-gitgutter does this when you preview a diff. You can see a simple example in the screenshot in the readme.
The code that calculates the intra-line differences is here.
有些工具可能已经提到过,但我将尝试在这里总结并添加一些额外的提示:
Bash/Terminal
您可以使用
colordiff
或diff-so-fancy
安装
选择取决于您的系统
使用
Git
如果您的 diff 尚未突出显示输出,可以使用以下命令进行配置:
Vim
Vim 有一个内置突出显示功能,可以通过编辑
~/.vimrc
并添加以下内容来启用它lines:为了突出显示确切的字节,需要一个额外的插件,有多个插件可用于此目的,即
diffchar.vim
或vim-gitgutter
Some of the tools may already mentioned but I will try to summarise and add some extra tips here:
Bash/Terminal
You can use the
colordiff
ordiff-so-fancy
Install
Pick depending on your system
Usage
Git
In case your diff not already highlight the output this can configured with:
Vim
Vim has a build-in highlighting that can be enabled via editing the
~/.vimrc
and adding the following lines:For highlighting the exact bytes it is required an extra plugin there are multiple plugins available for this purpose ie
diffchar.vim
orvim-gitgutter
是的,Vim 会执行此操作,包括突出显示行内更改的文本。
有关如何比较文件的更多详细信息,请参阅
:h diff
和:h 08.7
。Vim 使用相当简单的算法来突出显示。
它在行中搜索第一个更改的字符,然后搜索最后一个更改的字符,并简单地突出显示它们之间的所有字符。
这意味着每行不能有多个突出显示 - Vim 中的许多设计决策都优先考虑效率。
Yes, Vim does this including the highlighting of text changed within a line.
See
:h diff
and:h 08.7
for more details on how to diff files.Vim uses a fairly simple algorithm for it's highlighting.
It searches the line for the first changed character, and then the last changed character, and simply highlights all characters between them.
This means you can't have multiple highlights per line - many design decisions in Vim prioritise efficiency.