Like most rails developer, I spend a good chunk of time navigating around my files using bash everyday. I thought it’d be worth it to spend a couple of hours and learn how to customize an application that I will be using for years to come.
To make changes, locate your “.bash_profile” file and search in it for “PS1”. You will assign a string to the PS1 variable and what goes inside the string dictates what will be displayed in bash.
First off, whatever you put inside the string will be displayed unless you include special escape characters. For example, if you assign:
you will get the following prompt display:
However, if you want your prompt to look like this:
you will have to learn a few escape characters. (Note: this list is non-comprehensive, I’ve included the ones I’ve found useful.)
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
Try sticking a few of the escape characters into the string you assign to PS1 and see what each does to your prompt.
If you want to take your prompt to the next level, you can add some color. In the code below, replace the parentheses and the content inside the parentheses with the characters you want to display. (I’ve added a leading and trailing space for clarity, they should be removed when you sub in your characters.) You change the color by replacing the ‘#’ with a number between 0 and 256.
To change your text color, change the ‘#’ following ‘38;5;’.
To change you background color, change the ‘#’ following ‘48;5;’.
Here are a couple of color tables, both have the exact same color for each number.
The color escape sequence may look daunting, but if you can spot the pattern, you can easily get the output you desire.
The PS1 string for my customized prompt is:
(You can type the apple logo () by pressing shift + alt + k)