Keeping shell configurations in sync across multiple machines
Asked Answered
H

4

12

I'm a pretty active command line user and I have shell accounts all over the place. MacBooks, Linux desktop machines, Linux servers, Cygwin on XP, you name it.

How can I keep my shell configuration (.bashrc, .vimrc etc.) in sync across all these machines using the limited tools available across all platforms?

I have been using rsync in a pretty limited manner that involves manually copying some files over when I need them, but I want a standard way for setting up the same basic shell environment across all my machines. Tell me your shell account management strategy.

Harsh answered 20/10, 2008 at 12:30 Comment(0)
H
15

I have folder on Dropbox with global, per OS, and per machine shell configs:

$ ls ~/Dropbox/shell/bash
bashbootstrap  bashrc
bashrc-Darwin  bashrc-Darwin-laptopname  bashrc-Darwin-mininame
bashrc-Linux  bashrc-Linux-machineone  bashrc-Linux-machinetwo

bashrc is loaded on every machine, bashrc-Linux, bashrc-Darwin are loaded on their respective OSes, and several configs are specific to individual machines. (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.

I made a blog post about this here.

Harsh answered 29/7, 2011 at 16:18 Comment(0)
P
2

I've used version control for this in the past (svn, mercurial, etc...). You can set up your own server, or use a hosted one. Dropbox also works.

Patricapatrice answered 20/10, 2008 at 12:41 Comment(0)
O
0

If you have access to source control, I'd just check them in. This way you get to sync them across multiple machines and you can compare/roll-back if need be. If you don't have CVS/SVN at work there are free options available.

Osteoclast answered 20/10, 2008 at 12:42 Comment(0)
G
0

I prefer slightly different configuration files for Vim, Fish, Sway, etc. on different machines, so I made a program (filetailor) to handle this and then sync the result with Git.

filetailor is an open-source Python program that can make small changes to files using device-specific variables or using device-specific comments in the files.

For example, the following line would be commented out on every device except the one with hostname device1.

alias MYHOME='/home/dev1home/' #{filetailor device1}
Giorgi answered 14/11, 2021 at 23:33 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.