Tuesday, January 19, 2016

git branch in shell prompt

It's probably reasonably well known, but adding something like

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

export PS1="\u@\h \W\[\033[32m\]\$(parse_git_branch)\[\033[00m\] $ "

to your .bashrc will show you the git branch you're on, if any, for the current folder. 

That's actually wordier than I prefer my shell prompt to be: \u@\h gave me my current user and computer name... I use sudo etc sparingly enough that the former is pointless, and the hostname isn't neccesary because on this machine only I use an emoji instead of the traditional "$" at the end. (It was slightly tricky getting an entry from the emoji keyboard (on OSX, that's ctrl-cmd-space) into emacs where I happened to be editing the file... I had to jump out to the prompt, do a 
cat > ~/temp
type the emoji, then insert (ctrl-x i) that file into the .bashrc as I edited it in emacs.

I like having a goofy emoji in my prompt, it visually sets off where the current folder info stops and where the command begins. And it's fun.

No comments:

Post a Comment