I’ve been asked a few times recently about the prompt I have in my terminal.
It looks like this…
The people who’ve asked me about it have wondered what it shows, how it was done and how they can do it for themselves, so I thought I’d write it up for anyone that might be interested.
What does it show?
Each of these things automatically updates (as required) each time you run a command in the terminal. It was in part inspired by one of those fancy shell add-ons, like gbt, that does some of this and lots more. This approach is much lighter though, since it doesn’t rely on installing anything and is specifically tailored to my needs.
I don’t need anything particularly fancy looking. When I’m working in a terminal session, simplicity and reliability are key.
With that in mind I set out to update my prompt myself and came up with what you see above.
How do you do it?
This is what I came up with:
export PS1="\[\033[95m\]\u@\h \[\033[32m\]\W\[\033[33m\] [\$(git symbolic-ref --short HEAD 2>/dev/null)]\[\033[00m\]\$ "
(You may need to side-scroll to see all of it, sorry!)
This needs to go into your .bashrc file in your home directory.
Breaking it down
‘PS1’ is where bash keeps your default prompt configuration.
The bits that look like ‘\[\033[95m\]’ are ANSI colour codes – which you can see lots more examples of here – and they set the colours you see in the prompt. If we strip those out we get something that’s a little easier to read:
export PS1="\u@\h \W [\$(git symbolic-ref --short HEAD 2>/dev/null)]\$ "
(Using this as-is would recreate my prompt but using your default terminal text colour.)
Some of these are prompt shortcuts:
- ‘u’ is for username.
- ‘@’ is a literal @
- ‘h’ is system hostname.
- ‘W’ is the current working directory. ‘w’ is the full path if you prefer, but it’s often too long for my liking.
The git command runs in a sub-shell (that’s why it’s wrapped in
$()). The command itself just prints out the current branch and redirects any errors away to
/dev/null. This part is important because
git symbolic-ref --short throws an error when you’re not in a git managed directory and we don’t want that error to show up in our prompt.
sellorm@gouda ~ $ git symbolic-ref --short HEAD fatal: not a git repository (or any of the parent directories): .git
Redirecting the output stops these sorts of error messages from polluting your prompt.
Some other fun prompts
All of the following result in prompts that look like ‘>>>’ but in a variety of national flag colours.
Set them temporarily by running the below commands or add one to your .bashrc file to make the change permanent.
You can recover from a temporary change by closing the terminal and opening it again, or changing PS1 to something else.
export PS1="\[\033[31m\]>\[\033[37m\]>\[\033[34m\]>\[\033[00m\] "
export PS1="\[\033[34m\]>\[\033[37m\]>\[\033[31m\]>\[\033[00m\] "
export PS1="\[\033[31m\]>\[\033[33m\]>\[\033[32m\]>\[\033[00m\] "
Republic of Ireland
export PS1="\[\033[32m\]>\[\033[00m\]>\e[38;5;208m\]>\[\033[00m\] "
You can manipulate your prompt in a huge number of ways. There are even PS2, PS3 and PS4 prompts that you can configure! I like that my prompt is simple, but informative. It gives me information that I find generally useful, but doesn’t get in the way of my work.
Why not configure your own prompt and send me a pic! As always, you’ll can find me on twitter.