函数中的 Git 别名输出着色

标签 git bash git-alias

我想添加一个复杂的 git 别名,它会在执行命令时回显消息。我想给一些消息着色(红色表示错误等)。

[alias]
    test = !"f() { echo "\033[31mHello\033[0m World"; }; f"

但是,当我执行别名时出现错误:

bad config line X in file .gitconfig`

在终端中运行相同的命令 echo "\033[31mHello\033[0m World" 着色就好了。

最佳答案

反斜杠必须转义。来自 git-config 文档...

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.

这会起作用。

test = !"f() { echo \"\\033[31mHello\\033[0m World\"; }; f"

但是如果您的别名太复杂以至于您需要定义可能会变成一团糟的函数。我建议将这些函数放在它们自己的文件中并获取它。

test = !"source ~/.gitfuncs; f"

$ cat ~/.gitfuncs
f() { echo "\033[31mHello\033[0m World"; };

关于函数中的 Git 别名输出着色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42403640/

相关文章:

git stash 错误 : git stash pop and ended up with merge conflicts

git - 如何删除 Bitbucket 中的所有问题?

bash - 向所有进程发送 SIGTERM

git - 将参数传递给 Git 别名命令

git - 在 git-alias 中引用自己的用户名

git - Visual Studio 2015 git不显示所有历史记录,仅显示第一次提交

linux - yum install 无法在没有 root 权限的集群上运行

linux - 在 bash 创建的 tar 中排序文件

java - Shell 命令从 java 失败,但在手动运行时有效

git - 自定义 git "bang"别名的 Zsh 完成 - Git 分支名称