Thursday, September 2, 2010

Setting up your bash prompt for colours, aliases and other things

One of the little joys in life is to have a nice coloured bash prompt and a lot of aliases to simplify navigation in bash.

I've placed most of my commands in ~/.bashrc (which is a file that gets executed when you open a bash terminal).

To know how to colour a terminal, see this and this
Being able to create aliases to commands is one of the best things I've found out about the bash shell. You'll find a similar functionality in MS-DOS, called doskey.

These are the aliases I use in bashrc. Typing "cd .." became so annoying that the alias "f" (for "fall" to the below directory) became a handy tool.

    # User specific aliases and functions
    alias bashrc='vim ~/.bashrc;source ~/.bashrc;echo bashrc has been sourced'
    alias bashprofile='vim ~/.bash_profile;source ~/.bash_profile;echo bash_profile has been sourced'

    alias aliases="clear;cat ~/.bashrc|grep --color \"alias\""
    alias home='cd;ls;pwd'
    alias root='cd /;ls;pwd'
    alias die='halt -p'
    alias desktop='cd ~/Desktop;ls;pwd'
    alias f='cd ..;ls;pwd'
    alias ff='cd ../..;ls;pwd'
    alias fff='cd ../../..;ls;pwd'
    alias ffff='cd ../../../..;ls;pwd'
    alias fffff='cd ../../../../..;ls;pwd'
    alias ffffff='cd ../../../../../..;ls;pwd'
    alias fffffff='cd ../../../../../../..;ls;pwd'
    alias ffffffff='cd ../../../../../../../..;ls;pwd'
    alias home='cd;ls;pwd'
    alias root='cd /;ls;pwd'
    alias die='halt -p'

    alias startsql="service mysqld start"
    #leak checking for a program
    alias leakcheck="clear;rm log.txt;valgrind --log-file=\"log.txt\" --leak-check=full --show-leak-kinds=all /home/nav/NetBeansProjects/SampleProg/dist/Debug/GNU-Linux-x86/./sampleprog ;gedit log.txt"
    #adding a hibernate option which I found while using Fedora20
    alias hibernate='systemctl hibernate'
    #--- For a coloured, customised prompt
    # 0 is for non-bold colour. Replace it with 1 for bold
    # 31m is the colour
    # \u makes it show the username
    # \h makes it show the address
    # \W makes it show the directory name. Use \w for showing the full path
    #export PS1="\e[0;32m[\u@\h \W]\$ \e[m"

    export PS1="\e[1;32m[\W] \e[m"