I live in the command line across many Unix-like platforms and networks. I have two Mac laptops, two Linux workstations, shell logins scattered across the multiverse, and even a couple Windows VMs to boot. I’ve tried versioning my shell configuration files, but that requires me to check in/check out my changes across all environments. This turned out to be less than ideal. More manual effort, more hassle.
Enter Dropbox that syncs the files across multiple machines seamlessly and even gives you a bit of revision history. I concocted a system on top of Dropbox that allows me to have global, per OS, and per machine shell configs. For example, here are all the Bash config files I keep in sync via Dropbox (machine names changed to protect the innocent):
$ ls ~/Dropbox/shell/bash
bashbootstrap bashrc
bashrc-Darwin bashrc-Darwin-laptopname bashrc-Darwin-mininame
bashrc-Linux bashrc-Linux-machineone bashrc-Linux-machinetwo
Let’s ignore the bashbootstrap
file for the moment. You will notice that we have a globally-applied config file, bashrc
, two OS specific config files, bashrc-Linux
, bashrc-Darwin
, and several machine specific ones. (By the way, Darwin is the name of OS X’s BSD-like kernel.)
What ties it all together is the bashbootstrap
file. It loads each applicable config file in order of increasing specificity, this allows per OS and per machine overrides to have higher precedence. Additionally, we silently skip missing config files; you need not create empty config files for each of your machines to keep the script happy.
On a new machine, after installing Dropbox on ~/Dropbox
, I move away the default .bashrc
and just symlink the bootstrap file in its place instead:
$ mv ~/.bashrc ~/.bashrc.bak
$ ln -s ~/Dropbox/shell/bash/bashbootstrap ~/.bashrc
Oh, and here are the contents of the bashbootstrap
file:
if [ -z "$PS1" ]; then
return
fi
dropboxshelldir=~/Dropbox/shell
dropboxdir=$dropboxshelldir/bash
masterbashrc=$dropboxdir/bashrc
osbashrc=$masterbashrc-`uname`
localbashrc=$osbashrc-`hostname | cut -d. -f1`
echo -n "Applicable shell configs: "
for bashfile in "$masterbashrc" "$osbashrc" "$localbashrc"; do
if [ -r $bashfile ]; then
. $bashfile
echo -n "`basename $bashfile` "
fi
done
echo
# Set convenience aliases
myed=${VISUAL:-${EDITOR:-vim}}
alias editbashrc="$myed $masterbashrc"
alias editosbashrc="$myed $osbashrc"
alias editlocalbashrc="$myed $localbashrc"
One final note, this script also provides three convenience aliases for editing your Bash config files without having to remember where they are stored.
editbashrc
: Edit the global config file.editosbashrc
: Edit the OS-specific config file.editlocalbashrc
: Edit the machine-specific config file.
I only tested this on Bash, but it could work on other Bash like shells. But, as they say, your mileage may vary.
Did I mention that if you sign up to Dropbox with my referral we both get 250 megs of additional storage? Neat!