3

I am pretty new to Linux so please bear with me here :)

I want to customize my shell prompt (in zsh). Right now it looks like this (not written by me):

PROMPT="${user_host} ${current_dir} ${rvm_ruby} 
%B$%b "

Now what are these variables ${user_host} etc. called and where can I see a list of them such that I can include things like e.g. the current time in my prompt?

Ron
  • 20,638
AlphaOmega
  • 1,423

1 Answers1

3

The variables such as ${user_host} can be set to desired values inside your .zshrc file.

For example adding the following lines in your .zshrc:

local USER_HOST="${_prompt_colors[4]}%n@%m"
local CURRENT_DIR="${_prompt_colors[5]}%~"
PROMPT="${USER_HOST} ${CURRENT_DIR}$ "

will give you a prompt like:

ron@ron ~$

The characters that start with % in the above codes are special 'escape' sequences that are used to specify different kinds of information. Within the PROMPT variable, any occurance of these % sequences are replaced by the information that they represent. In the above codes:

  • %n represents username and is equivalent to $USERNAME
  • %m represents the hostname up to the first .
  • %~ represents $PWD, but will do two types of substitutions. If a named dir X is a prefix of the current directory, then ~X is displayed. If the current directory is your home directory, $HOME, just ~ is displayed.

Now, you have various options to show data/time:

  • %t - Current time of day, in 12-hour, am/pm format.
  • %T - Current time of day, in 24-hour format.
  • %* - Current time of day in 24-hour format, with seconds.
  • %w - The date in day-dd format.
  • %W - The date in mm/dd/yy format.
  • %D - The date in yy-mm-dd format.

So,

PROMPT="${USER_HOST} ${CURRENT_DIR}%t$ "

will give a prompt like:

ron@ron ~9:11AM$

See 'Expansion of prompt sequences' in man zshmisc or have a look at this or this for more options that are available.

Ron
  • 20,638
  • 1
    If you like the answer, just click the little grey ☑ under the "0" now turning it into beautiful green. If you do not like the answer, click on the little grey down-arrow below the 0, and if you really like the answer, click on the little grey checkmark and the little up-arrow... If you have any further questions, just ask another one! – Fabby Aug 02 '15 at 12:36