ZSH: Hide computer name in terminal
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:
It would save some space by removing anwarchoukah@anwars-mbp
, seeing as I actually know who I am:)
try to add export DEFAULT_USER="$(whoami)"
to your .zshrc
file
step 1. one your
.zshrc
file byvim .zshrc
step 2. go to end of your file.
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.
On MacOS 10.15 Catalina:
- Open the file
/private/etc/zshrc
in a text editor - Locate the comment:
# Default prompt
- Modify the line that looks like this:
PS1="%n@%m~ %& # "
- Save the file. You will be prompted to enter a password to save it.
- 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
Open up .zshrc
, find the line reading export PS1
or maybe export PROMPT
.
Remove the variable that is used for the hostname, could be %m or %M.
Zsh on Archlinux wiki
You can use colors and also have a prompt (or some information) on the right side.