Zsh prompt updates

May 22, 2009


I wanted some git information in the prompt I currently have. While thinking and googling about this, a quote on Bart’s log got me thinking. I don’t need to see my current host, nor do I need to see my current user name.

I always login under my own account (miekg) and if I do change accounts, I probably will be forced to use bash. So showing the current user name is quite useless.

Showing the host you are logged into is also only worth showing when you ssh-ing to a remote host.

So my default prompt now only shows the prompt (%) on the left and the current directory (~) on the right, like this:

Default zsh prompt

Very clean.

When logging into a remote system, the hostname of the system is added (in grey) to the right side of the prompt, like so: (I’m ssh-ing to my other host called elektron, which is electron in Dutch)

Remote zsh prompt

The code to do this:

[[ -n "$SSH_CLIENT" ]] && __ZH=$HOST

if $SSH_CLIENT is set, set $__ZH to the current hostname, then use $__ZH in your prompt.

Next I wanted some git support in my prompt, there are lots of sites which can help you. I stole code from all of them :)

There was however one annoying thing. I store my entire home directory in git, but I don’t want to see git information about it all the time. So I created the following function to detect if my the current git repository was the one from my home directory:

zsh_git_home() {
# I have my homedir in git, quite annoying to see 'master' all the time
git_dir=$(git rev-parse --git-dir 2> /dev/null)
[[ -z "$git_dir" ]] && return 0
[[ $(realpath "$git_dir") == $HOME/.git ]] && return 0
return 1

If I enter a git repository the current branch is put into the prompt at the left side, here I enter a repository with the master branch. (Note: as the window is very small my path is truncated)

Branch zsh prompt

Code is from Bart.

One other thing I added is that if the working directory is not clean (i.e. you have made some changes) an exclamation mark is added. Like so:

Branch change zsh prompt

The code for this can be found on the sites listed above.

Of course my other prompt features still work. Here is a shot with one background job. Notice that ^Z suspends this program and leaves an exit code of 20, which is shown in red:

Showing all

I’m still figuring out ways to get even more colors in the prompt :)