问题
Here is my ZSH prompt theme
function git_prompt_info() {
ref=$(git symbolic-ref HEAD 2> /dev/null) || return
echo "$(parse_git_dirty)$ZSH_THEME_GIT_PROMPT_PREFIX$(current_branch)$ZSH_THEME_GIT_PROMPT_SUFFIX"
}
PROMPT='$fg[yellow]%}⚡︎ $fg[cyan]%~ $(git_prompt_info)
%{$reset_color%}→ '
ZSH_THEME_GIT_PROMPT_PREFIX="[git:"
ZSH_THEME_GIT_PROMPT_SUFFIX="]$reset_color"
ZSH_THEME_GIT_PROMPT_DIRTY="$fg[red]+"
ZSH_THEME_GIT_PROMPT_CLEAN="$fg[green]"
RPROMPT='%T'
Which looks like
When I move the $(git_prompt_info)
to RPROMPT
function git_prompt_info() {
ref=$(git symbolic-ref HEAD 2> /dev/null) || return
echo "$(parse_git_dirty)$ZSH_THEME_GIT_PROMPT_PREFIX$(current_branch)$ZSH_THEME_GIT_PROMPT_SUFFIX"
}
PROMPT='%T $fg[yellow]%}⚡︎ $fg[cyan]%~
%{$reset_color%}→ '
ZSH_THEME_GIT_PROMPT_PREFIX="[git:"
ZSH_THEME_GIT_PROMPT_SUFFIX="]$reset_color"
ZSH_THEME_GIT_PROMPT_DIRTY="$fg[red]+"
ZSH_THEME_GIT_PROMPT_CLEAN="$fg[green]"
RPROMPT='$(git_prompt_info)'
it looks like
See the spacing on the right? Also the arrow starts in the wrong place?
How can I fix this?
回答1:
I believe $fg[color]
contains something like \e[32m
? If so, it must be enclosed in %{…%}
to indicate that this sequence has no width. But much better if you forget about the whole thing and use %F{color}
for foreground, %K{color}
for background and %f
/%k
to cancel them in place of $reset_color
. You must do
setopt promptsubst
setopt promptpercent
in order for this to work (you likely already do have this).
That gap is the width of colors, and they are the reason why you have wrong cursor position. Problem here is that zsh can’t query terminal with the question “Hey, I outputted some text, what is its width?” instead having to calculate width on its own.
来源:https://stackoverflow.com/questions/14049870/zsh-rprompt-weird-spacing