Full-disclosure: I’ll never win a Bash Script of the Year Award. I occasionally write them, but my skills are pretty basic, so apologies if my attempt to build out a meaningful bash prompt offends your bash sensibilities. Comments are semi-welcomed.
“To truly know a man, first you must know his prompt.”
I’m always intrigued when I see a fellow developer’s terminal prompt, as if I’m peering into his or her nerd soul. If you spend a lot of time in front of a terminal, it’s worthwhile to customize your prompt. Yet I often see people who stick with the system default.
Below is the default on my MacBook, and I honestly can’t imagine using it:
I don’t have many prompt requirements, but the following are non-negotiable must-haves:
- I can see the full pathname of the current working directory. I have lots of directories named target, bin, webapp, etc., so seeing the full path gives me a better chance of catching myself before doing something stupid due to an incorrect assumption about the current directory. Additionally, when scanning your previous commands, you know the working directory in which each was executed.
- My cursor is left-justified. Given that I want to see the full-path, if the cursor is on the same line, this shifts the cursor to the right as the path gets longer. Even the default prompt above shifted the cursor to the right because ‘aloder’ is longer than ‘~’. When I look back in my terminal window, I like having each command start in the same place. I also want to give my verbose commands the best chance of fitting on a single line.
- There’s a blank line before the prompt. This makes it easier to differentiate commands and their output when browsing the scrollback buffer.
Below is a PS1 environment variable that meets the above requirements. This lived in my .bashrc for quite some time.
This is the prompt it creates:
This prompt did the trick for a while, but in order to include the git branch as additional context, I later added in a parse_git_branch function and updated my PS1:
1 2 3 4
This is how it looks after cd-ing into a git working directory in which I’ve checked out the ‘master’ branch:
I was satisfied with this until I started using virtualenv and virtualenvwrapper for my Python development. I wanted to add the current active virtualenv to my prompt context, but I was not a fan of the PS1 update performed by virtualenv’s bin/activate script. I made my own changes but my prompt wouldn’t update the git or virtualenv context after changing directories due to virtualenv’s own PS1 mangling. Rather than figure out what was causing this, I did the following:
- Moved all PS1 properties/code into a separate ~/.bashrc_ps1 file.
- Updated the virtualenvwrapper $WORKON_HOME/postactivate script to source the new file, which ensures the PS1 is unaffected by virtualenv.
1 2 3 4
As for the prompt context itself, I wanted it to be terse. I’ve seen some crazy prompts that put way too much info on the screen. Assuming webpy is my active virtualenv and that I’m on the master git branch, I want the context portion of my prompt to look like this:
If I don’t have a virtualenv activated or am not in a git directory, it should degrade and display only one:
For instance, here’s what it looks like when I have a ‘webpy’ virtualenv activated but am not in a git project:
I also decided that I wanted to display the virtualenv or git branch in a different color if either is non-default; this way, only the non-default values will stand out. For instance, my default virtualenv is named ‘default’, and my typical git branch name is ‘master’.
Below is the full contents of my ~/.bashrc_ps1 file to achieve this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57