为了更好地识别命令行输出的开始和结束,我想改变提示符的颜色,使其与程序输出明显不同。当我使用zsh时,有人能给我一个提示吗?
当前回答
把这个放到~/.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%}%% "
其他回答
我不认为自动加载-U颜色和&颜色是需要的了,你可以简单地做:
PS1="%{%F{red}%}%n%{%f%}@%{%F{blue}%}%m %{%F{yellow}%}%~ %{$%f%}%% "
以达到与FireDude的答案相同的结果。有关更多信息,请参阅ZSH文档。
Zsh自带彩色提示。试一试
autoload -U promptinit && promptinit
然后prompt -l列出可用的提示,-p fire预览“fire”提示,-s fire设置它。
当你准备添加一个提示符时,在上面的自动加载行下面添加如下内容:
prompt fade red
把这个放到~/.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%}%% "
要根据最后一个命令的退出状态获得颜色提示符,您可以使用以下命令:
PS1='%(?.%F{green}.%F{red})%n@%m:%~%# %f'
只需将这一行添加到~/.zshrc。
文档列出了可能的占位符。
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.