如何改变zsh中提示符的颜色(与普通文本不同)?

时间:2021-10-12 05:37:57

To recognize better the start and the end of output on a commandline, I want to change the color of my prompt, so that it is visibly different from the programs output. As I use zsh, can anyone give me a hint?

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

6 个解决方案

#1


39  

Here's an example of how to set a red prompt:

下面是一个如何设置红色提示的示例:

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

The magic is the \e[0;31m (turn on red foreground) and \e[0m (turn off character attributes). These are called escape sequences. Different escape sequences give you different results, from absolute cursor positioning, to color, to being able to change the title bar of your window, and so on.

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

For more on escape sequences, see the wikipedia entry on ANSI escape codes

有关转义序列的更多信息,请参见*上关于ANSI转义代码的条目。

#2


194  

Put this in ~/.zshrc:

把这个放在~ / . zshrc:

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

Supported Colors:
Red, Blue, Green, Cyan, Yellow, Magenta, Black & White (from this answer) although different computers may have different valid options.

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

Surround color codes (and any other non-printable chars) with %{....%}. This is for the text wrapping to work correctly.

包围颜色代码(和任何其他不可打印的字符)%{%}。这是用于文本包装的正确工作。

#3


43  

Zsh comes with colored prompts builtin. Try

Zsh自带彩色提示框。试一试

autoload -U promptinit && promptinit

and then prompt -l lists available prompts, -p fire previews the "fire" prompt, -s fire sets it.

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

When you are ready to add a prompt add something like this below the autoload line above:

当您准备添加一个提示符时,在上面的autoload行下面添加如下内容:

prompt fade red

#4


14  

I don't think the autoload -U colors && colors is needed anymore and one can simply do:

我不认为现在需要的是autoload -U的颜色和颜色,我们可以简单的做:

PS1="%{%F{red}%}%n%{%f%}@%{%F{blue}%}%m %{%F{yellow}%}%~ %{$%f%}%% "

to achieve the same result as FireDude's answer. See the ZSH documentation for more info.

达到和FireDude的答案一样的结果。有关更多信息,请参见ZSH文档。

#5


4  

I have found that, with zsh5 (the default one on Debian Jessie), all those solutions works:

我发现,在zsh5 (Debian Jessie的默认设置)中,所有这些解决方案都有效:

  • $'\e[00m
  • “\ e[00美元
  • $fg[white]
  • fg美元(白色)
  • $fg{white}
  • 美元fg {白}

Now, they have a problem: they will move the cursor, resulting in ugly decal when tabbing (for auto-completion). The solution is simply to surround the escape sequences with %{FOOBAR%}. Took me a while to figure this. For 2nd and 3rd solutions loading colors module is mandatory. To keep the 1st solution readable, just define variables for the colors you use.

现在,他们有了一个问题:他们会移动光标,导致在tabbing(自动完成)时出现丑陋的贴纸。解决方案是简单地用%{FOOBAR%}包围转义序列。我花了一段时间才明白这一点。第2和第3个解决方案加载颜色模块是强制性的。为了保持第一个解决方案可读,只需为您使用的颜色定义变量。

#6


0  

man zshall and search for PROMPT EXPANSION

人应寻求迅速扩张。

After reading the existing answers here, several of them are conflicting. I've tried the various approaches on systems running zsh 4.2 and 5+ and found that the reason these answers are conflicting is that they do not say which version of ZSH they are targeted at. Different versions use different syntax for this and some of them require various autoloads.

在阅读了现有的答案后,其中的几个是相互矛盾的。我已经试过了运行zsh 4.2和5+的系统的各种方法,并且发现这些答案存在冲突的原因是,他们没有说他们针对的是哪个版本的zsh。不同的版本使用不同的语法,其中一些需要不同的autoloads。

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.

因此,最好的方法可能是对人zshall和搜索迅速扩展,以找出所有关于您的zsh安装的规则。在评论中,像“我使用Ubuntu 11.04或10.4或OSX”这样的东西并不是很有意义,因为不清楚你使用的是哪个版本的ZSH。Ubuntu 11.04并不意味着比ubuntu10.04更新版本的ZSH。旧版本的安装可能有很多原因。对于这个问题,ZSH的新版本并不意味着在不知道ZSH的版本的情况下使用哪种语法。

#1


39  

Here's an example of how to set a red prompt:

下面是一个如何设置红色提示的示例:

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

The magic is the \e[0;31m (turn on red foreground) and \e[0m (turn off character attributes). These are called escape sequences. Different escape sequences give you different results, from absolute cursor positioning, to color, to being able to change the title bar of your window, and so on.

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

For more on escape sequences, see the wikipedia entry on ANSI escape codes

有关转义序列的更多信息,请参见*上关于ANSI转义代码的条目。

#2


194  

Put this in ~/.zshrc:

把这个放在~ / . zshrc:

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

Supported Colors:
Red, Blue, Green, Cyan, Yellow, Magenta, Black & White (from this answer) although different computers may have different valid options.

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

Surround color codes (and any other non-printable chars) with %{....%}. This is for the text wrapping to work correctly.

包围颜色代码(和任何其他不可打印的字符)%{%}。这是用于文本包装的正确工作。

#3


43  

Zsh comes with colored prompts builtin. Try

Zsh自带彩色提示框。试一试

autoload -U promptinit && promptinit

and then prompt -l lists available prompts, -p fire previews the "fire" prompt, -s fire sets it.

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

When you are ready to add a prompt add something like this below the autoload line above:

当您准备添加一个提示符时,在上面的autoload行下面添加如下内容:

prompt fade red

#4


14  

I don't think the autoload -U colors && colors is needed anymore and one can simply do:

我不认为现在需要的是autoload -U的颜色和颜色,我们可以简单的做:

PS1="%{%F{red}%}%n%{%f%}@%{%F{blue}%}%m %{%F{yellow}%}%~ %{$%f%}%% "

to achieve the same result as FireDude's answer. See the ZSH documentation for more info.

达到和FireDude的答案一样的结果。有关更多信息,请参见ZSH文档。

#5


4  

I have found that, with zsh5 (the default one on Debian Jessie), all those solutions works:

我发现,在zsh5 (Debian Jessie的默认设置)中,所有这些解决方案都有效:

  • $'\e[00m
  • “\ e[00美元
  • $fg[white]
  • fg美元(白色)
  • $fg{white}
  • 美元fg {白}

Now, they have a problem: they will move the cursor, resulting in ugly decal when tabbing (for auto-completion). The solution is simply to surround the escape sequences with %{FOOBAR%}. Took me a while to figure this. For 2nd and 3rd solutions loading colors module is mandatory. To keep the 1st solution readable, just define variables for the colors you use.

现在,他们有了一个问题:他们会移动光标,导致在tabbing(自动完成)时出现丑陋的贴纸。解决方案是简单地用%{FOOBAR%}包围转义序列。我花了一段时间才明白这一点。第2和第3个解决方案加载颜色模块是强制性的。为了保持第一个解决方案可读,只需为您使用的颜色定义变量。

#6


0  

man zshall and search for PROMPT EXPANSION

人应寻求迅速扩张。

After reading the existing answers here, several of them are conflicting. I've tried the various approaches on systems running zsh 4.2 and 5+ and found that the reason these answers are conflicting is that they do not say which version of ZSH they are targeted at. Different versions use different syntax for this and some of them require various autoloads.

在阅读了现有的答案后,其中的几个是相互矛盾的。我已经试过了运行zsh 4.2和5+的系统的各种方法,并且发现这些答案存在冲突的原因是,他们没有说他们针对的是哪个版本的zsh。不同的版本使用不同的语法,其中一些需要不同的autoloads。

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.

因此,最好的方法可能是对人zshall和搜索迅速扩展,以找出所有关于您的zsh安装的规则。在评论中,像“我使用Ubuntu 11.04或10.4或OSX”这样的东西并不是很有意义,因为不清楚你使用的是哪个版本的ZSH。Ubuntu 11.04并不意味着比ubuntu10.04更新版本的ZSH。旧版本的安装可能有很多原因。对于这个问题,ZSH的新版本并不意味着在不知道ZSH的版本的情况下使用哪种语法。