ZSH PS1 adding spacing when tabbing

autocompletecommand lineoh-my-zshshellzsh

I'm having an issue with zsh/oh-my-zsh with my custom PS1 prompt (that I brought across from bash).

When I use the built-in zsh/oh-my-zsh prompt and tab for auto-completion, this works as expected.

When I use my own PS1 prompt, the whole input area moves across about 8 spaces and stays there until I send a break/new line.

Examples:

I typed in vim a and tabbed

Built in theme, robbyrussell with default prompt:

Built in theme, *robbyrussell* with default prompt:

Same theme, custom PS1 prompt:

Same theme, custom PS1 prompt:

PS1 string and variable definitions in .zshrc:

# Monokai colours
D=$'\e[37;40m'
PINK=$'\e[35;40m'
GREEN=$'\e[32;40m'
ORANGE=$'\e[33;40m'    

export PS1='${PINK}andrew${D}@${ORANGE}macbook${D}:%~$ '

As a test, I tried my prompt without the colour variables and it does work

Best Answer

It seems that the ANSI codes are messing with your terminal, possibly due to some interactions with the other contents of PS1 or because you do not reset to default.

Luckily, in zsh there is no need to use ANSI escape codes. You can use %F{color} and %K{color} to set foreground and background colors respectively and %f and %k to reset default values (See the Zsh Manual for more information)

Either of these should do the trick:

PS1='%5Fandrew%f@%3Fmacbook%f:%~$ '
PS1='%F{5}andrew%f@%F{3}macbook%f:%~$ '
PS1='%F{magenta}andrew%f@%F{yellow}macbook%f:%~$ '

I used %f instead of %7F or %F{white}, assuming you really wanted to just disable coloring in that place (hence naming the variable D instead of WHITE). If you want to use %F{white} instead, you should put a %f at the end of PS1.

If you use a 88-color or 256-color terminal, you can even use these the indices of these colors. For example %F{221} would get you a dark yellow.


Also note, that it is usually unnecessary to export PS1 in zsh - or bash for that matter.

Related Question