zsh change prompt input color
Asked Answered
M

1

13

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).

Muntjac answered 13/2, 2012 at 22:12 Comment(0)
L
13

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.

Leiker answered 29/11, 2012 at 22:8 Comment(2)
This is exactly what I wanted! Thanks. Would you mind explaining how this works? Also, one caveat is that it the color reverts during autocompletion (e.g. ls somepath[tab]), at least for my settings (I think zsh is inserting an uncolored slash during autocompletion) but that's a very minor complaint.Muntjac
There was a typo in the original string (#F instead of %F), but that wasn't the problem either. I just noticed that I'm having the same problem as you with the color changing after an autocomplete. prexec() gets called before every command executed from the shell, which is how this works to reset the color. Turns out you're supposed to close out all %F color changes with a %f tag to restore the original color. If I figure out a better solution to this I'll update the answer.Leiker

© 2022 - 2024 — McMap. All rights reserved.