Efficient Ways to Sync and Manage .vimrc Across Multiple SSH Hosts


2 views

As developers who frequently SSH into multiple servers, we all face the frustration of missing our carefully crafted Vim configurations. Whether it's missing key mappings, syntax highlighting, or plugin setups, working without your personalized .vimrc feels like coding with one hand tied behind your back.

The simplest approach is to keep a master copy on your local machine and transfer it when needed:

scp ~/.vimrc user@remote-host:~/

For regular access to the same servers, you can automate this with a shell function:

function vimsync() {
    for host in "$@"; do
        scp ~/.vimrc $host:~/ &&
        ssh $host "mv ~/.vimrc ~/.vimrc.bak 2>/dev/null; mv ~/vimrc ~/.vimrc"
    done
}

Maintain your Vim configuration in a Git repository:

# On new hosts:
git clone https://github.com/yourname/vim-config.git ~/.vim
ln -s ~/.vim/.vimrc ~/.vimrc

Update all machines simultaneously with:

function vimupdate() {
    cd ~/.vim && git pull
}

Combine SSH configuration with rsync for automatic syncing:

# In ~/.ssh/config
Host *
    ControlMaster auto
    ControlPath ~/.ssh/%r@%h:%p
    ControlPersist 10m

# Sync script
vim_sync() {
    rsync -az --no-perms ~/.vimrc $1:~/
}

Create a smart .vimrc that works locally and remotely:

" Check if we're in an SSH session
if exists('$SSH_CONNECTION')
    " Load minimal settings for remote
    source ~/.vim/remote.vim
else
    " Load full local configuration
    source ~/.vim/local.vim
endif

For teams managing multiple servers, consider tools like:

  • Ansible playbooks for Vim configuration
  • Chef/Puppet recipes
  • Docker containers with pre-configured Vim
# Sample Ansible task
- name: Deploy vimrc
  copy:
    src: ~/.vimrc
    dest: /home/{{ ansible_user }}/.vimrc
    owner: "{{ ansible_user }}"
    group: "{{ ansible_user }}"
    mode: '0644'

When syncing complex Vim configurations:

  • Minimize plugin usage on remote servers
  • Use lazy-loading for plugins
  • Consider compiled configurations (--with-features)

Every developer who frequently SSHes into multiple servers knows the frustration: you open vim only to find none of your carefully crafted configurations are available. The default vim settings feel like going back to the stone age after you've customized your perfect editing environment.

The most straightforward approach is using rsync to keep your configurations updated across machines:

rsync -avz ~/.vimrc user@remote-host:~/
rsync -avz ~/.vim/ user@remote-host:~/.vim/

You can automate this with a simple bash function in your .bashrc:

function vim-sync() {
    rsync -avz ~/.vimrc $1:~/
    rsync -avz ~/.vim/ $1:~/.vim/
}

Store your vim configuration in a Git repository and clone it on remote machines:

# On your local machine:
cd ~ && git init
git add .vimrc .vim/
git commit -m "Initial vim configuration"

# On remote machine:
git clone your-repo-url ~/vim-config
ln -s ~/vim-config/.vimrc ~/.vimrc
ln -s ~/vim-config/.vim ~/.vim

Add this to your ~/.ssh/config for automatic file transfer:

Host *
    PermitLocalCommand yes
    LocalCommand scp -q ~/.vimrc %r@%h:~/.vimrc

This will automatically copy your .vimrc whenever you SSH to any host.

For teams or frequent users, set up a central configuration server:

# In your .vimrc on all machines:
if filereadable('/mnt/config-server/vim/vimrc')
    source /mnt/config-server/vim/vimrc
endif

Mount the config server via NFS or SSHFS for automatic updates.

Use a plugin manager that supports local installations:

# In your .vimrc:
if empty(glob('~/.vim/autoload/plug.vim'))
  silent !curl -fLo ~/.vim/autoload/plug.vim --create-dirs
    \ https://raw.githubusercontent.com/junegunn/vim-plug/master/plug.vim
  autocmd VimEnter * PlugInstall --sync | source $MYVIMRC
endif