UPDATE: I’ve moved this code into my dotfiles repo as I continue to improve and update it.
I have always liked having my full path in my bash prompt, and also (when applicable) my current git branch. This way I always have the proper context I need to be able to navigate quickly and prevent myself from having to constantly type pwd
to figure out where I am.
This works well until you’re working in a smaller terminal window and are deep within a project. For one reason or another you’re down to 80-100 columns, and your prompt is taking up the entire width of the terminal (and then some).
I also have attempted to use the 2 line prompt where the info is on the first line, leaving your commands to be entered on the next line, but I find this really annoying. If you’re in the same boat as me, let me save you the hours of work on something so trivial.
The responsive prompt in action
I have my prompt set up to switch at 110 columns to a more concise layout. At more than 110 columns, it looks like this.
When I go below the terminal width of 110 columns I wanted to streamline it, while still providing relevant info that will help me get 90% of the information I get from the full prompt. This is what I ended up with.
Currently it checks the terminal width every time a new prompt is rendered, so reloading your bash environment is not necessary.
Extra
Another neat thing I recently did with my prompt is change the color of the arrow to red if the previous command returns an exit status other than zero.
The Code
If you’re new to bash prompt customization, please at least skim through this before continuing. It provides the basics of prompt customization, this is a more advanced customization.
Otherwise, you can take a look in my dotfiles repository.