Bash prompt customizations

Some paths can be really long and annoying. Here's how you fix it.
NOTE: I am using 14.04. Your mileage may vary.

Reduce the size of the prompt

This simple trick checks if the length of the current directory path is greater than 30. In that case, it breaks it up into two chunks -- first 12 letters and the last 15.

__update_prompt1()                                                                                                                                                                     
{                                                                                                                                                                                     
   DIR=`pwd | sed -e "s!$HOME!~!"`                                                                                                                                                   
   if [ ${#DIR} -gt 30 ]; then                                                                                                                                                       
     CurDir=${DIR:0:12}....${DIR:${#DIR}-15}                                                                                                                                       
   else                                                                                                                                                                              
     CurDir=$DIR                                                                                                                                                                   
   fi                                                                                                                                                                                
} 

Before:
[user@hostname:/local/mnt/workspace/somefolder1/foldera/test]$
After:
[user@hostname:/local/mnt/w....a/test/]$


Personally, I like the one below. It replaces the folder names with a single letter.


__update_prompt2()                                                                                                                                                                    
{                                                                                                                                                                                     
    CurDir=`pwd | sed -e "s!$HOME!~!" | sed -re "s!([^/])[^/]+/!\1/!g"`                                                                                                               
} 

Before:
[user@hostname:/local/mnt/workspace/somefolder1/foldera/test]$
After:
[user@hostname:/l/m/w/s/f/test]$

All we need to do is update the PROMPT_COMMAND and PS1


PROMPT_COMMAND=__update_prompt2                                                                                                                                                       
export PROMPT_COMMAND=${PROMPT_COMMAND}                                                                                                                                                                                                                           
PS1='[\u@\h:${CurDir}]\$ '                                                                                                                                     
export PS1 

There is one other thing I like to do -- move the cursor to the newline
PS1='[\u@\h:${CurDir}]\n\$ '

The prompt looks like,

[user@hostname:/l/m/w/s/f/test]
$ ls


Adding git branch information to prompt along with git completion


First things first, download the git-completion.bash and git-prompt.sh files from https://github.com/git/git/tree/master/contrib/completion

Add these do the .bashrc


source ~/.git-completion.bash
source ~/.git-prompt.sh 

And finally, update the PS1

PS1='[\u@\h:${CurDir}]$(__git_ps1 " (%s)")\n\$ '

When the folder is a git repository it will automatically show the current git branch.

[user@hostname:/l/m/w/s/f/myrepo] (master)
$ ls -a
. .. .git hello 

You get git completion for free as well.

[user@hostname:/l/m/w/s/f/myrepo] (master)
$ git che [TAB]
check-mailmap   checkout        cherry          cherry-pick


Preserving the history across terminals



This can be done in three simple steps
  1. history -a : Append the new history lines (history lines entered since the beginning of the current Bash session) to the history file
  2. history -c : Clear the history list. This may be combined with the other options to replace the history list completely
  3. history -r : Read the history file and append its contents to the history list

Let's do this part of the PROMPT_COMMAND

PROMPT_COMMAND=__update_prompt2
export PROMPT_COMMAND="history -a; history -c; history -r; ${PROMPT_COMMAND}"

That's it!