Search
⌘K
    to navigateEnterto select Escto close

    Configure Command Prompt

    These days we use git in most of our projects. If we are using git then it's a good idea to know which branch we are working on. We can check what our current branch is but it would be much nicer if the command prompt displays the current branch automatically.

    Second advantage is that when someone gets an error then usually they send a screenshot of the error. If the screenshot of the terminal,by default, display the directory and the branch name then that make debugging so much easier.

    What I shell I'm using

    Check this answer to find what shell you are using.

    Custom Command Prompt for bash users

    First check if you are using ~/.bash_profile or ~/.bashrc. You can find out if these files exist or not by executing following command.

    1$ cat ~/.bashrc
    2$ cat ~/.bash_profile
    3$ cat ~/.zshrc
    

    If you're using zsh, please install oh-my-zsh. Oh My Zsh is a framework to manage ZSH configuration and it by default comes with themes and helper functions.

    If you're using bash, then open ~/.bashrc or ~/.bash_profile whichever of these file exists. Then at the bottom of that file paste the following content. Save the file and then open a new terminal.

    1txtblk='\e[0;30m' # Black - Regular
    2txtred='\e[0;31m' # Red
    3txtgrn='\e[0;32m' # Green
    4txtylw='\e[0;33m' # Yellow
    5txtblu='\e[0;34m' # Blue
    6txtpur='\e[0;35m' # Purple
    7txtcyn='\e[0;36m' # Cyan
    8txtwht='\e[0;37m' # White
    9
    10bldblk='\e[1;30m' # Black - Bold
    11bldred='\e[1;31m' # Red
    12bldgrn='\e[1;32m' # Green
    13bldylw='\e[1;33m' # Yellow
    14bldblu='\e[1;34m' # Blue
    15bldpur='\e[1;35m' # Purple
    16bldcyn='\e[1;36m' # Cyan
    17bldwht='\e[1;37m' # White
    18
    19unkblk='\e[4;30m' # Black - Underline
    20undred='\e[4;31m' # Red
    21undgrn='\e[4;32m' # Green
    22undylw='\e[4;33m' # Yellow
    23undblu='\e[4;34m' # Blue
    24undpur='\e[4;35m' # Purple
    25undcyn='\e[4;36m' # Cyan
    26undwht='\e[4;37m' # White
    27
    28bakblk='\e[40m'   # Black - Background
    29bakred='\e[41m'   # Red
    30badgrn='\e[42m'   # Green
    31bakylw='\e[43m'   # Yellow
    32bakblu='\e[44m'   # Blue
    33bakpur='\e[45m'   # Purple
    34bakcyn='\e[46m'   # Cyan
    35bakwht='\e[47m'   # White
    36
    37txtrst='\e[0m'    # Text Reset
    38
    39a="\n$txtwht\u$txtblu \w $txtpur"
    40e="$\[\033[00m\] "
    41
    42function parse_git_url {
    43  git config --get remote.origin.url | sed 's/\.git$//;s/\(.*[:/]\)\([^/]*\)\/\([^/]*\)\(\.git\)\{0,1\}$/(\2\/\3)/'
    44}
    45
    46function parse_git_branch {
    47  git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/\1/'
    48}
    49
    50function custom_git_prompt {
    51  branch=$(git branch 2> /dev/null) || return
    52  echo "$(parse_git_branch)"
    53}
    54
    55PS1="$a\$(custom_git_prompt)\n$e"
    
    Previous
    Next