My fancy Zsh prompt
I switched from
zsh a few years ago and I am never looking back. It
has awesome tab completion: for example
cd doc/sh/pl becomes
Documents/shared/planning. It also expands git commands and branches,
environment variables and other things. It has extended file globbing which
provides a good replacement for
find. If you spend a lot of time in the
shell, you should really give
zsh a try.
Whatever shell you use, it’s worth spending a few minutes configuring your prompt. The prompt is something you will see literally thousands of times a day. So why not make it useful?
Here is mine. First, let’s open a new shell in the home directory:
Simple and to the point.
~! Let’s go into some directory:
It displays the path, from the home directory, with the current directory displayed in bold (“planning”). To keep the prompt short, we only display the last 3 subdirectories:
And now let’s move into a git repo:
The prompt displays the path starting at the root of the repo (“org”), and also the current branch in green (“master”). Now let’s move into a subdirectory in the same repo:
The root of the repo is highlighted (“org”), and the current directory is displayed in bold like before. We could also display something indicating if the repo is clean or if it has uncommitted changes, but I prefer to keep it simple.
Here is the code (in
Notice the function
current_git_branch: this is the fastest way I have found
to get the name of the branch. The trick is to make the execution of the prompt
as fast as possible, since it gets executed every time you hit Enter.