Prefix or Suffix oh-my-zsh's shell prompt

16/Feb/2022 · 1 minute read

Almost every developer loves his/her cool and colorful shell, so I install oh-my-zsh on my laptops and remote servers. However, as I use similar themes of oh-my-zsh and I also have the exactly same user name, most time I could not distinguish the actual machine on which I was operating.

I tried to search online about how to decorate the zsh prompt so that I can insert a machine label within it. Unfortunetely, all articles which I could find specified that it has to overwrite the PS1 or PROMPT environment variable in the ~/.zshrc file. These kinds of solutions will nail the prompt even though users change their zsh theme in the future.

Fortunately, I suddenly realized that I could export the PROMPT variable with shell substitution, that is my final solution:

# ~/.zshrc
source $ZSH/oh-my-zsh.sh
# ... some lines, it is worth mentioning that the below
# line have to be after `source $ZSH/oh-my-zsh.sh`
export PROMPT="[devcloud]$PROMPT"

Now my shell will always be prefixed a [devcloud] label, I also added different labels on my different VPS so that I can judge if I am operating on a server quickly.

The below shows the screenshot that prefix the prompt with a [devcloud] label.