Skip to content

Instantly share code, notes, and snippets.

@sarim
Last active February 5, 2020 20:01
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sarim/6078891 to your computer and use it in GitHub Desktop.
Save sarim/6078891 to your computer and use it in GitHub Desktop.
Gittu Smart Path Prompt for Bash

##What is it ? Many of us like to see pwd in bash prompt. However if we travel too deep inside directory trees, the path becomes very lengthy and almost eats all the space. So its good to only show last part of the path. That what this script does.

Before:

Before

After:

After

##Installation

  1. Just put the gittu-path-prompt.sh file to some place. Ex: we place it in ~/.gittu-path-prompt.sh

  2. Now add these line in .bash_profile or .profile or .bashrc (according to your OS)

     source ~/.gittu-path-prompt.sh
    
  3. Now add the \$(__gittu_path) inside your PS1 and you are done.

##Customizing

  • You can edit MAXPATHLEN to set how many chars to show
  • You can edit EXTENDSYMBOL to set change the default "..." before chopping the path.

If all these words seems alien to you, just google about "customizing bash prompt"

Happy bash-ing.

__gittu_path ()
{
MAXPATHLEN=20
EXTENDSYMBOL=...
HOMEDIR=$(echo $HOME | sed 's/\//\\\//g')
PWDPATH=`pwd | sed "s/^${HOMEDIR}/~/"`
PATHLEN=${#PWDPATH}
if [ "$PATHLEN" -gt $MAXPATHLEN ] ; then
STARTPOS=$((PATHLEN - MAXPATHLEN))
PWDPATH=$EXTENDSYMBOL${PWDPATH:$STARTPOS:$MAXPATHLEN}
fi
echo $PWDPATH
}
@amanstein
Copy link

Thanks a lot for this awesome work! Masnun helped me finding it :-)

@MoleOrbitalHybridAnalyst

Thanks! I did not know PS1 can have function calls.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment