Essential bash customizations: prompt, ls, aliases, and history date

Introduction

Some Linux terminals especially remote ones look lifeless. In this post I want to show in 10 min, how hugely you can improve interaction experience with those terminals by using aliases, customising command prompt and ls output, and adding history date.

Prerequistis

I am assuming you are familiar with Bash terminal and its commands and bashrc and bash_profile.

Alias

This one is top priority: define as many as aliases that saves your time in typing. There are many long commands that you type many times a day, make aliases for them like:

alias workssh="ssh myid@blah.blah.blah"
alias hgrep="history | grep"
alias current="cd '~/program1/bin/'"

Customise bash prompt

The default command prompt may look like this:

my-machine$ 

in plain white and not showing the current directory or any other information. To change that, we need to set PS1 environment variable. The coloring code can be really confusing, to save time we use this free online bash PS1 generator. In the website, drag and drop items such as date, directory, and hostname. By clicking on each item, you can set the color of them. This is what I made:

(.Get 1)

Now copy-paste the export PS1=blah blah blah into your terminal. You should immidiately see the change. Also you can paste the line into ~/.bashrc and ~/bash_profile to make it as the default style.

Color ls output

ls colors are set in the environment variable LS_COLORS. You can check if it is set in your terminal by running

echo $LS_COLORS

The format of LS_COLORS is like:

filetype1=x1;x2:filetype2=x3;x4;x5:...

where filetype1, 2,… are specific file extensions like *.jpg, *.png or the below table

filetypeexplanation
NORMALNormal (nonfilename) text
FILERegular file
DIRDirectory
LINKSymbolic link
EXECExecutable file

x1, x2,… are the codes which are picked from the table below

FG=foreground

BG=background

codebehavior
0default color
1brighter colors
4underlined text
5flashing text
30for black FG
31red FG
32green FG
33yellow (or brown) FG
34blue FG
35purple FG
36cyan FG
37white (or gray) FG
40black BG
41red BG
42green BG
43yellow (or brown) BG
44blue BG
45purple BG
46cyan BG
47white (or gray) BG

This is the LS_COLORS I have in ~/.bashrc of a remote server:

export LS_COLORS="di=4;44:*.pbs=0;33:*.txt=0;34:*.vtk=0;36:*.vti=0;36:*.cpp=0;32:*.cmake=0;31:*.stl=0;35:*.py=0;32;47:*.csv=0;37;44:$LS_COLORS"

Running ls in a folder I get:

(.Get 1)

If you don’t see any color, there is a chance that coloring is off, add this into ~/.bashrc and/or ~/.bash_profile.

alias ls="ls --color=auto"

History date

As far as I’ve seen, bash history doesn’t store time and date of previous commands. To do so, add this to your ~/.bashrc and ~/.bash_profile:

export HISTTIMEFORMAT="%d/%m/%y %T "

There has been times, I really needed the exact time that I deleted something and this helped me.

More like this

See this post that I show how you can systematically copy current path in a Shell terminal and use it somewhere else.

References

Linux manual for dir_colors

Tags ➡ Linux HPC

Subscribe

I notify you of my new posts

Latest Posts

Comments

1 comment
David 20-Jun-2023
Super nice site --- both content and style. Is it done in Hugo?