Setting the ZSH prompt: It's as exciting as it sounds!
Tuesday, 5. June 2007, 15:47:03

NOTE: ZSH now sets pwd (present working directory) for the prompt by default, or at least it does for Ubuntu. This makes this blog post superfluous for most people who might want to try zsh. SO TRY IT ALREADY! 8-1-2008
I first ran the zshell (zsh) in late 2006, after reading the following:
"zsh
Z shell. The newest of the shells. Compatible with the Bourne shell. Offers command-line editing. Has very powerful completion features. If you do not know any shell yet, and your Linux distribution carries zsh, go with that choice from the start."
Strong words! That's from the fifth edition of Running Linux, By Matthias Kalle Dalheimer and Matt Welsh and published by O'Reilly, which claims to be one of the first and oldest Linux manuals, and considering that it's in the fifth edition (first edition 1995), It's probably true.
Just because a manual, even an historically important manual, says to use zsh is not a reason to switch... but it's a good reason to try it. It's real easy to try any alternative shell (as long as it's installed, of course) all you have to do is type the path of the shell:
/usr/bin/zsh
and your new shell starts up.
If you want to change the login shell, the command is:
chsh
You'll be prompted for your password and then for the path of the shell.
For Debian, the default path for zsh is /usr/bin/zsh.
The path for bash is /bin/bash
I was using SUSE at the time, and SUSE makes zsh really easy. It's all set up to look and feel like BASH, only with a better tab completion. When there are a lot of options for completing a command or filename, zsh will list them, and then, if you keep hitting the TAB key, it will scroll through the possible completions, unlike BASH-- which will list the possible completions, but then if you want to get to the one you seek, you're going to have to do more typing.
And so, with SUSE's preconfigured version of zsh, I got a small immediate benefit of smoother tab completion with no downside, and the prospect of more advanced features that I might discover in the future, plus it was cool to be using something that most people weren't using. I liked zsh... and that was a good reason to switch.
I was really happy with zsh... but then I switched from SUSE to Debian. In Debian, the z shell is not preconfigured at the default, the way bash is. In debian, when you open the bash shell to the ~ directory, the prompt looks like this:
oscar@debian:~$
change directory to ~/Desktop, and the bash prompt looks like this:
oscar@debian:~/Desktop$
With zsh unconfigured in debian, the ~ prompt looks something like this:
debian%
and when you cd to ~/Desktop, it looks like this:
debian%
and so on. I've always found it helpful to have the prompt to remind me what directory I'm in. Using the pwd (present working directory) command was too much trouble for the tab completion advantages to justify, and so it was back to bash for a while But last night, with a google search for "zsh prompt" and some fortunate intuition, I was able to set the prompts for zsh.
Yes I said prompts, plural! ZSH can actually be set up to display two prompts: a left prompt and a right prompt! Here's what it looks like:

The left prompt contains the usual directory information. The right prompt contains the time and date-- because that's what I set it up to display. There's a long list of possibilities for both prompts that are listed below. The right hand prompt will automatically vanish if I need that space to type. I'm not sure how useful it's going to be to have time and date information in the prompt... I already have a clock on my desktop, after all... but it looks kind of cool. There are ways to add color to the prompt, and I'll be trying to figure that out next.
That was entirely too much "why", so I'm going to cover the "how" rather quickly.
In the configuration file, I set the prompts like so:
PROMPT='%d%>:%{\e[0m%}' # default prompt
RPROMPT='[%* on %D]' # prompt for right side of screen
The code for setting the prompt is called "escape sequences". Each sequence begins with the percent sign:
%d
Your current working directory relative to /home/
%~
Your current working directory relative to /home/member/u/username
%B....%b
Begin and end bold print
%U....%u
Begin and end underlined print
%M
server name - prodigy.redbrick.dcu.ie
%m
The part of the hostname up to the first . - prodigy
%n
Your Login Name
%T
System time in HH:mm format
%*
System time in HH:mm:ss format
%D
Today's Date in YY-MM-DD
%l
Your current tty e.g. pts/100
Below is the entire configuration file, verbatim, which resides in my system at /etc/zsh/zshrc. The part where I set the prompts is printed in bold italics, and begins at line 15. I got the information on how to set the prompts from this page.
To configure your zsh like mine, you only need to copy all the bold text below to a textfile, save the textfile as zshrc, and you can further customize it to your liking using the escape sequences listed above.
If you prefer to have only one (left-hand) prompt, you can disable the right hand prompt in the usual way by "commenting" with the # symbol:
#RPROMPT='[%* on %D]' # prompt for right side of screen
Then you can copy or move the file (as root or using sudo) to /etc/zsh.
This is really a kind of turning point for me, since a configured zsh was about the only thing that I wasn't getting in Debian that I used to get in SUSE. SUSE's a great system, but Debian gives me more, now that I know how to get it. More on that later.
To be honest, it's going to be a while-- maybe years-- before the advanced tab completion and other features of zsh save me all the time that I spent learning how to set up zsh to my liking... but what the hell, I've got things set up just the way I like them, and "the way I like them is exactly the way things ought to be". This time, it's not about time; it's about satisfaction.
My /etc/zsh/zshrc file:
#
# /etc/zshrc is sourced in interactive shells. It
# should contain commands to set up aliases, functions,
# options, key bindings, etc.
#
if [ -r /etc/profile ] ; then
. /etc/profile
fi
# Shell functions
setenv() { export $1=$2 } # csh compatibility
# Set prompts
PROMPT='%d%>:%{\e[0m%}' # default prompt
RPROMPT='[%* on %D]' # prompt for right side of screen
#key setups
# bindkey SO HERE'S HOW I CONFIGURED THE PROMPT FOR ZSH:-v # vi key bindings
bindkey -e # emacs key bindings
bindkey ' ' magic-space # also do history expansion on space
# setup backspace correctly
stty erase `tput kbs`
#delete key
bindkey '\e[3~' delete-char
#home
bindkey '\e[1~' beginning-of-line
m really dependent of th
#end
bindkey '\e[4~' end-of-line
#insert
bindkey '\e[2~' overwrite-mode
#tab completion
bindkey '^i' expand-or-complete-prefix
#pick your favorite editor
#export EDITOR="emacs"
#export EDITOR="vi"
#export EDITOR="vim"
#export EDITOR="gedit"
## zsh specific options
#keep background processes at full speed
setopt NOBGNICE
#restart running processes on exit
setopt HUP
#
setopt PROMPT_SUBST
#keep history file between sessions
HISTSIZE=1000
SAVEHIST=1000
HISTFILE=~/.history
setopt APPEND_HISTORY
#for sharing history between zsh processes
#setopt INC_APPEND_HISTORY#
# /etc/zshrc is sourced in interactive shells. It
# should contain commands to set up aliases, functions,
# options, key bindings, etc.
#
if [ -r /etc/profile ] ; then
. /etc/profile
fi
# Shell functions
setenv() { export $1=$2 } # csh compatibility
# Set prompts
PROMPT='%d%>:%{\e[0m%}' # default prompt
RPROMPT='[%* on %D]' # prompt for right side of screen
#key setups
# bindkey SO HERE'S HOW I CONFIGURED THE PROMPT FOR ZSH:-v # vi key bindings
bindkey -e bold # emacs key bindings
bindkey ' ' magic-space # also do history expansion on space
# setup backspace correctly
stty erase `tput kbs`
#delete key
bindkey '\e[3~' delete-char
#home
bindkey '\e[1~' beginning-of-line
#end
bindkey '\e[4~' end-of-line
#insert
bindkey '\e[2~' overwrite-mode
#tab completion
bindkey '^i' expand-or-complete-prefix
#pick your favorite editor
#export EDITOR="emacs"
#export EDITOR="vi"
#export EDITOR="vim"
#export EDITOR="gedit"
## zsh specific options
#keep background processes at full speed
setopt NOBGNICE
#restart running processes on exit
setopt HUP
#
setopt PROMPT_SUBST
#keep history file between sessions
HISTSIZE=1000
SAVEHIST=1000
%d
Your current working directory relative to /home/
%~
Your current working directory relative to /home/member/u/usernameZSH prompt configuration issue SOLVED!
%B....%b
Begin and end bold print
%U....%u
Begin and end underlined print
%M
server name - prodigy.redbrick.dcu.ie
%m
The part of the hostname up to the first . - prodigy
%n
Your Login Name
%T
System time in HH:mm format
%*
System time in HH:mm:ss format
%D
Today's Date in YY-MM-DD
%l
Your current tty e.g. pts/100
HISTFILE=~/.history
setopt APPEND_HISTORY
#for sharing history between zsh processes
#setopt INC_APPEND_HISTORY
#setopt SHARE_HISTORY
#never ever beep ever
setopt NO_BEEP
#"running" `/etc` actually does a `cd /etc`
setopt AUTO_CD
#correct mistakes
setopt CORRECT
setopt AUTO_LIST
#allow tab completion in the middle of a word
setopt COMPLETE_IN_WORD
#tab completion moves to end of word
setopt ALWAYS_TO_END
setopt listtypes
# %1 killed. will show up exactly when it is killed.
setopt interactivecomments
unsetopt flowcontrol
#setopt
#setopt
#automatically decide when to page a list of completions
LISTMAX=0
#MAILCHECK=0
limit coredumpsize 0
# The following lines were added by compinstall
zstyle ':completion:*' completer _expand _complete _correct _approximate
zstyle ':completion:*' completions 1
zstyle ':completion:*' file-sort name
zstyle ':completion:*' glob 1
zstyle ':completion:*' insert-unambiguous true
zstyle ':completion:*' list-colors ${(s.:.)LS_COLORS}
zstyle ':completion:*' list-prompt %SAt %p: Hit TAB for more, or the character to insert%s
zstyle ':completion:*' max-errors 2
zstyle ':completion:*' original true
zstyle ':completion:*' prompt 'correction: %e '
zstyle ':completion:*' squeeze-slashes true
zstyle ':completion:*' substitute 1
autoload -U compinit
compinit
# End of lines added by compinstall
autoload -U colors
colors
#boring default prompt
#with bash compatibility (bash doesn't use PROMPT, only PS1)
export PROMPT="[%n@%m %1/]%# "
#export PS2="> "
#setopt SHARE_HISTORY
#never ever beep ever
setopt NO_BEEP
#"running" `/etc` actually does a `cd /etc`
setopt AUTO_CD
#correct mistakes
setopt CORRECT
setopt AUTO_LIST
#allow tab completion in the middle of a word
setopt COMPLETE_IN_WORD
#tab completion moves to end of word
setopt ALWAYS_TO_END
setopt listtypes
# %1 killed. will show up exactly when it is killed.
setopt interactivecomments
unsetopt flowcontrol
#setopt
#setopt
#automatically decide when to page a list of completions
LISTMAX=0
#MAILCHECK=0
limit coredumpsize 0
# The following lines were added by compinstall
zstyle ':completion:*' completer _expand _complete _correct _approximate
zstyle ':completion:*' completions 1
zstyle ':completion:*' file-sort name
zstyle ':completion:*' glob 1
zstyle ':completion:*' insert-unambiguous true
zstyle ':completion:*' list-colors ${(s.:.)LS_COLORS}
zstyle ':completion:*' list-prompt %SAt %p: Hit TAB for more, or the character to insert%s
zstyle ':completion:*' max-errors 2
zstyle ':completion:*' original true
zstyle ':completion:*' prompt 'correction: %e '
zstyle ':completion:*' squeeze-slashes true
zstyle ':completion:*' substitute 1
autoload -U compinit
compinit
# End of lines added by compinstall
autoload -U colors
colors
#boring default prompt
#with bash compatibility (bash doesn't use PROMPT, only PS1)
export PROMPT="[%n@%m %1/]%# "
#export PS2="> "










melic # 27. September 2007, 13:46
blackbelt_jones # 1. August 2008, 16:37
(edit)
Ah, but I just discovered that zsh has apparently been upgraded so that the prompt gives the pwd by default! Huzzah!