Beautifying the Bash Experience

Customizing the Command Prompt

There’s an insane number of things you can do to improve your Bash experience, one of which being changing up your command prompt.

There are couple of definitions to note within this section: the environmental variables $PS1 and $PS2. $PS1 refers to the primary command prompt, the one you see every time you open up a terminal running Bash. $PS2 refers to the secondary command prompt, which is what you see when the terminal is expecting more input from you, like when you use gcalccmd.

The Problem

I found the default primary command prompt to be a bit too long; it was eating up valuable screen space. Hence, I set it to something shorter, my initials in bold green, and a dollar sign: \e[1;32mKR$ \e[m.

However, when I used the Ctrl+p or the Up Arrow key to access the previous command, it sometimes held a part of the previous line in the command line. This did not affect the next command I ran, but it still bugged me.

Stuff that didn’t work

Based on StackOverflow’s answers, I enclosed my prompt in \[ and \], to get \[\e[1;32mKR$ \e[m\], but that was even worse.

The Solution

Turns out, I didn’t read the answers correctly. The solution is to change the command prompt, the value of $PS1 by enclosing things that begin with \e in \[ and \]. These are the nonprintable portions. I had to split this up into two different sections, one for each nonprintable chunk. Hence, I was left with \[\e[1;32m\] and \[\e[m\] once I had added in those sequences. The final solution was \[\e[1;32m\]KR$ \[\e[m\].

Changing Appearance of Bash Layout

You can change the colors of how your directories, files and other things look when you list out the contents of a directory. This information is stored in an environmental variable called $LS_COLORS.

If you type

echo $LS_COLORS

you’ll see a bunch of gunk. If you want to play around before committing to anything, you can set another environmental variable to the value of $LS_COLORS, and make any changes you want to $LS_COLORS to check out the differences. These changes will go away once you kill the current terminal. If you want any changes to be permanent, just add a line to your .bashrc file.

There are a gazillion options to customizing your setup, but a basic one could read as follows:

di=1;36:*.png=1;35:ex=1;33

Let’s go through it bit by bit:

  1. Directories are bright cyan
  2. All PNG images are purple
  3. All executables are bright yellow
  4. Entries are colon-separated

My current settings for this variable are in my .bashrc file.