I’m back to using VIm again, after a few years of working with TextMate on the Mac. VIm is great because it works the same on many different platforms, and I jump from Mac to Windows a lot these days.
I’ve devised a setup that lets me keep almost all of my configuration for VIm on my Dropbox account so I can have the same configuration synchronized on all of the computers I use. Here’s how you do it:
Of course, I assume you have Dropbox and VIm installed on your Mac and Windows machines. I’m not covering any of that setup in this article.
You need a few folders in your Dropbox to make this work. I find doing this on my Mac the easiest:
mkdir -p ~/Dropbox/dotfiles/.vim/autoload mkdir -p ~/Dropbox/dotfiles/.vim/backup mkdir -p ~/Dropbox/dotfiles/.vim/bundles
Next, we’ll create a master
.vimrc file in our Dropbox. If you already have one, move it there.
mv ~/.vimrc ~/Dropbox/dotfiles/.vimrc_main
If you don’t have a good config file, there’s one at the end of this article you can look at. For now, just create a blank file there:
Next, let’s set up a way to manage plugins, or extensions to VIm.
We’ll use Pathogen.vim to manage our plugins. Pathogen modifies VIm’s load paths and lets us keep our plugins in folders we can easily update.
Download Pathogen.vim from the official site and place it in your Autoload folder.
mv pathogen.vim ~/Dropbox/dotfiles/.vim/autoload/pathogen.vim
Now, create a new .vimrc in your Dropbox folder. This will be our normal .vimrc for our macs.
And put this in the file:
call pathogen#helptags() call pathogen#runtime_prepend_subdirectories(expand('~/Dropbox/dotfiles/.vim/bundles')) source ~/Dropbox/dotfiles/.vimrc_main
We’re telling Pathogen to load any installed plugins from our Dropbox, and then we’re loading in our
vimrc_main file which will contain all of our platform independent configuration.
Now, symlink that to your home directory so VIm will find it.
ln -s ~/Dropbox/dotfiles/.vimrc_mac ~/.vimrc
And symlink your .vim folder so everything else you put there gets loaded as well:
ln -s ~/Dropbox/dotfiles/.vim ~/.vim
Now we’re ready to set up the Windows side of things.
Since Windows has no symlink support, you’ll need to create a couple of empty stubs. You’ll need a _vimrc file and a
_vimfiles folder in your Home directory.
First, from a command prompt, create a
And an autoload folder
Then copy pathogen.vim into that folder from your dropbox
cp "%HOMEDRIVE%%HOMEPATH%\My Dropbox\dotfiles\.vim\autoload\pathogen.vim" %HOMEDRIVE%%HOMEPATH%\vimfiles\autoload\pathogen.vim
Create the file
Add this to the file:
call pathogen#helptags() call pathogen#runtime_prepend_subdirectories(expand('c:\My Dropbox\dotfiles\.vim\bundles')) source c:\My Dropbox\dotfiles\.vimrc_main
This is nearly identical to the file on our Mac, but it instead specifies the paths to the vim bundles folder and
vimrc_main files in our Dropbox. Change those paths to the ones for your Dropbox installation!
Environment Variables and Path
At this point, you should set a
HOME environment variable, as Windows doesn’t yet ship with that variable set by default (although it does have
HOMEPATH variables.) Many VIm plugins use the
HOME environment variable, and things might break down if you don’t have one set. You can set the
HOME environment variable to
Next, you should ensure that VIm is on your system PATH so you can access it from the commandline.
You typically set both of these environment variables through the Control Panel.
- From the desktop, right-click My Computer and click Properties.
- In the System Properties window, click on the Advanced tab.
- In the Advanced section, click the Environment Variables button.
- Finally, in the Environment Variables window, highlight the Path variable in the Systems Variable section and click the Edit button. Add the path to gvim.exe to this list, keeping in mind that each different directory is separated with a semicolon.
- Next, create a new environment variable called HOME, and give it the value
Many of the most awesome plugins for VIm are now maintained in Git repositories stored on Github. If you have Git installed on your Mac, which you can do quickly with the Mac OSX installer, you can clone each plugin’s repository right into your
vim/bundles folder and Pathogen will load them right up.
Here’s what I use:
- Rails.vim gives me support for Rails development
- The Cucumber and HAML bunldes provide syntax support for their respective languages.
- The NERDTreee plugin gives me a project tree which is absolutely necessary when working on anything substantial.
- The closetag plugin lets me close any open HTML tags by pressing CTRL+_, and since I work in HTML and XML a lot, it’s a lifesaver when you’re several levels deep in a hierarchy.
- I’m using the VividChalk color scheme, which I love.
git clone git://github.com/tpope/vim-rails.git
git clone git://github.com/tpope/vim-cucumber.git
git clone git://github.com/tpope/vim-haml.git
git clone git://github.com/tpope/vim-endwise.git
git clone git://github.com/scrooloose/nerdtree.git
git clone git://github.com/scrooloose/nerdcommenter.git
git clone git://github.com/tpope/vim-surround.git
git clone git://github.com/vim-scripts/closetag.vim.git
For suggestions on other plugins, see Tammer Saleh’s blog post on the topic, which is very helpful.
A .vimrc_main config file
.vimrc_main file we created in the Dropbox? You’ll put all of your common configuration in that file. Here’s what’s in mine, if you’d like a starting point. I’ve pulled this from so many sources I can’t even remember where everything came from, but I have found that peeking at other people’s VIm files is really helpful.
A couple of important points about this configuration:
- The mapleader key is set to the comma, and I’ve set it so that the NERDtree plugin can then be toggled with
,dwhen you’re in normal mode.
- I’ve set soft tabs to two spaces.
It’s not an amazing configuration by any means, but it does the job for me.
There are a ton of steps to this process, but once you’ve gone through it, you can easily work on code on any configuration. And you can install Dropbox on Linux, too, so you could expand this over to that platform as well.
Share your comments and suggestions. I’m always looking for new VIm tips.