Is Your Bash Prompt Cramping Your Style?

It is sometimes hard to form a mental model of a directory tree when working with the command line. GUI shells tend to provide more visual cues. For example, in OS X, the Finder has a column view that allows you to quickly see how directories are nested.

Finder column view showing nested directories

So, like a lot of Bash users, I used to get around this by printing the current working directory in my shell prompt:

Directory structure printed in Bash prompt

However, things can get pretty cramped when dealing with deeply nested directories. A better solution is needed. Ideally, I want a minimal prompt that just displays my user@host. I only need to get my bearings when I’m moving around in the directory tree. Therefore, I’ve come up with the following.

Directory structure printed after every cd.  Bash prompt no longer prints current working directory.

The current working directory is printed after every cd. This allows me to get my bearings when I need to, while keeping my Bash prompt short and sweet. The following incantation in ~/.bashrc does the trick:

# Print working directory after a cd.
cd() {
    if [[ $@ == '-' ]]; then
        builtin cd "$@" > /dev/null  # We'll handle pwd.
        builtin cd "$@"
    echo -e "   \033[1;30m"`pwd`"\033[0m"

Have you come up with other solutions for longpromptitis? Let me know in the comments!

comments powered by Disqus