Quick link: online tool for generating colour settings

A quick post showing how to set prompt and ls colours in Linux and OSX.

The file that is edited depends on the operating system: in Linux, it is ~/.bashrc, in OSX it is ~/.bash_profile

In OSX: ~/.bash_profile

In OSX the file that controls you terminal colours is ~/.bash_profile. Insert the following lines into ~/.bash_profile:

# colours of prompt
export PS1='\[\e[1;38;5;010m\]\u \w\[\e[0;38;5;45m\] \$\[\e[0m\]  '
# colours of ls command 
export CLICOLOR=1
export LSCOLORS=GxBxhxFxhxhxhxhxhxGxGx

NOTE: after adding these lines to the ~/.bash_profile, in order for the changes to be applied either open a new terminal or use the command:

source ~/.bash_profile

An explanation of what these lines mean is below; the figure below shows the colourscheme it creates:

prompt and ls

Prompt: Computer name and current directory/path in bold green, $ in turquoise blue.

ls: file names in grey, and folders in bold cyan.

In Linux: ~/.bashrc

In Linux the file that controls you terminal colours is ~/.bashrc (note the difference to OSX - don't know why OSX could not have just used the same).

Also, note that it is LS_COLORS instead of LSCOLORS, and that the encoding scheme is different. Two great resources are this link and this online tool.

# colours of prompt
export PS1='\[\e[1;38;5;010m\]\u \w\[\e[0;38;5;45m\] \$\[\e[0m\]  '
# colours of ls command 
export CLICOLOR=1
export LS_COLORS="di=1;36:ln=1;31:so=37:pi=1;35:ex=37:bd=37:cd=37:su=37:sg=37:tw=1;36:ow=1;36"

NOTE: after adding these lines to the ~/.bashrc, in order for the changes to be applied either open a new terminal or use the command:

source ~/.bashrc

Further Note: this seems to be a bit tempermental on KDE.

I did this but I don't get ls colours...

Sometimes I encountered the problem that I modified the ~/.bashrc file but ls still did not produce any colours. One solution (courtesy of this post) is to add the following to your ~/.bashrc:
# force colours
alias ls='ls --color=auto'

or

# force colours
alias ls='ls --color=always'

Short or long prompt?

Usually it is better (for space reasons) to only show the current directory in the prompt:

ps1short





But often I want to have my prompt showing the full path:

pslong





By default, I use the short version, but to toggle between then I created two commands. To switch to the long version:

# . PS1LONG will show long version. NB: Remember the "."
. ps1long

And to switch back to the short version:

# . PS1SHORT will show short version. NB: Remember the "."
. ps1short

Of course, you can make the commands whatever you want; ps1 just reminds me it is for the prompt.

To create these shortcuts, enter the following into a terminal, which willl create the folder ~/build/prompt/ and the two files ps1long and ps1short, set them to executable, and append a line to the ~/.bash_profile to include them in PATH (so that the commands can be found):

#create and move to ~/build/prompt dir
currentdir=$(pwd)
mkdir -p ~/build
mkdir -p ~/build/prompt; cd ~/build/prompt
 
echo $'#!/bin/bash' > ps1short
echo "PS1='\[\e[1;38;5;010m\]\u \W\[\e[0;38;5;45m\] \$\[\e[0m\] '" >> ps1short
chmod 0755 ps1short
 
echo $'#!/bin/bash' > ps1long
echo "PS1='\[\e[1;38;5;010m\]\u \w\[\e[0;38;5;45m\] \$\[\e[0m\] '" >> ps1long
chmod 0755 ps1long
 
#move back
cd "$currentdir"
 
#not done yet!

Not finished yet! Still need to update ~/.bash_profile or ~/.bashrc.

For OSX:

echo $'export PATH=~/build/prompt:$PATH'  >> ~/.bash_profile  
 
#need these aliases
echo "alias ps1short='. ps1short'" >> ~/.bash_profile
echo "alias ps1long='. ps1long'" >> ~/.bash_profile
 
#done

You may want to change ~/build/prompt to the long version,

e.g. /Users/USERNAME/build/prompt.

For Linux:

echo $'export PATH=~/build/prompt:$PATH'  >> ~/.bashrc  
 
#need these aliases
echo "alias ps1short='. ps1short'" >> ~/.bashrc
echo "alias ps1long='. ps1long'" >> ~/.bashrc
 
#done

Credits:

This link showed me hwo to force echo to send special characters to a file. This link reminded me that . is an alias for source

I tried to find a method that did not need to use the ., but was unable to find such a solution.

Explanation of colour choices

The above lines look incomprehensible, but that is just because you don't yet know the 'key' to the two different systems. Below is a brief explanation (actually, just a couple of links that will explain it).

Explaining prompt colours (PS1)

This section explains the various parts of the command:

export PS1='\[\e[1;38;5;010m\]\u \w\[\e[0;38;5;45m\] \$\[\e[0m\]  '

This awesome Stackoverflow post showed me how to set the colours.

Breaking it down,

  • export PS1=' ... ' says that whatever is the quotes is the new settings for the prompt colours
  • \[\e[1;38;5;010m\]:
    • \[\e[: start of an escape sequence
    • 1;: text will be bold
    • 38;5;: set the foreground colour
    • 010m\]: green (see the colour table in the Stackoverflow post)
  • \u : username, followed by a space
  • \w: current directory (long version); short version is \W
  • \[\e[0;38;5;45m\]:
    • \[\e[: start of an escape sequence
    • 0;: not bold (just normal)
    • 38;5;: set the foreground colour
    • 45m\]: turquoise blue (see the colour table in the Stackoverflow post)
  • $: space, followed by dollar sign
  • \[\e[0m\] : reset colours/boldness, followed by a space

Note: the escape sequence \e can also be encoded by \\033.

Explaining LSCOLORS

This great link got me started, and from the manual for ls:

man ls
...
#scroll down until ENVIRONMENT - LSCOLORS
...
 
1. directory
2. symbolic link
3. socket
4. pipe
5. executable
6. block special
7. character special
8. executable with setuid bit set
9. executable with setgid bit set
10. directory writable to others, with sticky bit
11. directory writable to others, without sticky
and

colour codes:

##colour code
# a	Black
# b	Red
# c	Green
# d	Brown
# e	Blue
# f	Magenta
# g	Cyan
# h	Light grey
# A	Bold black, usually shows up as dark grey
# B	Bold red
# C	Bold green
# D	Bold brown, usually shows up as yellow
# E	Bold blue
# F	Bold magenta
# G	Bold cyan
# H	Bold light grey; looks like bright white
# x	Default foreground or background

So my setting

export LSCOLORS=GxBxhxDxhxhxhxhxhxGxGx

above means:

##(All backgrounds are default, i.e. black)
# G - directory foreground - Bold cyan
# x - directory background - Default
# B - symbolic link foreground - Bold red
# x - symbolic link background - Default
# h - socket foreground - light grey
# x - socket background - Default
# D - pipe foreground Bold brown/yellow
# x - pipe background - Default
# h - executable foreground - light grey
# x - executable background - Default
# h - block foreground - light grey
# x - block background - Default
# h - character foreground - light grey
# x - character background - Default
# h - executable foreground - light grey
# x - executable background - Default
# h - executable foreground - light grey
# x - executable background - Default
# G - directory foreground - Bold cyan
# x - directory background - Default
# G - directory foreground - Bold cyan
# x - directory background - Default

Even though there are essentially three types of directory (including the "sticky" variation), I could not be bothered to set different colours. Also, I think it looks les pretty. So, I just set all directories to Bold Cyan.

Online tools

Check out this online .bashrc generator for a way to build your own PS1 colourscheme.

Check out this online tool which provides a way to build your own LS_COLORS colourscheme.

Other good links

All these links helped me at some point to create the theme above.

This link has more details (and more advanced settings) for Linux.

Another link for OSX.

Yet another link for OSX.

This link explains "sticky" directories, in case you want to set the colours differently for sticky directories. Although it is possible to combine the PATH exports into a single line, I prefer to have them on separate lines.

~/.bash_profile

Here is my ~/.bash_profile, which sets my paths for LaTeX, etc, and sets the prompt colours. It is pretty basic in comparison to others that are on the web.

 
#tmux path: 
export PATH=/Users/dean/build/tmux-2.1:$PATH

#local bin path: 
export PATH=/usr/local/bin:$PATH

#LaTeX path: 
export PATH=/usr/local/texlive/2016/bin/x86_64-darwin:$PATH

#gitlatex path: 
export PATH=/Users/dean/build/gitlatexdiff:$PATH

#prompt path
export PATH=/Users/dean/build/prompt:$PATH

#need this for R
export LANG=en_US.UTF-8

#need this line - without it Python's matplotlib dies
export LC_ALL=en_US.UTF-8


#prompt colours
export PS1='\[\e[1;38;5;010m\]\u \w\[\e[0;38;5;45m\] \$\[\e[0m\] '

#ls colours
export CLICOLOR=1
export LSCOLORS=GxBxhxFxhxhxhxhxhxGxGx

Shortcuts for short/long prompt

Create the folder ~/build/prompt:

mkdir -p ~/build/prompt/

And in that folder create the file ~/build/prompt/ps1short containing:

#!/bin/bash
PS1='\[\e[1;38;5;010m\]\u \W\[\e[0;38;5;45m\] $\[\e[0m\] '

and the file ~/build/prompt/ps1long containing:

#!/bin/bash
PS1='\[\e[1;38;5;010m\]\u \w\[\e[0;38;5;45m\] $\[\e[0m\] '

(The only change between them is that \W (ps1short) changes to \w (ps1long).

Next, make them executable. In a terminal:

chmod 755 ~/build/prompt/ps1short
chmod 755 ~/build/prompt/ps1long

Finally, add the following two lines to your ~/bash_profile (OSX) or ~/bashrc (Linux):

#prompt path
export PATH=~/build/prompt:$PATH

alias ps1short='. ps1short'
alias ps1long='. ps1long'

Save, and open a new terminal. Now you can switch between short/long path by using the commands ps1short or ps1long.

To increase key repeat speed

Check out this article for OSX.