Search This Blog

Sunday, September 25, 2016

Synchronizing vim environment accros multiple machines

Although I primarily use Sublime Text, some time ago I decided to become proficient in Vim, to improve my productivity when working gon remote machines.

Over time I've customized my vim environment. Every time I use a different machine, I need to setup the environment to my liking. While not terribly hard, the problem is that as a keep working on several remote machines and customizing my vim environment, the machines environments get out of sync. I put together this little bash script to keep things in sync across all machines I regularly work with.

#!/usr/bin/env bash
# Synchronizes this computer's vim environment to remote boxes
#
# Usage:
#
# $ ./vimsync.sh [BOXES]
#
# If BOXES is not specified, the default list will be used (see
# implementation a couple of lines below)
# BOXES is a space separated list of BOXES of the form
# [user1@]BOXNAME1 [user2@]BOXNAME2 ...

## boxes to sync to
if [[ $# -eq 0 ]]; then
  declare -a boxes=($RADGPU1 $RADGPU2 $DEVG1 $DEVG3 $DEVG4 "radshazam@$MACMINI")
else
  declare -a boxes=($@)
fi

echo $boxes

## files to synchronize
declare -a files=(".vimrc" ".vim/colors/cheerfully_dark.vim" ".gitconfig")

## directories to create
declare -a directories=("~/.vim/colors")

## sexyness
BALCK='\033[0;30m'
RED='\033[0;31m'
GREEN='\033[0;32m'
ORANGE='\033[0;33m'
BLUE='\033[0;34m'
PURPLE='\033[0;35m'
CYAN='\033[0;36m'
GRAY='\033[0;37m'
NC='\033[0m' # No Color


## now loop through the above arrays
for box in "${boxes[@]}"
do

  # need -e to allow backslash escapes
  echo -e "${CYAN}[Syncing to ${box}]${NC}"

  # create required directories
  for dir in "${directories[@]}"
  do
    cmd="ssh ${box} test -d ${dir} || mkdir -p ${dir}"
    echo -e "> ${GRAY}${cmd}${NC}"
    $cmd
  done

  # clone Vundle.vim if necessary
  cmd="ssh ${box} test -d ~/.vim/bundle/Vundle.vim || git clone https://github.com/VundleVim/Vundle.vim.git ~/.vim/bundle/Vundle.vim"
  echo -e "> ${GRAY}${cmd}${NC}"
  $cmd

  # sync files
  for file in "${files[@]}"
  do
    #cmd="scp $HOME/$file ${box}:/home/jorge.herrera/$file"
    cmd="rsync -avzhe ssh $HOME/$file ${box}:~/$file"
    echo -e "> ${GRAY}${cmd}${NC}"
    $cmd
    #echo "done"
  done

  # need -e to allow backslash escapes
  echo -e "${GREEN}[Done with ${box}]${NC}"

done

The env variables $SERVER1, $SERVER2, $SERVER3 are defined elsewhere (e.g. in my ~/.bash_profile).

No comments:

Post a Comment