zsh提示和主机名 [英] zsh prompt and hostname

查看:243
本文介绍了zsh提示和主机名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在.zshrc中使用以下提示:

I use the following prompt in .zshrc:

PROMPT="%{$fg[magenta]%}%n%{$reset_color%}@%{$fg[blue]%}%m %{$fg[yellow]%}%1~ %{$reset_color%}%# "

打开终端时,我会看到以下提示:

When I open terminal I see this prompt:

zoltan@zoltan-Macbook-Pro ~ %

是否可以在主机名中添加文本"zoltan"?我想使它看起来像这样:

Is it possible to drop the text "zoltan" in the hostname? I would like to make it look like this:

zoltan@Macbook-Pro ~ %

任何建议将不胜感激.谢谢!

Any suggestion would be greatly appreciated. Thank you!

推荐答案

有点麻烦,但是您可以假装%m是参数,并使用参数扩展从主机名中剥离zoltan:

It's a bit of a mess, but you can pretend the %m is a parameter and use parameter expansion to strip the zoltan from the host name:

PROMPT="...${${(%):-%m}#1} ..."

一些解释.首先,创建一个实际上没有参数名称的参数"扩展.它只是将您提供的文本用作值":

A little explanation. First, you create a "parameter" expansion that doesn't actually have a parameter name; it just uses the text you provide as the "value":

${:-%m}

下一步,添加%扩展标志,以便处理该值中找到的所有提示转义符.

Next, add the % expansion flag so that any prompt escapes found in the value are processed.

${(%):-%m}

最后,接下来是使用#运算符从字符串中删除前缀的最终扩展:

Finally, next it in a final expansion that uses the # operator to remove a prefix from the string:

${${(%):-%m}#zoltan-}

您可以通过逐个构建一点来驯服您的提示(并使用zsh的提示转义符来处理颜色变化,而不是显式地嵌入终端控制序列).

You can tame your prompt a bit by building up piece by piece (and use zsh's prompt escapes to handle the color changes, rather than embedding terminal control sequences explicitly).

PROMPT="%F{magenta}%n%f"  # Magenta user name
PROMPT+="@"
PROMPT+="%F{blue}${${(%):-%m}#zoltan-}%f" # Blue host name, minus zoltan
PROMPT+=" "
PROMPT+="%F{yellow}%1~ %f" # Yellow working directory
PROMPT+=" %# "

这篇关于zsh提示和主机名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆