Rewbie Newbie

Documenting my path on becoming a Rails Developer.

Customize Your Bash Prompt

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:

1
export PS1 = "Prompt >> "

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
\d : the date in "Weekday Month Date" format (e.g., "Tue May 26")

\D{format} :  the format is passed to strftime(3) and the result is inserted into the prompt string; an empty format results in a locale-specific time representation. The braces are required

\e : an ASCII escape character (033)

\h : the hostname up to the first '.'

\H : the hostname

\j : the number of jobs currently managed by the shell

\l : the basename of the shell’s terminal device name

\n : newline

\t : the current time in 24-hour HH:MM:SS format

\T : the current time in 12-hour HH:MM:SS format

\@ : the current time in 12-hour am/pm format

\A : the current time in 24-hour HH:MM format

\u : the username of the current user

\w : the current working directory, with $HOME abbreviated with a tilde

\W : the basename of the current working directory, with $HOME abbreviated with a tilde

\\ : a backslash

\[ : begin a sequence of non-printing characters, which could be used to embed a terminal control sequence into the prompt

\] : end a sequence of non-printing characters

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.

1
\e[38;5;#;48;5;#m (your characters go here!) \e[00m

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.

text color table background color table

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:

1
export PS1 = "\n[\e[38;5;81;48;5;17m\$PWD/\e[00m](\e[38;5;81;48;5;17m\t\e[00m)\n\e[38;5;46m\e[00m "

(You can type the apple logo () by pressing shift + alt + k)