zsh change prompt input color

Sherwin Yu picture Sherwin Yu · Feb 13, 2012 · Viewed 7.7k times · Source

I want to change the color of the input text in zsh (the text that I type for each command). Example: in user@host> ls ~/ I would want ls ~/ to be yellow to stand out from standard output.

I know I can accomplish this in bash using

export PS1=" $BIGreen \u@\h \w \$ $IYellow" 

At the end of the prompt, the color is set to Yellow, input text I type is yellow (with the appropriate color variables defined). And then

trap 'echo -ne "\e[0m"' DEBUG

Which resets the color to normal when the outputs of my command are displayed.

How can I accomplish this in zsh? Currently, I have

PROMPT=$'{$fg[green]%}%n@%{$fg[green]%}%m %# %{$fg[yellow]%}'

in .zshrc (setting color to yellow at the end) but it does not work. (I also wouldn't know how to set the color back to white after the command).

Answer

Jim Stewart picture Jim Stewart · Nov 29, 2012

Try this:

PROMPT="%F{green}%n@%m %# %F{yellow}"
preexec () { echo -ne "\e[0m" }

I tried using trap, but it looks like DEBUG doesn't happen until after the command runs/before the next prompt, so the command ends up executing in yellow. The preexec() function gets called before the command executes, so you can restore the default color there.