为了更好地识别命令行输出的开始和结束,我想改变提示符的颜色,使其与程序输出明显不同。当我使用zsh时,有人能给我一个提示吗?


当前回答

试试我最喜欢的: 放在

~/.zshrc

这条线:

PROMPT='%F{240}%n%F{red}@%F{green}%m:%F{141}%d$ %F{reset}'

不要忘记

source ~/.zshrc

为了测试更改

当然,你可以改变颜色/颜色代码:-)

其他回答

下面是一个设置红色提示符的例子:

PS1=$'\e[0;31m$ \e[0m'

神奇的是\e[0;31m(打开红色前景)和\e[0m(关闭字符属性)。这些被称为转义序列。不同的转义序列会给出不同的结果,从绝对光标定位到颜色,再到能够更改窗口的标题栏,等等。

有关转义序列的更多信息,请参阅维基百科关于ANSI转义码的条目

把这个放到~/.zshrc:

autoload -U colors && colors
PS1="%{$fg[red]%}%n%{$reset_color%}@%{$fg[blue]%}%m %{$fg[yellow]%}%~ %{$reset_color%}%% "

支持颜色: 红色,蓝色,绿色,青色,黄色,品红,黑色和白色(从这个答案),尽管不同的计算机可能有不同的有效选项。

用%{....%}包围颜色代码(以及任何其他不可打印的字符)。这是为了让文本换行正确工作。

此外,下面是如何让它与这里的目录修剪一起工作。

PS1="%{$fg[red]%}%n%{$reset_color%}@%{$fg[blue]%}%m %{$fg[yellow]%}%(5~|%-1~/.../%3~|%4~) %{$reset_color%}%% "

为了补充上述所有答案,另一个方便的技巧是将彩色提示设置放入zsh函数中。在那里你可以定义局部变量来别名较长的命令,例如rc=$reset_color或定义你自己的颜色变量。不要忘记把它放在你的.zshrc文件中,并调用你已经定义的函数:

# Coloured prompt
autoload -U colors && colors
function myprompt {
  local rc=$reset_color
  export PS1="%F{cyan}%n%{$rc%}@%F{green}%m%{$rc%}:%F{magenta}%~%{$rc%}%# "
}
myprompt

Zsh自带彩色提示。试一试

autoload -U promptinit && promptinit

然后prompt -l列出可用的提示,-p fire预览“fire”提示,-s fire设置它。

当你准备添加一个提示符时,在上面的自动加载行下面添加如下内容:

prompt fade red

man zshall和搜索PROMPT EXPANSION

在阅读了这里现有的答案后,其中一些答案是相互矛盾的。我在运行zsh 4.2和5+的系统上尝试了各种方法,发现这些答案相互冲突的原因是它们没有说明它们针对的是哪个zsh版本。不同的版本使用不同的语法,其中一些需要各种自动加载。

So, the best bet is probably to man zshall and search for PROMPT EXPANSION to find out all the rules for your particular installation of zsh. Note in the comments, things like "I use Ubuntu 11.04 or 10.4 or OSX" Are not very meaningful as it's unclear which version of ZSH you are using. Ubuntu 11.04 does not imply a newer version of ZSH than ubuntu 10.04. There may be any number of reasons that an older version was installed. For that matter a newer version of ZSH does not imply which syntax to use without knowing which version of ZSH it is.