ZSH: Hide computer name in terminal

余生颓废 提交于 2019-12-03 05:42:08

问题


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


回答1:


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.

Customizing your shell prompt

By the way you can use colors, and you can also have a prompt (or some info) on the right side.

Zsh on Archlinux wiki




回答2:


try to add export DEFAULT_USER="$(whoami)" to your .zshrc file




回答3:


  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.




回答4:


Just add prompt_context() {} to .zshrc




回答5:


Just add this to your ~/.zshrc file:

export DEFAULT_USER=\`whoami`



回答6:


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



回答7:


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"



来源:https://stackoverflow.com/questions/31848957/zsh-hide-computer-name-in-terminal

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!