ZSH: Hide computer name in terminal
ZshPromptZsh Problem Overview
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:)
Zsh Solutions
Solution 1 - Zsh
try to add export DEFAULT_USER="$(whoami)"
to your .zshrc
file
Solution 2 - Zsh
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
Solution 3 - Zsh
-
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.
Solution 4 - Zsh
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.
You can use colors and also have a prompt (or some information) on the right side.
Solution 5 - Zsh
I like this approach (on my mac)
put in .zshrc
PS1="%n$ "
The terminal will look like
username$
Solution 6 - Zsh
Just add prompt_context() {}
to .zshrc
Solution 7 - Zsh
Unfortunately none of the .zshrc
changes worked for me.
Machine : Mac M1, Big Sur 11.4
So this is what worked.
I Navigated to where the ZSH themes were installed, opened my theme, agnoster
in TextEdit, and modified the configuration where it chooses what do display, which by default is $username@%m
.
Note : %m
here is the machine name.
Here is a screenshot of delta. Yellow is what I did, Green is the default setting from github version of agnoster
theme.
Voila this worked. Now to me it just displays the machine name, as I intended.
Hope that helps. Many links and SOF posts only made me click that solution.
Solution 8 - Zsh
Set DEFAULT_USER
in ~/.zshrc
file to your regular username. You can get your exact username value by executing whoami
in the terminal.
Something like this:
export DEFAULT_USER=username
Solution 9 - Zsh
If you are using PowerLevel9k theme, there is a variable POWERLEVEL9K_CONTEXT_TEMPLATE
that can change the view of your hostname and computer name.
The default option is %n@%m
,
- %n -> username
- %m -> machine name
to hide hostname:
-
Open the
.zshrc
file usingsudo nano ~/.zshrc
-
Add the line
POWERLEVEL9K_CONTEXT_TEMPLATE="%n"
at the end of .zshrc file -
Save the file.
Solution 10 - Zsh
Maybe this will help you [ Open Profile => Shell ]
PS1="~ $: ";clear;
Solution 11 - Zsh
Just add this to your ~/.zshrc
file:
export DEFAULT_USER=\`whoami`
Solution 12 - Zsh
Install Oh My Zsh
is the easiest solution for me:
One liner install:
sh -c "$(curl -fsSL https://raw.github.com/ohmyzsh/ohmyzsh/master/tools/install.sh)"
Result:
Solution 13 - Zsh
If you're using Powerlevel10k, then you can run p10k configure
and configure the output a bit.
My username and computer-name was gone after having gone through it. It feel less hacky, than the other solutions here.
Solution 14 - Zsh
Thanks to Wes Bos' amazing video series Command Line Power User , I managed to find the answer.. It's pretty simple. Stuff like that is set in the ZSH theme.
Open up .zshrc
, change the theme from ZSH_THEME="agnoster"
(which is what I was using) to something else.
Wes Bos has made a lovely theme called Cobalt2 which does exactly what I was looking for :)
I've now changed it to ZSH_THEME="cobalt2"