ZSH: Hide computer name in terminal
Asked Answered
K

17

109

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

Klepac answered 6/8, 2015 at 7:6 Comment(4)
Ask this question at superuser.com. You have more change for an answer thereCarisacarissa
Go to your ~/.oh-my-zsh/themes folder, open the theme you are using, comment out the line contains prompt_context or something similar. Or try to remove $USER@%m from the theme file.Corriecorriedale
Thx @MaxPeng! You should have made it a full answer...Blouse
open finder and go to /Users/{YourUserName}/.zshrc and add below line in the top of file. export DEFAULT_USER="$(whoami)Ambroseambrosi
P
29

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.

Prussia answered 7/8, 2015 at 9:23 Comment(1)
by adding this PS1="%1d %& # " would show you current working directoryMetalline
T
197

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

Taut answered 13/7, 2016 at 21:34 Comment(6)
Please note, this will hide both user and hostname (the prompt will be empty). This works because prompt will be populated only if DEFAULT_USER variable is different to USER variable, thus setting both to be the same hides the prompt.Irbm
You can use $USER instead of "$(whoami)"Triplicity
Thanks for this hint! Instead of using $USER or whoami I set it to my normal logged in user and now I only get the user@host when su'd into another account. This is perfect for me!Tetragonal
For the fizsh frontend of ZSH, in the file ~/.fizsh/fizsh-prompt.zsh, I modified the two lines including _fizsh_prompt="%n@%m... to _fizsh_prompt="%n....Battleship
best answer i have been searching for since last 20mins. thanksTenotomy
Hmm this doesn't do anything for me.Triplane
B
73

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
Bores answered 28/1, 2020 at 7:58 Comment(5)
You need to use the sudo command to modify the file. I used 'sudo nano /private/etc/zshrc' enter your password and then you'll be able to edit the file in terminal. After you've successfully saved your changes, close your old terminal and start a new one and you will see the changes.Oribelle
If you want a minimal terminal username, follow the steps above and in the # Default prompt section below, use the following: PS1="%# "Aircondition
by adding this PS1="%1d %& # " would show you current working directoryMetalline
would be better to do this in ~/.zshrc => export PS1="apple@apple-pro %1~ %# " => then source ~/.zshrc to reload your terminalMarniemaro
this file does not exist on my system.Triplane
T
54
  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.

Thickskinned answered 10/7, 2016 at 19:51 Comment(2)
Thanks. Tried hours. Your solution helped me.Googly
I wanted to remove the host and only show the user, this worked greatSeparation
P
29

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.

Prussia answered 7/8, 2015 at 9:23 Comment(1)
by adding this PS1="%1d %& # " would show you current working directoryMetalline
G
17

For the macOS I like this approach

put in .zshrc

PS1="%n$ "

The terminal will look like

username$

Gridley answered 16/3, 2021 at 11:0 Comment(0)
C
13

Just add prompt_context() {} to .zshrc

Chery answered 8/6, 2019 at 3:47 Comment(2)
this is problematic if you logged in as a different user, because you will not know which user you are in right nowCeramics
This is actually perfect, because you can still see git branches on terminal, instead of fixing a single value.Hidden
J
11

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.

enter image description here

Voila this worked. Now to me it just displays the machine name, as I intended.

enter image description here

Hope that helps. Many links and SOF posts only made me click that solution.

Jeremiad answered 21/7, 2021 at 17:5 Comment(0)
C
4

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
Canicular answered 1/9, 2017 at 14:28 Comment(0)
I
4

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:

  1. Open the .zshrc file using sudo nano ~/.zshrc

  2. Add the line POWERLEVEL9K_CONTEXT_TEMPLATE="%n" at the end of .zshrc file

  3. Save the file.

Illumine answered 17/5, 2021 at 7:41 Comment(0)
J
3

For hiding the computer name without removing the current directory name instruction. Add this in .zshrc

PS1="%n@ %1~ %# "

And run this

source ~/.zshrc 

And restart the terminal!!!

Jonson answered 5/3, 2023 at 18:41 Comment(0)
A
2

Maybe this will help you [ Open Profile => Shell ]

PS1="~ $: ";clear;
Ambulance answered 14/4, 2020 at 10:38 Comment(1)
there is no such optionLinkboy
R
1

Just add this to your ~/.zshrc file:

export DEFAULT_USER=\`whoami`
Regicide answered 21/6, 2017 at 15:39 Comment(0)
R
1

Install Oh My Zsh is the easiest solution for me:

https://ohmyz.sh/

One liner install:

sh -c "$(curl -fsSL https://raw.github.com/ohmyzsh/ohmyzsh/master/tools/install.sh)"

Result:

enter image description here

Recalescence answered 23/5, 2021 at 12:1 Comment(3)
bro come-on! You are using the default theme on OMZ here, which is robbyrussell and this theme settings does that change for you. People are wondering for those changes in another theme or without theme!Jeremiad
hey, i'm not 100% understand, would you care to explain more :)Recalescence
Hey no issues mate. I just meant is that, to remove username@machinename is a task. Installing OMZ does that automatically for you because it uses a theme, one I mentioned above, and that theme has this property where it hides them for you. But it may be a challenge for other themes in the package. For eg. the one OP has asked is agnoster. Nevermind, here is how I achieved a desired change, in Answer below.Jeremiad
P
1

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.

Pearlinepearlman answered 15/10, 2021 at 11:14 Comment(0)
C
1

I don't know why I can't find a simplified solution.

So here is the simplified one.

Just jump into the themes directory,

# ~/.oh-my-zsh/themes

Select the theme, as I have been using

# vim agnoster.zsh-theme

Just remove %m to remove and also you can remove the username too.

Once done, Just run

#zsh 

To reflect the changes, Enjoy :)

Costumier answered 2/6, 2022 at 12:35 Comment(0)
A
0

open zshrc file finder, by clicking going to path /Users/{YourUserName}/.zshrc and add below line in the top of file.

export DEFAULT_USER="$(whoami)

enter image description here

Ambroseambrosi answered 26/4 at 5:45 Comment(0)
K
-2

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"

Klepac answered 6/8, 2015 at 7:23 Comment(4)
This assumes that you are using ZSH_THEME, which is not necessary for modifying the prompt.Salespeople
I'm a total noob when it comes to the command line, but am trying to learn. Your comment implies there's a better way. Any chance you could expand on it?Klepac
One of the things that a zsh theme does is set the value of the parameter PS1 (or maybe PROMPT; they are synonyms). This value is what is displayed as your main prompt. See man zshmisc, especially the section "SIMPLE PROMPT ESCAPES", to see how various special character sequences are displayed when they appear in the value of PS1.Salespeople
@Salespeople - that did it – enough for me to know where to start. Thanks!Klepac

© 2022 - 2024 — McMap. All rights reserved.