Sunday, May 17, 2009

Script to synchronize VIM settings

Abstract: My scripts to backup vim setting and synchonize between Windows and Linux.


Finally I cannot bare the boring work of copying my vim settings around my labtop(with both windows and linux installed), lab pc and working server. So I write a few scripts to automatic this work for me:)


The first script is used to copy the scirpts to a certain dir on my laptop that can be share accross Windows and Linux systems. tr and sed are used to convert file format and handle some syntax difference.

#!/bin/bash
CURDIR=`pwd`
BACKDIR="/cygdrive/e/vim-setting"
if [ -d $BACKDIR ] ; then
echo "Backup to $BACKDIR"
else
mkdir "$BACKDIR"
echo "Make backup dir $BACKDIR"
fi
cd "$BACKDIR"
rm .vimrc
rm .gvimrc
rm -rf .vim
cp '/cygdrive/c/Documents and Settings/Yuanjie/_vimrc' .vimrc
tr -d '\15\32' < '/cygdrive/c/Documents and Settings/Yuanjie/_gvimrc' | sed 's/set guifont=\([^:]*\):h\([0-9]*\)/set guifont=\1\\ \2/' >.gvimrc
cp -r '/cygdrive/c/Documents and Settings/Yuanjie/vimfiles' .vim
cd $CURDIR

After files are moved to the synchronize folder, I init a git repo and push it to a host. The synchronize script on the Linux side goes like this:

#!/bin/bash
CURDIR=`pwd`
EMOUNT=0
BACKDIR="/media/e/vim-setting"
if [ ! -d "$BACKDIR" ] ; then
echo "trying to mount drive e ..."
pmount /dev/sda9
if [ "$?"=0 ] ; then
echo "drive e mounted"
EMOUNT=1
else
echo "drive e cannot be mounted"
exit
fi
fi
cd $HOME
tar cjf .vim-setting.$(date +%F).tar.bz2 .vim/ .vimrc .gvimrc
rm -rf .vim/ .vimrc .gvimrc
cp -r $BACKDIR/.vim $HOME
cp $BACKDIR/.vimrc $HOME
cp $BACKDIR/.gvimrc $HOME
if [ "$EMOUNT"=1 ] ; then
pumount /dev/sda9
if [ "$?"=0 ] ; then
echo "drive e unmounted"
else
echo "drive e cannot be unmounted"
exit
fi
fi
cd $CURDIR

The scirpt above tar the original setting files and copy the new settings from the synchronize dir. Since the drive is not always mount, a dir test is used and drive will be mounted with pmount. The synchonize scirpt on Windows is similar but simpler than this, but guifont setting in gvimrc will have to be changed back like this:

sed 's/set guifont=\([A-Za-z\\ ]*\)\\ \([0-9]*\)/set guifont=\1:h\2/'

Notice that you may have to modify this in case there's number in your font name...

No comments:

Post a Comment