Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

zsh prompt theme causing cursor to shift right upon completion request

I'm trying to create a simple prompt using escape characters to define the colors of the prompt components:

PROMPT=$'\e[0;35m%n\e[0m \e[0;92m%~\e[0m \e[0;97m$ \e[0m'

This works fine except for one thing - when I go to tab-complete my cursor (and the associated command to be completed) gets shifted to the right.

A little googling led me here: https://github.com/robbyrussell/oh-my-zsh/issues/23

Changing the prompt formatting to something like:

PROMPT='%{$fg[green]%}%n%'

fixes the problem. However, the green, magenta, blue, etc. color options are more limiting in terms of the colors that you can choose than using the escape characters along with the color codes shown above.

I cannot figure out how to format the first prompt shown above so that the tab completion issue goes away. I've a tried a few different permutation with regards to where to put the %{ and they all just end up breaking prompt string.

What is the correct way to format this? OR, is there a way to access more colors using the $fg[COLOR] scheme than red, blue, magenta, etc.?

like image 896
dan_g Avatar asked Feb 13 '16 00:02

dan_g


1 Answers

You could put the %{%} around the escape characters.

%{...%}

Include a string as a literal escape sequence. The string within the braces should not change the cursor position.

-- zshmisc(1) SIMPLE PROMPT ESCAPES, Visual effects

So, the below simplified erroneuos case,

PROMPT=$'\e[0;35m%n\e[0m '

could be

PROMPT=$'%{\e[0;35m%}%n%{\e[0m%} '

And why PROMPT='%{$fg[green]%}%n%' works because the escape sequences which $fg[green] produces are correctly wrapped within %{...%}.

like image 178
hchbaw Avatar answered Dec 08 '22 18:12

hchbaw