Sunday, November 15, 2015

Customising Command Line Prompt

Customising command line prompt is helpful as we can display useful information. Such as Git branch when browsing though a repository.

I have done this on a Mac, but you can do this on any Unix base OS.

To customize your terminal open your bash profile file at the following path.

vi ~/.bash_profile

Here is a sample configuration, I've added to customize my command line.

# Define some colours
RESET="\[\017\]"
RED="\[\033[31;1m\]"
GREEN="\[\033[0m\]"
BLUE="\[\033[34m\]"
YELLOW="\[\033[33;1m\]"
# Path to git-prompt.sh below is specific to a Mac. Update this to suit your environment.
git_prompt=/Applications/Xcode.app/Contents/Developer/usr/share/git-core/git-prompt.sh
# Read the file
[[ -r $git_prompt ]] && source $git_prompt
PS1="${BLUE}\u${GREEN}@\h:${RED}\W ${YELLOW}\$(__git_ps1 '(%s)')${GREEN}$ "


In the above configuration, I can see my username (\u) in blue, host name (\h) in green, current directory (\W) in red and git branch in yellow. If the current directory is not a Git repository, git branch is not displayed.

EG (in a git repository):

achintha@localhost:tmp (master)$

EG (other than a git repository):

achintha@localhost:tmp $

Here's a list of other available options you can use,

\d – Current date
\t – Current time
\h – Host name
\# – Command number
\u – User name
\W – Current working directory (ie: Desktop/)
\w – Current working directory with full path (ie: /Users/Admin/Desktop/)

No comments:

Post a Comment