11

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

this

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

enter image description here

See the spacing on the right? Also the arrow starts in the wrong place?

How can I fix this?

hjpotter92
  • 78,589
  • 36
  • 144
  • 183
ahmedelgabri
  • 1,228
  • 3
  • 17
  • 30
  • 2
    Hey, could you please share that color scheme? I know this thread is old, but maybe you still got it =/ – roboslone Jul 10 '17 at 20:08

1 Answers1

13

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.

ZyX
  • 52,536
  • 7
  • 114
  • 135
  • Now my file looks like this http://pastebin.com/icU1e62D it looks like this http://f.cl.ly/items/3i3l2V3k3d1d1I321I1s/Screen%20Shot%202012-12-28%20at%201.26.36%20AM.png I tried adding setopt promptsubst setopt promptpercent but it didn't work – ahmedelgabri Dec 27 '12 at 21:31
  • @Gabri It works or you won’t be seeing this (first allows expanding of `$()`, second make `%{` work). I do not see any problems in what you have shown here. – ZyX Dec 27 '12 at 21:54
  • the tab completions is not working properly I have an extra space & if you clicked backspace to delete what you wrote the last letter will still be visibile check this http://cl.ly/3v1U2k1b2f3h – ahmedelgabri Dec 28 '12 at 05:23
  • @Gabri I see now. `%{…%}` as I said means that text has no width. This rule is violated in `*_DIRTY` variable. – ZyX Dec 28 '12 at 16:22
  • Thank you, this solved my problem as well. I had to use ${...$} because I wanted 256 colors, not the 16 colors that have name aliases. – Sridhar Sarnobat Apr 16 '13 at 22:37
  • @Sridhar-Sarnobat You don’t need to use `%{…%}` for 256 colors as well. `%F{color}` accepts also color codes (`0…255`). – ZyX Apr 17 '13 at 04:01
  • @ZyX - awesome. I can now do away with the helper variables for colors and the helper function I had. Thank you very much. For anyone else wondering, here is an example: PROMPT='%F{134}'`uname`' %F{110}%D{%a %d %B %G}>%f ' – Sridhar Sarnobat Apr 18 '13 at 22:39
  • `%F{161}%n%f` doesn't work for me but `%{$FG[161]%}%n%f` does. Anyone know why that is? – mpen Apr 24 '14 at 16:29
  • @Mark Maybe because your zsh is too old. – ZyX May 01 '14 at 15:01
  • @ZyX: Nope, it was a fresh install. I don't think numbers above 8 or so were working for me. Small numbers and named colors do work. – mpen May 01 '14 at 16:21