git目录之外的其他zsh终端提示
问题描述:
有人知道该怎么做吗?当我不在git目录中时,我需要/想要我的正确提示来显示时间,而当我在git目录中时,我希望它显示分支.
Does anyone know how to do this? I need/want my right prompt to display time when I'm outside a git directory, and I want it to show the branch when I'm inside a git dir.
这是我使用的配置,但是要在git dir之外使用 RPROMPT =%*"
.
But want to use RPROMPT="%*"
when outside git dir.
答
一种方法是将所有内容放入您自己的precmd函数中.这是一个示例(将其添加到〜/.zshrc
):
One way is to put everything into your own precmd function. Here's an example (add this to ~/.zshrc
):
my_precmd() {
vcs_info
psvar[1]=$vcs_info_msg_0_
if [[ -z ${psvar[1]} ]]; then
psvar[1]=${(%):-%*}
fi
}
autoload -Uz vcs_info
zstyle ':vcs_info:git:*' formats '%b'
autoload -Uz add-zsh-hook
add-zsh-hook precmd my_precmd
RPROMPT=%1v
一些注意事项:
-
vcs_info
调用内置的zsh来设置$ vcs_info_msg_0 _
. -
zstyle ...
从vcs_info
配置结果. -
psvar [1]
psvar
数组是一组特殊变量,可以在提示符(%1v
)中轻松引用. -
if [[--z $ {psvar [1]}]]
测试是否由vcs_info
设置了任何内容;如果结果为零长度,请改用时间. -
$ {(%):-%*}
获取时间.有几段内容:(%)
说要像提示中那样替换百分比转义符,:-
用于创建替换的匿名变量,而%*
是设置时间的提示转义. -
add-zsh-hook ...
将my_precmd
添加到precmd挂钩中,因此它将在每个提示之前被调用.比直接设置precmd_functions
更好. -
RPROMPT =%1v
设置右侧提示以始终显示psvar [1]
的值.
-
vcs_info
Calls the zsh builtin to set$vcs_info_msg_0_
. -
zstyle ...
Configures the result fromvcs_info
. -
psvar[1]
Thepsvar
array is a set of special variables that can be referenced easily in a prompt (%1v
). -
if [[ -z ${psvar[1]} ]]
Tests to see if anything was set byvcs_info
; if the result is zero-length, use the time instead. -
${(%):-%*}
Gets the time. There are a few pieces:(%)
says to substitute percent escapes like in a prompt,:-
is used to create an anonymous variable for the substitution, and%*
is the prompt escape that sets the time. -
add-zsh-hook ...
Addsmy_precmd
to the precmd hook, so it'll be called prior to each prompt. This is preferable to settingprecmd_functions
directly. -
RPROMPT=%1v
Sets the right-side prompt to always display the value ofpsvar[1]
.