55

I have a development server hosted on Digital Ocean, using Ubuntu 14.04. I switched my shell to ZSH and decided to go with the Agnoster theme. In order to get user@hostname to stop showing, I set the DEFAULT_USER in my .zshrc file.

For some reason on the ubuntu server, that's not working. The hostname still shows, and will not go away. I'm doing the exact same thing on my Mac OSX, and it works fine.

Here are some screenshots:

enter image description here

enter image description here

Anyone know what's going on? I even tried DEFAULT_USER="$USER@$HOST" with no luck.

If I go back to the default, Robby Russell theme, it works just fine.

enter image description here

12 Answers 12

99

It is the feature according to this; when we are sshing, the hostname will be shown.

Overriding the function prompt_context or build_prompt on Agnoster theme will rescue. Putting below snippets at the very end of the ~/.zshrc for example.

# redefine prompt_context for hiding user@hostname
prompt_context () { }
0
73

Here is my version from first two answers. They explain very clearly. I will merge again.

  1. step 1. open your .zshrc file by vim .zshrc

  2. step 2. go to end of your file.

  3. Paste this code:

careful indent again your code

prompt_context() {
  if [[ "$USER" != "$DEFAULT_USER" || -n "$SSH_CLIENT" ]]; then
    prompt_segment black default "%(!.%{%F{yellow}%}.)$USER"
  fi
}

Reference link: agnoster theme code

Hope this help :)

2
  • 2
    This should be approved answer, he do not modify library source code that could be a problem later on. Nov 30, 2016 at 2:29
  • 2
    Agreed, this is the most helpful answer ^_^
    – Titus
    Dec 21, 2016 at 5:11
32

I'm using agnoster theme too.

Edit agnoster.zsh-theme and find $user@%m and delete @%m.

Image sample:

enter image description here

1
  • Thanks! But I think this one is for local machine (mac) but will not work on a server as the original question was asking. For a local Mac machine, you also need to source that filename afterwards. Type: "source agnoster.zsh-theme" in the command line without the double quotes.
    – toking
    Oct 17, 2023 at 13:24
15

My config in ~/.oh-my-zsh/themes/agnoster.zsh-theme

prompt_context() {
  # Custom (Random emoji)
  emojis=("⚡️" "🔥" "💀" "👑" "😎" "🐸" "🐵" "🦄" "🌈" "🍻" "🚀" "💡" "🎉" "🔑" "🇹🇭" "🚦" "🌙")
  RAND_EMOJI_N=$(( $RANDOM % ${#emojis[@]} + 1))
  prompt_segment black default "${emojis[$RAND_EMOJI_N]} "
}
1
13

You can set DEFAULT_USER="[user name]" in your .zshrc file and it will stop showing the user@hostname in your terminal.

In order to get the [user name], type id -un in your terminal and it will output the [user name] value.

1
  • 3
    $DEFAULT_USER was empty on cygwin, might also be on some linux boxes. Add to first line of .zshrc export DEFAULT_USER=`id -un`
    – MTJ
    Jul 11, 2016 at 17:32
9

If you're on macOS, the addition of a single line to your ~/.zshrc file is enough to hide the machine name in the Terminal:

# Where we specify the theme
ZSH_THEME="agnoster"
# Force yourself as the system's default user
DEFAULT_USER="$(whoami)"

This will negate the "$user" != "$DEFAULT_USER" check here, thereby hiding the machine name locally while still displaying it for SSH connections.

2

Put this in your .zshrc file before the 'source $ZSH/oh-my-zsh.sh'

DEFAULT_USER=drewr

I use Agnoster as well, and this is what I put to take away the username from displaying.

Hope this helps.

2

We needn't edit agnoster.zsh-theme but append the code below at the end of .zshrc:

export USER=''
prompt_context() {
  if [[ "$USER" != "$DEFAULT_USER" || -n "$SSH_CLIENT" ]]; then
    prompt_segment black default "%(!.%{%F{yellow}%}.)$USER"
  fi
}

we can change export USER='' like export USER='john' to show what we want.

1

1. open ~/.zshrc add line: DEFAULT_USER=whoami or export DEFAULT_USER= myusername (mac os x)

2. open ~/.oh-my-zsh/themes/agnoster.zsh-theme files.

find ## Main prompt add # for prompt_context line.

1

Related to the context: Sometimes sourcing the zshrc by "source ~/.zshrc" might not work. Start a new terminal to see the updated changes.

1

Although the accepted answer is perfectly fine, I prefer to modify my .zshrc file as little as I can to keep things clean.

So, a slightly different approach would be to override the theme (as recommended on https://github.com/robbyrussell/oh-my-zsh/wiki/Customization#overriding-and-adding-themes).

  1. Go to your ~/.oh-my-zsh/custom/themes folder
  2. Create a file called agnoster.zsh-theme
  3. Add your customization and save (for example, here, redefine the prompt_context() so it does nothing):
prompt_context() {}
  1. Update your terminal with your changes:
source ~/.zshrc
0
prompt_context() {
  # Custom (Random emoji)

  MINUTE_TRC=$(date +%M)
  emojis_1=("🐈" "🔥" "🚀" "💡" "😼" "😹" "🙀" "😻" "😽" "🇮🇳" "😺" "😸")
  emojis_2=("🙈" "🙉" "🙊" "🐵" "🦊" "🦁" "🐯" "🐮" "🐰" "🐼" "🧸" "🐿️")    

  if (($MINUTE_TRC % 2 != 0)); then
    RAND_EMOJI_N=$(( $RANDOM % ${#emojis_1[@]} + 1))
    prompt_segment black default "%(!.%F{red}.%F{red})%n%F{green}@%F{Blue}%m %F{reset} ${emojis_1[$RAND_EMOJI_N]} "
  else
    RAND_EMOJI_N=$(( $RANDOM % ${#emojis_2[@]} + 1))
    prompt_segment black default "%(!.%F{red}.%F{red})%n%F{green}@%F{green}%m %F{reset} ${emojis_2[$RAND_EMOJI_N]} "
  fi
}

Improvement on Pongsatorn Nitithammawoot config. This will make the emoji change every min.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.