为了更好地识别命令行输出的开始和结束,我想改变提示符的颜色,使其与程序输出明显不同。当我使用zsh时,有人能给我一个提示吗?
当前回答
为了补充上述所有答案,另一个方便的技巧是将彩色提示设置放入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
其他回答
要根据最后一个命令的退出状态获得颜色提示符,您可以使用以下命令:
PS1='%(?.%F{green}.%F{red})%n@%m:%~%# %f'
只需将这一行添加到~/.zshrc。
文档列出了可能的占位符。
Debian/Ubuntu/树莓派操作系统风格bash提示符:
autoload -U colors && colors
PS1="%B%F{034}%n@%m%f%b:%B%F{019}%~ %#%f%b "
不完全确定自动加载-U颜色和&颜色是必要的,但我确实需要它当我使用$fg[…]]而不是%F{…}。 在我的例子中;
%F(% F)开始(停止)前景色-我已经使用了256COLOR值,但你也可以使用十六进制,例如,我已经使用了正常的绿色%F{034},但%F{#00aa00}也适用 %B(% B)开始(停止)加粗 % n $用户名 %m短的机器名 %~登录/工作目录,如果~在主目录 如果是正常用户,则显示%#
编辑:在macOS大苏尔,蒙特雷和文图拉测试。不确定早期的zsh版本,但Ventura 13.0是zsh 5.8.1 (x86_64-apple-darwin22.0)
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.
试试我最喜欢的: 放在
~/.zshrc
这条线:
PROMPT='%F{240}%n%F{red}@%F{green}%m:%F{141}%d$ %F{reset}'
不要忘记
source ~/.zshrc
为了测试更改
当然,你可以改变颜色/颜色代码:-)
Zsh自带彩色提示。试一试
autoload -U promptinit && promptinit
然后prompt -l列出可用的提示,-p fire预览“fire”提示,-s fire设置它。
当你准备添加一个提示符时,在上面的自动加载行下面添加如下内容:
prompt fade red