This is one of the best tips if you are developer working with git on the terminal. If you are inside a directory or project managed by git, this will append the working branch your code is on to the prompt!

function parse_git_branch {
git branch --no-color 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/\ \[\1\]/'
}

BOLD=$(tput bold)
RESET=$(tput sgr0)

export PS1="[\u@\h \W]\[$BOLD\]\$(parse_git_branch)\[$RESET\] $ "

The key here is, of course, the bash function created called: parse_git_branch(). Then we are modifying the default bash prompt and including the result via: $(parse_git_branch)

I do like to throw in a little color for the branch. So my actual prompt is:

export PS1="[\u@\h \W]\e[0;35m\[$BOLD\]\$(parse_git_branch)\[$RESET\]\e\[m $ "
mike: Mike is an opinionated computer programmer living in Michigan with his wife and three kids.

Discussion

No comments yet, be the first.

Leave a Comment