zsh提示和主机名

编程入门 行业动态 更新时间:2024-10-28 14:26:16
本文介绍了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提示和主机名

本文发布于:2023-10-17 03:07:18,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1499597.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:主机名   提示   zsh

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!