Creating shortcut commands in terminal using .bashrc file

Hi guys I would like to share about .bashrc file, I think you might already have come across this file at your home directory on any linux system, I am using Linux Mint 12 currently whatever I explain here will hold good for almost all linux distributors.

What is .bashrc ?
This is a hidden text  file in your home directory. The ~/.bashrc file determines the behavior of interactive shells. A good look at this file can lead to a better understanding of Bash. This file will be run as soon as you launch your shell.

                 Now you might have got little idea, for now just remember that .bashrc would control your terminal behavior. so you might think what so special about this file? actually this is a quit interesting file you can play around and give customize your bash a better way.

Creating shortcuts:
              .bashrc can be used to customize commands the way you like. 
  •    Every time you do 
             $~ sudo apt-get install "some_package"
            so its always boring to write sudo apt-get install every time : ( so why not create shortcut for it???
            how about just 
            $~ get "some_package"    
            ?/???? isn't its cool?? and now the question is  how to create short cuts??

          ITS VERY EASY!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
        just add the below line in your .bashrc file,
         $~ vi ~/.bashrc
         and add following lines
       #-------------------------------------------------------------------------------------------
       alias get='sudo apt-get install'    
      #-------------------------------------------------------------------------------------------

   Isn't it simple? so you can customize as many commands as you like, here are some of my short cuts i prefer to use

   alias cdd='cd ..'                                    #go one folder up
   alias cddd='cd ../..'                              # go two folders up
   alias cdddd='cd  ../../..'                       #  go three folders up
   alias cdb='cd -'                                  #  go to previous folder
   alias  get='sudo apt-get install'            # sudo apt-get install is boring !!!
  
If you want more operations in a single command then the above trick doesnt work, say if you want to cd and ls in a single command then its not possible using alias..... but dnt panic we have one more trick in here...
its fuctions!!!!!!

function cdl { cd $1; ls }                      #   $1 is the folder name passed to cdl command

the above line is just like our c functions, fuction keyword followed by name and operations in the braces.
so you can have full control over the commands you use.
Below are somemore customized shortcuts from my .bashrc file.


#--------------------------------------------------------------------------------------------------

#the following lines are added by sagarsakre@gmail.com
#cdd to go folder up                                      
    alias cdd='cd ..'   #I have patent on this :P
    alias get='sudo apt-get install'   # Typing sudo apt-get install every time is irritating :P
  alias s='sudo ` history | tail -2 | head -1 | cut -b8-`'     #type s and previous command will be executed with sudo appended and its working now :)
   alias ge='gedit $1'
    function cdl { cd $1; ls;}            #change directory and do ls
    alias cdb='cd $OLDPWD'                #go back to previous folder
    alias cd..='cd ..'                    #go up
        alias motd='fortune | cowsay -f tux' #message of the day, apt-get fortune if its not working

#make clean and then make
function makec { make clean; make; }
#make and send report to the mail XXXXXXX@gmail.com and give real time notification.
#Note: You need to setup mailx for using this feature, you can follow instructions @this link to set it up
#courtesy: idea by harshendra avabratha
function makes { make clean
make
if [ $? -eq 0 ]; then
notify-send "make successfull"
echo "build success" | mailx -s buil_message XXXXXXXXX@gmail.com
else notify-send "make fail"
echo "build failed" | mailx -s build_message XXXXXXXXX@gmail.com
fi
}

#make and send message along with the build results printed on the console and realtime notification as well
#courtesy: idea by harshendra avabratha

function make_report { make clean
                                make &> make.log       #send both STDOUT and STDERR to make.log
                                if [ $? -eq 0 ]; then
                                notify-send "make successfull"
                                cat make.log | mailx -s buil_success sagarsakre@gmail.com
                                else notify-send "make fail"
                                cat make.log | mailx -s build_failure sagarsakre@gmail.com
                                fi
                                }

#cutomization ends here...:)

#-------------------------------------------------------------------------------------------------------------


Even you can use shortcuts like ctrl+n, ctrl+some_key, just like the one you use ctrl+l to clear the window.

#--------------------------------------------------------------------------------------------------------
bind '"\e[24~":"cd .. ; ls\n"'   # This will do `cd ..` and `ls` when you press key
bind '"\C-N":"cd .. \n"'           # This will do `cd ..` when you press Ctrl+N 
#---------------------------------------------------------------------------------------------------------

You can change the actions according to your needs, read more about key binding here


enjoy .bashrc!!!!!!!!!!!!!11 enjoy shortcuts !!!!!!!!!!!! :)
-sagar sakre



Comments

Popular posts from this blog

Makefile Tutorial

Turbo C for 64 bit windows 7