将常用选项缩写为多个 git 命令
Abbreviate commonly used option to multiple git commands
我有时将 git diff
与 --color-words='\w+|.'
选项一起使用,以内联显示更改,并且仅针对整个单词。
自从我注意到我正在使用它,我为 diff --color-words='\w+|.'
.
创建了一个 git 别名
但现在我注意到,还有许多其他地方我想使用相同的选项,例如 git show
、git stash show -p
、git log -p
等等。所以无法预测我将来可能会在哪里需要该选项。
我试过这个:
$ git config --global alias.words "--color-words='\w+|.'"
$ git diff words
fatal: ambiguous argument 'words': unknown revision or path not in the working tree
Use '--' to separate paths from revisions, like this:
'git <command> [<revision>...] -- [<file>...]'
还有这个:
$ git config --global alias.--words "--color-words='\w+|.'"
error: invalid key: alias.--words
有什么方法可以为一个困难的选项创建一个别名,这样我就可以将它与各种命令一起使用并节省打字和思考的时间吗?
Is there any way to create an alias to an arduous option?
没有别名,没有。
您仍然可以使用别名通过该选项定义您自己的 diff/show/log 命令。 (例如diffw
、showw
、logw
)
或者你可以考虑编写一个 shell 脚本(在 bash 中,甚至在 Windows 中,因为它由 Git for Windows msys2 解释),比如 git-diffw
(没有扩展名,在你的 PATH 中的任何地方设置)
任何名为 git-xxx
的脚本都允许使用 xxx
作为 Git 命令:git xxx
.
这意味着您处于脚本世界中,您可以在其中利用常用选项(例如在文件或环境变量中设置)
可以使用函数:
git() {
ISWORDS=false
# "${@}" is an array of the parameters sent to the method
for i in "${@}"; do
# If the parameter equals "words"
if [[ "$i" = "words" ]]; then
ISWORDS=true
break
fi
done
# If "words" was a parameter (see above)
if [[ "$ISWORDS" ]]; then
# "${@}" is an array of the parameters sent to the method
for i in "${@}"; do
declare -a OTHERPARAMS=()
# Add every parameter to OTHERPARAMS, apart from "words"
if [[ ! "$i" = "words" ]]; then
OTHERPARAMS+=("$i")
fi
done
# Call /usr/bin/git with every parameter except "words", and
# add the extra parameter --color-words too
/usr/bin/git "${OTHERPARAMS[@]}" --color-words='\w+|.'
else
# Else, just call /usr/bin/git with all parameters normally
/usr/bin/git "${@}"
done
}
这会覆盖 git 命令,如果未找到参数 "words",则使用路径中的函数定期调用它。如果是,它会从参数中删除它,使用其他参数和 --colour-words 的额外参数调用命令,代替 "words"
受 Nick Bull 回答的启发,我已将此功能添加到我的 .zshrc
:
GIT=`which git`
git() {
$GIT ${@/--words/'--color-words=\w+|.'}
}
它执行任何 git
命令,其中 --words
选项替换为 '--color-words=\w+|.'
。
有关详细信息,请参阅 Z-Shell 用户指南的 Pattern replacement section。
请注意,在 bash
中您需要额外的引号:"${@/--words/'--color-words=\w+|.'}"
.
现在唯一缺少的是 --words
是 --wor<TAB>
的一个可能的制表符完成,这样它就不会被 --word-diff
代替(导致需要退格并重写)。
为了完整起见,我最初将 Nick Bull 的回答重写为:
git() {
arguments=()
for i in "$@"; do
if [[ $i == --words ]]; then
arguments+=('--color-words=\w+|.')
else
arguments+=($i)
fi
done
/usr/bin/git $arguments
}
但是模式替换可以用更少的代码实现同样的效果。
我有时将 git diff
与 --color-words='\w+|.'
选项一起使用,以内联显示更改,并且仅针对整个单词。
自从我注意到我正在使用它,我为 diff --color-words='\w+|.'
.
但现在我注意到,还有许多其他地方我想使用相同的选项,例如 git show
、git stash show -p
、git log -p
等等。所以无法预测我将来可能会在哪里需要该选项。
我试过这个:
$ git config --global alias.words "--color-words='\w+|.'"
$ git diff words
fatal: ambiguous argument 'words': unknown revision or path not in the working tree
Use '--' to separate paths from revisions, like this:
'git <command> [<revision>...] -- [<file>...]'
还有这个:
$ git config --global alias.--words "--color-words='\w+|.'"
error: invalid key: alias.--words
有什么方法可以为一个困难的选项创建一个别名,这样我就可以将它与各种命令一起使用并节省打字和思考的时间吗?
Is there any way to create an alias to an arduous option?
没有别名,没有。
您仍然可以使用别名通过该选项定义您自己的 diff/show/log 命令。 (例如diffw
、showw
、logw
)
或者你可以考虑编写一个 shell 脚本(在 bash 中,甚至在 Windows 中,因为它由 Git for Windows msys2 解释),比如 git-diffw
(没有扩展名,在你的 PATH 中的任何地方设置)
任何名为 git-xxx
的脚本都允许使用 xxx
作为 Git 命令:git xxx
.
这意味着您处于脚本世界中,您可以在其中利用常用选项(例如在文件或环境变量中设置)
可以使用函数:
git() {
ISWORDS=false
# "${@}" is an array of the parameters sent to the method
for i in "${@}"; do
# If the parameter equals "words"
if [[ "$i" = "words" ]]; then
ISWORDS=true
break
fi
done
# If "words" was a parameter (see above)
if [[ "$ISWORDS" ]]; then
# "${@}" is an array of the parameters sent to the method
for i in "${@}"; do
declare -a OTHERPARAMS=()
# Add every parameter to OTHERPARAMS, apart from "words"
if [[ ! "$i" = "words" ]]; then
OTHERPARAMS+=("$i")
fi
done
# Call /usr/bin/git with every parameter except "words", and
# add the extra parameter --color-words too
/usr/bin/git "${OTHERPARAMS[@]}" --color-words='\w+|.'
else
# Else, just call /usr/bin/git with all parameters normally
/usr/bin/git "${@}"
done
}
这会覆盖 git 命令,如果未找到参数 "words",则使用路径中的函数定期调用它。如果是,它会从参数中删除它,使用其他参数和 --colour-words 的额外参数调用命令,代替 "words"
受 Nick Bull 回答的启发,我已将此功能添加到我的 .zshrc
:
GIT=`which git`
git() {
$GIT ${@/--words/'--color-words=\w+|.'}
}
它执行任何 git
命令,其中 --words
选项替换为 '--color-words=\w+|.'
。
有关详细信息,请参阅 Z-Shell 用户指南的 Pattern replacement section。
请注意,在 bash
中您需要额外的引号:"${@/--words/'--color-words=\w+|.'}"
.
现在唯一缺少的是 --words
是 --wor<TAB>
的一个可能的制表符完成,这样它就不会被 --word-diff
代替(导致需要退格并重写)。
为了完整起见,我最初将 Nick Bull 的回答重写为:
git() {
arguments=()
for i in "$@"; do
if [[ $i == --words ]]; then
arguments+=('--color-words=\w+|.')
else
arguments+=($i)
fi
done
/usr/bin/git $arguments
}
但是模式替换可以用更少的代码实现同样的效果。