If you are like me – a computer nomad, roaming from one machine to another throughout the day – you should put your Vim configuration files under source control. Cause, you are using vim, right? If you are not using vim, then just mentally substitute the word “Emacs” wherever you see “vim” or “vi”. The principles are more or less the same.
My vim files are currently on Github and I finally set everything up so that this single repository can be easily shared by different platforms. Previously I had separate setup for Windows, Linux and Mac and it was driving me crazy. So I went through all of them, threw up stuff I never used or needed, and consolidated them into a single profile that is now portable.
Step number one to accomplish this is to put your .vimrc inside your .vim directory. The directory is where Vim stores all kinds of files – color schemes, plugins, etc.. It is a natural place to create a repository. The problem is that the main config vile .vimrc lies outside of it, smack dab in the middle of your home dir. That file must be tracked as well. So we just move it into .vim and create a soft link in ~.
On Linux and Mac you accomplish it like this:
mv .vimrc .vim/.vimrc ln -s .vim/.vimrc .vimrc
Now create a git repository inside, and boom! You are golden!
What about Windows? Windows sucks. For whatever reason, the window port of the one true editor uses different naming convention: _vimrc for the config file and vimfiles for the directory. These files are located in your %USERPROFILE% directory.
How do we take our Linux config and port it to Windows? First let’s delete the current config and clone our repository:
rmdir /s /q vimfiles del _vimrc git clone path/to/your/.vim.git
Now you should have a %USERPROFILE%\.vim directory with your configuration, and your vimrc file. It won’t do you any good though, because Vim will ignore it. So we need to do what we did on Linux and create some soft links. If you are running Vista or above, you are in luck. The recent versions of windows come equipped with a mklink command (even home editions). So you just do this:
mklink /d vimfiles .vim mklink _vimrc .vim\.vimrc
If you start Vim now, everything will work better than expected.
For the sake of argument though let’s assume you are some sort of a wierdork, and you use Windows as your primary platform. You have your environment all set up there, and you want to push it over to your MacBook or something. Here is what you do:
- Rename _vimrc to .vimrc
- Rename vimfiles to .vim
- Move .vimrc into .vim
- Create soft links for _vimrc and vimfiles
- Put .vim under source control
If you want you can copy and paste these commands to make it happen:
rename _vimrc .vimrc rename vimfiles .vim move .vimrc .vim mklink /d vimfiles .vim mklink _vimrc .vim\.vimrc
On the other end, you just clone the repository into ~ and soft link .vimrc to .vim/.vimrc and you are all set.
If you are extra clever, you should be using Pathogen to manage your vim plugins. If you do, you will be installing them as subdirectories under .vim/bundle. The cool thing about our modern open source ecosystem is that 90% of these plugins are using git and are on github. So instead of duplicating the code all over the place you can set them up as git submodules like I did here.
For example, to add the nifty auto-close plugin you would do this:
cd .vim git submodule add https://github.com/Townk/vim-autoclose.git bundle/vim-autocose git submodule init git submodule update
If you do this, there is a small caveat: by default git does not pull in submodules when you clone a repository. You will end up with bunch of empty directories but no files in them. So you have to remember to do this immediately after you clone:
git submodule init git submodule update
In fact, you would probably want to periodically run the submodule update command to automatically fetch fresh and patched versions of all your plugins.