2013-04-14

Make your dotfiles follow you automatically when you ssh to a server

I finally took a little bit of my time to sort out this stupid problem.
For those who login often on remote servers, how horrible is it to see everything set to their default values once you are logged in ?
bash is your shell instead of zsh, there is no color, no completion, no syntax highlight, vim has none of your plugins available, nada !
The first thing to do is to make your configs as self-contained as possible : for example I use oh-my-ssh for zsh which contains the themes and plugins in its own .oh-my-zsh directory and same idea for vim with pathogen.
Then create a small script like this one that you can alias to your ssh command.
#!/bin/zsh
tar c -C${HOME} .oh-my-zsh .zshrc .vim .vimrc .tmux.conf | ssh $1 'tar mx -C${HOME}' 
ssh -t $1 "/bin/zsh"
Edit: OR this cleaner way thanks to mooism2 on hackernews by adding to a specific set of hosts in your .ssh/config
#
# For example for all your server
#
Host *
   PermitLocalCommand yes 
   LocalCommand tar c -C${HOME} .oh-my-zsh .zshrc .vim .vimrc .tmux.conf \
               | ssh -o PermitLocalCommand=no %n "tar mx -C${HOME} ; chsh -s /bin/zsh"


What does it do ?
First, it uses a trick to stream by ssh all the required files from your local home dir and decompress them to your remote home dir. I do it like that to avoid to have any special rights set up on sshd to be able to copy files etc ... Of course, depending on what matters for you, you need to modify the list of copied files. Note for the unusual m parameters : it is here to silence a warning you can get due to the time differences between hosts.
Then, it forces an interactive session and gives you the opportunity to select your favorite remote shell.
Another nice trick is to use a oh-my-zsh theme like "agnoster" and set you DEFAULT_USER in your .zshrc
Normal Prompt →
Remote User Prompt →
Remote Root Prompt →
So your prompt will set with you remoteuser@machine if it is a remote shell and a normal prompt if it is a local one.