18

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!

Zoltan King
  • 1,964
  • 4
  • 18
  • 38
  • Perhaps change the hostname of your system? `%m` is "The hostname up to the first '.'" An alternative might be to define another variable with the value you want, and reference that instead of `%m`... – twalberg May 12 '15 at 20:30

4 Answers4

18

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+=" %# "
chepner
  • 497,756
  • 71
  • 530
  • 681
  • Thanks for colors syntaxis, I finished one line prompt: (no oh-my-zsh): ```export PROMPT="%F{magenta}%n%f@%F{blue}${${(%):-%m}#zoltan-}%f %F{yellow}%1~ %F{white}"``` – Andrew Oct 20 '19 at 14:03
  • I took your answer and ran with it. https://stackoverflow.com/a/66462684/117471 I included many links to documentation. Thanks for the inspiration! ☮️❤️ – Bruno Bronosky Mar 03 '21 at 18:19
13

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 ;)

vincedgy
  • 395
  • 4
  • 6
3

I stumbled upon this question and the wonderfully informative answer from @chepner inspired me to remove the .local from the %M (fully qualified machine hostname) in my own universal prompt.

In their case, they did:

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

In my case, I did:

${${(%):-%M}%.local}

NOTE:

In shell substring removal

  • # means "trim from the left"
  • % means "trim from the right"

You can remember which is which because # and % are on the left and right of $ which is the char you use for ${parameter_substitution).

Bruno Bronosky
  • 66,273
  • 12
  • 162
  • 149
3

In my case, I had PS1 set in .zshrc. Look for '%m' - this corresponds to the hostname that you see in the prompt. Replace it what you want. Let me replace '%m' with CUSTOM.


OLD

enter image description here

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

NEW

enter image description here

autoload -U colors && colors
PS1="%{$fg[green]%}%n%{$reset_color%}@%{$fg[cyan]%}CUSTOM %{$fg[yellow]%}%~ %{$reset_color%}%% "
dpaks
  • 375
  • 1
  • 13