将正则表达式 git diff split words at punctuation into .gitconfig file
putting regex to make git diff split words at punctuation into .gitconfig file
当前设置
我的 .gitconfig
当前包含此别名:
[alias]
wdiff = diff --color-words --histogram
让我写 git wdiff
并逐字而不是逐行 diff
输出。我用它来用 LaTeX 写学术散文。
目标
此方法只在白色处分词space。我想在标点符号处进行划分,例如,last word of sentence.
更改为 last word of sentence.\footnote{New footnote.}
会产生 diff
输出,看起来像这样:
last word of sentence.\footnote{New footnote.}
而不是当前输出:
last word of sentence.sentence.\footnote{New footnote.}
(斜体表示删除,粗体表示添加)。
尝试的解决方案
我发现 以一个正则表达式开头,它完全符合我在命令行中的要求,但我还没有想出如何将它放入我的 .gitconfig
文件而不生成错误消息 fatal: bad config line 12 in file /Users/alex/.gitconfig
。这是我放入 .gitconfig
文件的内容:
[alias]
wdiff = diff --color-words='[^][<>()\{},.;:?/|\=+*&^%$#@!~`"'\''[:space:]]+|[][<>(){},.;:?/|\=+*&^%$#@!~`"'\'']' --histogram
好像是分号的问题。
处理 .gitconfig
中类似问题的 different question 建议在整个别名周围加上双引号。但是当我在我的例子中这样做时,我得到了同样的错误信息。我认为这是因为正则表达式还包含双引号。
问题
如何将正则表达式放入我的 .gitconfig
文件中以便正确解析它?
在找到this page of documentation之前,我也很困惑。您感兴趣的部分是:
A line that defines a value can be continued to the next line by ending it with a \
; the backslash and the end-of-line are stripped. Leading whitespaces after name =
, the remainder of the line after the first comment character #
or ;
, and trailing whitespaces of the line are discarded unless they are enclosed in double quotes. Internal whitespaces within the value are retained verbatim.
Inside double quotes, double quote "
and backslash \
characters must be escaped: use \"
for "
and \
for \
.
The following escape sequences (beside \"
and \
) are recognized: \n
for newline character (NL), \t
for horizontal tabulation (HT, TAB) and \b
for backspace (BS). Other char escape sequences (including octal escape sequences) are invalid.
因此,.git/config
中的正确别名是:
wdiff = "diff --color-words='[^][<>()\{},.;:?/|\\=+*&^%$#@!~`\"'\''[:space:]]+|[][<>(){},.;:?/|\\=+*&^%$#@!~`\"'\'']' --histogram"
在这种情况下,您只需将所有内容括在双引号中并转义 "
和反斜杠。
当前设置
我的 .gitconfig
当前包含此别名:
[alias]
wdiff = diff --color-words --histogram
让我写 git wdiff
并逐字而不是逐行 diff
输出。我用它来用 LaTeX 写学术散文。
目标
此方法只在白色处分词space。我想在标点符号处进行划分,例如,last word of sentence.
更改为 last word of sentence.\footnote{New footnote.}
会产生 diff
输出,看起来像这样:
last word of sentence.\footnote{New footnote.}
而不是当前输出:
last word of sentence.sentence.\footnote{New footnote.}
(斜体表示删除,粗体表示添加)。
尝试的解决方案
我发现 .gitconfig
文件而不生成错误消息 fatal: bad config line 12 in file /Users/alex/.gitconfig
。这是我放入 .gitconfig
文件的内容:
[alias]
wdiff = diff --color-words='[^][<>()\{},.;:?/|\=+*&^%$#@!~`"'\''[:space:]]+|[][<>(){},.;:?/|\=+*&^%$#@!~`"'\'']' --histogram
好像是分号的问题。
处理 .gitconfig
中类似问题的 different question 建议在整个别名周围加上双引号。但是当我在我的例子中这样做时,我得到了同样的错误信息。我认为这是因为正则表达式还包含双引号。
问题
如何将正则表达式放入我的 .gitconfig
文件中以便正确解析它?
在找到this page of documentation之前,我也很困惑。您感兴趣的部分是:
A line that defines a value can be continued to the next line by ending it with a
\
; the backslash and the end-of-line are stripped. Leading whitespaces aftername =
, the remainder of the line after the first comment character#
or;
, and trailing whitespaces of the line are discarded unless they are enclosed in double quotes. Internal whitespaces within the value are retained verbatim.Inside double quotes, double quote
"
and backslash\
characters must be escaped: use\"
for"
and\
for\
.The following escape sequences (beside
\"
and\
) are recognized:\n
for newline character (NL),\t
for horizontal tabulation (HT, TAB) and\b
for backspace (BS). Other char escape sequences (including octal escape sequences) are invalid.
因此,.git/config
中的正确别名是:
wdiff = "diff --color-words='[^][<>()\{},.;:?/|\\=+*&^%$#@!~`\"'\''[:space:]]+|[][<>(){},.;:?/|\\=+*&^%$#@!~`\"'\'']' --histogram"
在这种情况下,您只需将所有内容括在双引号中并转义 "
和反斜杠。