Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ZSH: Hide computer name in terminal

Tags:

prompt

zsh

How would I hide the computer name in the prompt while using the terminal?

At the moment it shows both username and computer name like so:

iTerm screenshot of user prompt

It would save some space by removing anwarchoukah@anwars-mbp, seeing as I actually know who I am:)

like image 810
achoukah Avatar asked Aug 06 '15 07:08

achoukah


People also ask

How do I change the prompt in oh my zsh?

To customize the ZSH prompt, we need to modify the prompt= variable inside the . zshrc file. We can populate the prompt variable with various placeholders, which will alter how the ZSH prompt appears.


3 Answers

Try to add export DEFAULT_USER=$USER to your .zshrc file

like image 188
Alex Kompaniets Avatar answered Oct 16 '22 09:10

Alex Kompaniets


On MacOS 10.15 Catalina:

  1. Open the file /private/etc/zshrc in a text editor
  2. Locate the comment: # Default prompt
  3. Modify the line that looks like this: PS1="%n@%m~ %& # "
  4. Save the file. You will be prompted to enter a password to save it.
  5. Load a new Terminal window.

For example, you can:

  • Remove "%n@%m" to get rid of both the username and computer name
  • Remove "%n" to get rid of the user name
  • Remove "%m" to get rid of the machine name
like image 23
Sebastian Knopp Avatar answered Oct 16 '22 10:10

Sebastian Knopp


  1. step 1. one 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
}

EDIT - explaining what this does

This will remove the machine@user name from the prompt IF: - you are not logged in as the default user - you are not in an ssh client shell

For most people, not needed, but if you regularly ssh to other machines and have multiple terminals open (eg remote sys admin type work) then this is extremely useful so when you look at your terminal you know which machine and user you are logged in as inside that terminal.

If you don't need/manage that type of complexity then use one of the other answers to just modify your export PROMPT/PS1 value.

* WARNING *

If you are using a custom shell or theme, this might not work and although the prompt will no longer show your computer and username it will keep throwing the error:

prompt_context:2: command not found: prompt_segment

For example, you can see with this (very popular) powerlevel9k it does not work. This is because the Powerlevel9k theme uses it's own magic and you simply add commands to your ~/.zshrc file to get the same result, eg:

POWERLEVEL9K_LEFT_PROMPT_ELEMENTS=(context)

More info on that here.

like image 43
hqt Avatar answered Oct 16 '22 11:10

hqt