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 set up 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=($SERVER1, $SERVER2, $SERVER3 "anotheruser@$SERVER3")
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).

26 comments:

  1. This blog is so nice to me. I will continue to come here again and again. Visit my link as well. Good luck
    cara menggugurkan kandungan
    obat penggugur kandungan

    ReplyDelete
    Replies
    1. Hi, Great.. Tutorial is just awesome..It is really helpful for a newbie like me.. I am a regular follower of your blog. Really very informative post you shared here. Kindly keep blogging. If anyone wants to become a Front end developer learn from HTML5 CSS3 Javascript Online Training from India . or learn thru HTML5 CSS3 Javascript Online Training from India. Nowadays JavaScript has tons of job opportunities on various vertical industry. HTML5 CSS3 Javascript Online Training from India

      Delete
  2. Thank you for sharing this blog its very interesting and helpful.
    Best IT Training in Bangalore

    ReplyDelete
  3. This comment has been removed by the author.

    ReplyDelete
  4. هل تبحث عن شركة متخصصة فى خدمات التنظيف بالطائف بافضل المعدات والسوائل وثقة تمة فى العمل ودقة فى النتائج كل هذه المميزت توفرها شركة الخليج الشركة الافضل والامثل فى الخدمات المنزلية بالطائف وبما اننا الشركة الافضل والامثل بدون منافس سوف نسعى لتوفر افضل الخدمات باقل تكلفة وبقدر كبير من الاهتمام والدقة عزيزى اينما كنت فى اى منطقة ا وحى تابع لمدينة الطائف اتصل بنا وسوف نصلك فى الحال شركة الخليج للخدمات المنزلية شركة تنظيف منازل بالطائف
    شركة تنظيف فلل بالطائف
    شركة تنظيف خزانات بالطائف
    شركة تسليك مجارى بالطائف
    شركة رش مبيدات بالطائف
    شركة مكافحة نمل ابيض بالطائف
    شركة مكافحة حشرات بالطائف
    شركة عزل اسطح بالطائف
    شركة عزل خزانات بالطائف

    ReplyDelete

  5. تقدم شركة خدمات التنظيف وكشف التسربات بالرياض حيث ان الشركة لها باع طويل فى تقديم الحلول فى مجال النظافه
    وهناك بعض الخدمات منها كماتقدم الشركة كفاه خدمات التنظيف وكشف التسربات والغزل عميلانا العزيز تفضل بزياره
    شركة مكافحة الفئران بالرياض
    شركة مكافحة النمل الابيض بالرياض
    شركة كشف تسربات المياه بالرياض
    شركه تنظيف مكيفات بالرياض
    ارخص شركة تنظيف منازل بالرياض
    افضل شركة تنظيف منازل بالرياض
    شركة مكافحة حشرات بجازان
    افضل شركة تنظيف بجازان

    ReplyDelete
  6. تتميز شركتنا شركة الصياد للخدمات المنزليه علي مستوي المملكه وايضا تعطي شركتنا الضمان الكامل للعميل حتي يطمئن وايضا تكون جميع عملياة النظافه والعمليات التي يطلبه مننا العميل تكون تحت اشرافه تماما وايضا تتمتع شركتنا بافضلالمعدات والخامات التي نستعمله في مجالات التنظيف وايضا نملك احدث السيارات لنقل العفش الي اي مكان يحدده العميل لنا ونملك افضل المكينات واتجهزة البخار التي نستعمله في تنظيف المجالس بالبخار وشركتنا جاهزه في اي وقت يحدده العميل لنا وعلي اتم الاستعداد لاي عمل يئمرنا به
    شركة تنظيف بابها
    شركة تنظيف مجالس بابها
    شركة مكافحة حشرات بابها
    شركة تنظيف خزانات بابها
    شركة تسليك مجاري بابها
    شركة تسليك مجاري بخميس مشيط
    شركة تنظيف بخميس مشيط
    شركة تنظيف خزانات بخميس مشيط
    شركة تنظيف مجالس بخميس مشيط
    شركة مكافحة حشرات بخميس مشيط

    ReplyDelete
  7. cool, please guidance so that I can create a blog like yours

    ReplyDelete
  8. thank you for the information provided, we are waiting for the next info

    ReplyDelete


  9. A tank cleaning company in Dammam has modern and advanced cleaning equipment used in cleaning, washing and sterilizing water tanks in Dammam, whether upper or lower tanks, and using special detergents used to clean and wash the tank bottom, walls and surfaces.شركة تنظيف خزانات بالدمام

    Insect control company in Dammam is one of the best and largest pest control companies in the Kingdom, where it performs all tasks and works of pest control of all kinds, combating termites, cockroaches, flies, mosquitoes and mice. The insect control company in Riyadh offers high-quality insecticide spraying.شركة مكافحة الحشرات بالدمام

    ReplyDelete