bash zsh 更改提示输入颜色

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9268836/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 01:33:28  来源:igfitidea点击:

zsh change prompt input color

bashterminalzshzshrc

提问by Sherwin Yu

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.

我想更改 zsh 中输入文本的颜色(我为每个命令键入的文本)。示例:在user@host> ls ~/我想要ls ~/黄色以从标准输出中脱颖而出。

I know I can accomplish this in bashusing

我知道我可以在bash 中使用

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

我怎样才能在zsh 中做到这一点?目前,我有

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

在 .zshrc 中(最后将颜色设置为黄色)但它不起作用。(我也不知道如何在命令后将颜色设置回白色)。

采纳答案by Jim Stewart

Try this:

尝试这个:

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

I tried using trap, but it looks like DEBUGdoesn'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.

我尝试使用trap,但看起来DEBUG直到命令运行之后/下一个提示之前才会发生,因此该命令最终以黄色执行。该preexec()函数在命令执行之前被调用,因此您可以在那里恢复默认颜色。