Archive for the 'Development' Category

Bash Autocompletion for Git

The default installation of Git on OS X doesn’t include support for autocompletion. I know it’s possible, because whatever package I used to install Git on my Windows work computer has it, but I’ve never taken the time to figure it out. Well, here’s how it works. Note that these instructions were about 10x longer, until I decided to read the contents of the bash-completion.bash script… the installation is a lot easier than I was making it.

First, you need to copy the bash-completion script somewhere appropriate.

cd /usr/local/git/contrib/completion/
cp git-completion.bash ~/.git-completion.sh

Now add the following line to your ~/.bash_profile

source ~/.git-completion.sh

Next time you login, autocompletion in Git should work just fine. If you want to take it one step further, here’s how you add the current branch to your command line. The bash-completion script tells you to change your PS1 to the following.

PS1='[\u@\h \W$(__git_ps1 " (%s)")]\$ '

Which is cool, but I already had my PS1 set up, complete with colors and shit. Here’s what I ended up doing.

GIT_PS1_SHOWDIRTYSTATE=1
GIT_PS1_SHOWSTASHSTATE=1
GIT_PS1_SHOWUNTRACKEDFILES=1
export GIT_PS1_SHOWDIRTYSTATE
export GIT_PS1_SHOWSTASHSTATE
export GIT_PS1_SHOWUNTRACKEDFILES

PS1='[\e[1;32m]$(__git_ps1 " (%s)")[\e[m]' PS1="[\e[1;36m]\h:\W[\e[m]$PS1[\e[1;36m]> [\e[m]" export PS1

What a mess! I’m never going to be able to remember what I did here unless I write it down here.

First off, the GIT_PS1_ variables add an indicator to your command line letting you know that you’re in a dirty working directory, you have something stashed or there are untracked files. Handy.

Second, anything between [\e[1;32m] and [\e[m] is given a color. You can read more about it here.

Finally, the function $(__git_ps1 " (%s)") will replace %s with the current branch (plus a flag if you’ve set those variables I described earlier) if you are currently in a directory under Git source control.

The final result:

Terminal%20%E2%80%94%20bash%20%E2%80%94%2080%C3%9748

I’ll be updated my recent post on my .bash_profile to reflect these changes.

My .bash_profile

In case anyone is interested. The meat of it is a bunch of two-letter aliases for common Git commands. My favorite part, though, is the shortcut to SSH into the iMac. My iMac has an equivalent alias to SSH into my MacBook. Makes it super easy to login and push whatever I was working on back to the server, if I’d forgotten to after switching computers.

Oh, and this isn’t in my profile, but just learned this trick last week and which I had known about it years ago. In OS X, ‘open ./’ opens the current directory in Finder.

!/bin/bash

This is the preferred file for running init commands

This file will be run on any login

echo echo "######################################" echo "# #" echo "# Welcome to Jim's Macbook #" echo "# #" echo "######################################" echo

setting up bash completion

source ~/.git-completion.sh GIT_PS1_SHOWDIRTYSTATE=1 GIT_PS1_SHOWSTASHSTATE=1 GIT_PS1_SHOWUNTRACKEDFILES=1 export GIT_PS1_SHOWDIRTYSTATE export GIT_PS1_SHOWSTASHSTATE export GIT_PS1_SHOWUNTRACKEDFILES

PATH=$PATH:/Users/jkubicek/bin export PATH

PS1='[\e[1;32m]$(__git_ps1 " (%s)")[\e[m]' PS1="[\e[1;36m]\h:\W[\e[m]$PS1[\e[1;36m]> [\e[m]" export PS1

alias ls='ls -G' alias imac='ssh jimk@imac.local' alias gc='git commit -m' alias ga='git add .' alias gs='git status' alias grso='git remote show origin' alias gl='git log --oneline --graph'

Updated: I added a bunch of stuff related to git autocomplete, in light of my recent post.