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.





No comments:

Post a Comment