Sunday, 17 September 2017

Getting most out of Linux terminal


Here are a few tips that you can incorporate in your Linux terminal to make your life easier. All these settings have to be done in ~/.bashrc file. Also note that you have to  either restart your terminal for changes to take place or just execute this command
exec bash
This basically reloads any config changes that you made in ~/.bashrc.

1. Colourise your terminal

If your terminal is all monotonic black and white consider adding/uncommenting this line in terminal

force_color_prompt=yes

2. Enable tab completion by adding following lines in .bashrc

if ! shopt -oq posix; then
  if [ -f /usr/share/bash-completion/bash_completion ]; then
    . /usr/share/bash-completion/bash_completion
  elif [ -f /etc/bash_completion ]; then
    . /etc/bash_completion
  fi
fi

3. Change colour of Linux prompt username with respect to last exit value.

#CHANGE BASH USER NAME BASED ON EXIT CODE ##
color_exit_code(){
       if  [ $? -eq 0 ]; then
            echo -e "\033[01;32m"  #green
       else
            echo -e "\033[01;31m"   #red
 fi

}
After adding this function , you have called it. Discussed later in this post.

4. Git info on terminal prompt

 Display git branch in terminal and show the status of repository by changing the branch colour name
#GIT COLORING FUNCTION ########################################################
git_set_color(){
   UNTRACKED="$( git status --short 2> /dev/null | awk '{ print $1 }'| grep "??" | wc -l )"
   MODIFIED="$( git status --short 2> /dev/null | awk '{ print $1 }'| grep "M" | wc -l )"
   DELETED="$( git status --short 2> /dev/null | awk '{ print $1 }'| grep "D" | wc -l )"
   RENAMED="$( git status --short 2> /dev/null | awk '{ print $1 }'| grep "R" | wc -l )"
   ADDED="$( git status --short 2> /dev/null | awk '{ print $1 }'| grep "A" | wc -l )"
   REMOTE="$( git status 2> /dev/null | grep "ahead"| wc -l )"
   if [[ "$UNTRACKED" -eq 0 && "$DELETED" -eq 0 ]]; then
           if [[ "$MODIFIED" -eq 0 && "$RENAMED" -eq 0 && "$ADDED" -eq 0 && "$REMOTE" -eq 0 ]]; then
     echo -e "\033[00;32m"  #green
    elif [[ "$MODIFIED" -eq 0 && "$RENAMED" -eq 0 && "$ADDED" -eq 0 ]]; then
     echo -e "\033[01;34m"   #blue
    else
     echo -e "\033[00;33m"   #yellow
    fi
   else
    echo -e "\033[00;31m"   #red
   fi
}
Now call both of these git_set_color and color_exit_code in PS1 variable
Like
if [ "$color_prompt" = yes ]; then
    PS1='${debian_chroot:+($debian_chroot)}$(color_exit_code)\u\[\033[00m\]:\[\033[01;34m\]\w$(git_set_color)$(parse_git_branch)\n\[\033[0;37m\]\$\[\033[00m\]'
else
    PS1='${debian_chroot:+($debian_chroot)}\u@\h:\w\$ '
fi

Here' s how Linux prompt will look like after changes.
Green colour show clean git repository


Yellow colour show modified files

Red colour show presence of untracked files in repository

Blue colour show commit has not been pushed to remote repository




















Changes colour of username based on exit code 





5. Create alias for useful long commands

Let's create alias for long and frequently used commands like  "sudo openconnect grid1.abc.com" , Now by just executing "vpn"  corresponding command get executed.
alias vpn="sudo openconnect grid1.abc.com"

6. Try different terminal emulator personally I prefer terminator.

Terminator provides features like splitting screen, creating tabs, also broadcast feature. Broadcast feature is really useful if you have to do same task in multiple terminal i.e if you want to  execute same set of commands by logging in multiple computers.

7. Know better alternative to navigate quickly in between directory location

For jumping in between directory location use shortcut like
cd     Jump directly to home directory
cd -   Jump to previous directory location
cd ..  Jump to parent directory

8. Enable Vi or Emacs mode in terminal. 

If you are vim command junky set this in your ~/.bashrc
set -o vi
Else by default you use emacs shortcut
set -o emacs
 which generally start which Ctrl . Here are some sample emacs mode shortcut.

C-a move cursor to (at) beginning-of-line
C-e move cursor to end-of-line
C-f move cursor forward one character
C-b move cursor backward one character
C-n move cursor to next line
C-p move cursor to previous line
C-v scroll file forward by one screenful
ESC v scroll file backward by one screenful
ESC < go to beginning-of-buffer
ESC > go to end-of-buffer
ESC f    move cursor forward one word
ESC b move cursor backward one word

Go more detail go to source .

9. Start using Screen or Tmux.

If you work on the remote server considers using multiplexer like screen or tmux. So that you work without worry about network disconnection or if you want to access running process after some time.

10. git diff vs diff-so-fancy

If you git version control you project, which you must should, you'll find diff-so-fancy a better alternative to plain git diff.

11. Ctrl + R enhanced with fzf

Bash command Ctrl+R is used to browse through earlier command used in terminal, fzf combines it which fuzzy matching. A must try.





Saturday, 16 September 2017

Saviour of Civilisation

Long before technological advancement begun, nature was in kind of perfect harmony. There were instances where single species seems to dominate the landscape for some time but over the course of time, nature used take the lead. In case of human by dint of our Intelligence we seemed to slowed this process.It may take time, nature  will ultimately restore the balance. But it may be too late and may result in slow and painful death like situation for human civilisation. In Hindu mythology all three gods  creation(Brahma), upkeep(Vishnu) and destruction(Shiva) is considered of equal importance. Destruction is not something bad, but in fact it's first step toward new construction.


Earth is facing another major form of extinction of species and this time it is entirely man made.
Earlier major extinction gave way for other suppressed species to flourish. As it is big animals and plants which suffer most in such cases. Till now there has been five major extinction , and we facing sixth extinction.

Increasing population of human has not just affected other species but have also significantly reduced the quality of human life as well. Few decades back is was easy to have a big front yard garden and lot of open spaces to roam. Now a day it only a privilege for a selected riches. Decreasing resources will further degrade lifestyle and increase stress in our life.

So what is solution for all these ? What can be done to ensure to other species of plants and animals thrives as well? Also how to ensure enough availability of resource for upcoming generation?

Some may claim solar energy, nuclear energy, population control or effective use of other technology as way of solution. But I beg to differ these way are too naive to effectively reduce burden of 7.2 billion people.

Destructive approach can be only effective way to free the earth. What such approach will be our saviour?
It may be
1. Nuclear Weapons
2. Massive asteroid Attack
3. Global Infectious Disease
4. Or a reason not yet known to mankind
 
There may be several other reasons, many of which I am unaware of. What part we can play to accelerate this process ? If possible I'll try to address this question in next blog.