zsh prompt and hostname

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-09 16:01:16

问题


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 ~ %

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!


回答1:


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-}

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+=" %# "



回答2:


If you're using OhMyZsh, this is one line to add at the bottom of you .zshrc if you want the user and hostname in your PS1/PROMPT :

export PROMPT='%(!.%{%F{yellow}%}.)$USER@%{$fg[white]%}%M ${ret_status} %{$fg[cyan]%}%c%{$reset_color%} $(git_prompt_info)'

Enjoy ;)



来源:https://stackoverflow.com/questions/30199068/zsh-prompt-and-hostname

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!