home of the madduck/ blog/
Adding VCS information to the Zsh prompt

I was excited by Pierre's idea to add Git branch information to the Zsh prompt and even more so when I saw Mike implement support for multiple VCSs.

Unfortunately, Mike's a Bash user, and so I took it upon myself to port the idea to Zsh. The file 60_vcsprompt is sourced from my .zshrc, which sets psvar[1] through psvar[3]. Those are then used in 80_prompt (also sourced from .zshrc) when setting $PS1.

My prompt follows the same principle as Mike's and puts the branch name at the repository root location in the repository path. In the following example, ~, ~/code, and ~/code/netconf/netconf are three separate Git repositories, while ~/code/unionfs-fuse and ~/code/unperish are maintained with Mercurial and Bazaar respectively:

lapse:~|master|% cd code
lapse:~/code|master|% cd netconf 
lapse:~/code|master|netconf% cd netconf
lapse:..e/netconf/netconf|master|% cd src
lapse:..etconf/netconf|master|src% git checkout no-threads
Switched to branch "no-threads"
lapse:..nf/netconf|no-threads|src% cd ../../../unionfs-fuse 
lapse:../unionfs-fuse|hg:default|% cd ../unperish
lapse:..unperish|bzr:unperish@159|%

You'll notice that unlike Mike's prompt, mine's limited to a maximum length of 25 characters. However, the repository root path is kept at least 10 characters long, so the prompt might get longer than 25 characters if you descend deep into a repository's subdirectories.

I couldn't easily figure out how to add support for other version control systems, so if you do, please feed back the patches! And the same goes for suggestions and improvements.

One of the next things I am planning to implement is an indicator for when your working tree contains uncommitted changes, e.g.:

lapse:..etconf/netconf|master|src% touch foo
lapse:..tconf/netconf|master*|src%

So watch those files.

NP: Gazpacho: Bravo