I’m not going to try convince you to use Vim.
If you’re reading post and haven’t already closed it, you’re probably already convinced that it’s useful.
The purpose of this post is to help if you want to learn Vim because you see its promise, but just can’t seem to get past the hump of learning it and making it your primary editor. Or, perhaps you don’t know where to start.
In my opinion, and probably many others’, it is worth getting past the Vim hump, so hang in there! I’ll tell you how I went from IDE lover to Vim lover and hopefully provide some useful tips to get you there as well, if you want to.
I got my start developing in Java. So, what was my code editor of choice? An IDE, of course! First Eclipse, then IntelliJ IDEA.
As I moved along in my career, I saw presenters at meetups and conferences who used Vim. I watched in awe as they performed text gymnastics during their presentations. It was like they were playing an instrument, rather than coding.
Occasionally, in a fit of inspiration, I would give Vim a try. But I could never could get into the swing of things. It just seemed too hard to learn and I couldn’t get past how difficult it was to navigate around a project with a large set of files.
So, I puttered on, half-effectively using Vim on remote Linux servers where I quickly needed to view or edit a file without downloading it.
The moment of inspiration
Usually it went something like this:
Vim Ninja: (( tap tap tap )) – ((50 lines show up on screen in the exact spot he wanted))
Me: Wait! How the hell did you do that!? Show me!
Vim Ninja: Oh, that’s a macro, you just type ‘q’ then…
You get the idea.
I didn’t get any detailed training from him, per se, but I was able to find out about some useful plugins and configuration options that got me really excited to use it again.
So, I spent a handful of hours learning how to configure Vim and installed those plugins that the Vim Ninja recommended. After I finished, I told myself I was going to force myself to use Vim and only Vim for at least two weeks.
Then, shortly after I started, this happened:
I love this slide from SparkBox, because it is so true with lots of developer tools, but especially with Vim (and Git – perhaps the topic of another post).
If you’re comfortable where you’re at coding, when you decide to try a new tool like Vim, there is an immediate, sharp hurt you will feel. The hurt is usually accompanied by questioning the usefulness of the tool and the strong desire to give up.
You have to be determined to get through that hump, but when you do, you reach places you never thought possible.
How to learn to stop worrying and love the Vim
To appreciate Vim and take full advantage of it, you have to want to learn it, and need discipline. Force yourself to use it as your primary text editor for at least a few days or a week. Longer if you can. If you don’t, you will simply hit the “Ouch!” stage, get frustrated and never go back.
Where to begin?
For people in the same boat I was, or people who are brand spankin’ new to Vim, here are a few suggestions to get started on your Vim journey.
Install the right version
As of this writing, the latest version is 7.4 and that’s the one you should install to get all the latest good stuff.
On OS X
On OS X, Vim is installed by default, but chances are it’s an older version and you’ll want to get a newer one, and update it frequently.
To do this, I would recommend using Homebrew and installing Vim using that:
brew install macvim --override-system-vim
You’ll also want to make sure that your fresh new Homebrew install of Vim is the default version you use when you type
--override-system-vim flag should do most of this.
If you type
which vim you should see
/usr/local/bin/vim. If you hit issues, the default version of vim may be first in your
$PATH and may need to be adjusted. This StackOverflow post is really helpful to get that configured correctly.
For Windows, the Vim installer will give you options to install a gui and console version of Vim to use.
Learn the basics
This post isn’t about how to do this, but there are tons of resources out there:
- Use the built in tutor: type
:help tutorin Vim
- Play a game
- Find another tutorial
- Or, if you don’t mind paying a bit, check out Upcase
Go whole hog
Don’t learn a bit of Vim, dip your toe in, then shift back to Sublime Text (without vintage mode on).
Make Vim your default editor, and open up all your text files in it. Resist any urge to open up anything else to type into, unless there is no other option! Do this for at least two or three weeks.
This is a bit like trying to get into an exercise regimen. It’s painful at first, but then gets easier and easier. Finally, you get to a point where you feel great about it and it starts to become more effortless.
Find a time when you’re not under tight deadlines
Under the gun to get features or fixes in at the end of a project? Not the time to be learning a new editor. Learning Vim will cause you to frequently pause, remember a key combination, and interrupt your flow temporarily.
Find a stretch of time at the beginning of a project, or second to that, in the middle of a project, where you have a bit of leeway to let your brain and your finger muscles get used to the shift in how your new editor works.
If you’re not under a lot of pressure, you’ll power through the “Ouch!” stage in a much easier fashion.
Focus on how to properly use Visual Mode
Learning how to really use visual mode effectively was my “a-ha” moment. This is why using a modal editor is great.
Learn the basics of
shift+v and about yanks and puts. Once you get the hang of these basics, you will be flying and the (text) world is your oyster.
Learn about the basics of a .vimrc file
A .vimrc file in your home directory is how you will configure Vim. And you’ll probably want to set some custom configurations right off the bat.
A great start is showing line numbers. (Why doesn’t Vim show them by default!?)
Just create a file called
.vimrc in your home directory which contains this:
And start vim up. Viola, you’ll have line numbers all the time.
Then, go from there. There are tons of great resources out there on how to configure Vim. I would recommend finding some examples of .vimrc files and learning about the configurations you see in them. Everyone and their brother are starting to post their .vimrc and other dotfiles on Github.
You can find my .vimrc, too, it’s right here.
My favorite resource is to look at ThoughtBot’s .vimrc file. They have a good default set of options and the comments do a pretty good job of explaining what certain things are.
Install Some Good Plugins
There are some Vim purists who think you should learn Vim without using plugins. I disagree. It is useful to know “pure” Vim in case you’re out on servers and don’t have any plugins. But doing any significant development in Vim without a few decent plugins is like using a rotary phone – it’ll get the job done, and it’s kinda hipster-retro-cool, but it ain’t fun.
First off, go get Vundle. Vundle is a Vim plugin manager that will make installing new Plugins and updating them a breeze.
Once you get it setup, you can just add lines like this to your
.vimrc to add new plugins:
In this config, the text you put inside the quotes is a git repo for a Vim plugin. Then you can run
:PluginInstall to install. You’re done.
A list of plugins that will come in handy regardless of your language
This allows you to type
ctrl+p and a filename. It’ll do some fuzzy searching on your filesystem to find files that match. You can then navigate up and down the search results list and open a file in your buffer.
This is really similar to Eclipse’s
ctrl+shift+r or IntelliJ’s
This gives you a sidebar with a tree version of your filesystem (think the tree view in Sublime Text).
It’s awesome to be able to navigate up and down your file structure and open folders freely with just the keyboard. This is really great when you can’t even start to remember the name of a file.
I also find it really useful to use Vim’s
/ search functionality find a folder name in the tree to quickly navigate to it.
I would recommend mapping the toggle on/off for nerd tree to a key binding like
map <C-n> :NERDTreeToggle<CR>. This maps
ctrl+n to turn on the tree.
Auto completion of parens, brackets, etc. Ya need it.
If you use git, this one is great to see visual indicators of what git status your files are in right in a file tree.
Again, for you git users, this one is great to visually see added/deleted/changed lines in the left hand side of your buffer.
Java coders, things are lacking…
Simply put: I could not find anything that could really compare with the tight integration and IDE provides for a static language like Java.
There is real power of being able to navigate to static types and methods with a simple keyboard command or ctrl+mouse click. It’s also crucial for me to be able to do type-safe refactorings. Both of these things are built right into these IDE’s and are bound to keyboard shortcuts right out of the gate.
That’s not to say there aren’t options for Vim: I tried out Eclim which requires a headless version of Eclipse, but it was wonky to setup, and didn’t run very smoothly. It just didn’t feel worth it.
So, if you have to code in Java and only Java, you might have a tougher time really committing to Vim using the suggestions in this post.
If you learn a combination IDE shortcuts for navigating around files and combine those with Vim’s keybindings along with Visual Mode, you’re more than halfway there. That’s what I do, and I find it really effective.
If you’re new to Vim and this is your only option, just really force yourself to not turn the plugin off if you get frustrated! :)
Some other thoughts
If I had unlimited time, I would really love to create an open source project which packages up these essential Vim plugins and installs them with some default configurations. I spent a pretty good amount of time getting configurations together so Vim felt usable enough for me. It would help others get over the hump a lot quicker if there were pre-configured “packages” that could be installed. Maybe something like this already exists and I don’t know about it?
Finally, while writing this post, I found this really great post from Antonin Januska which shares some very similar opinions of mine and is a really good read.
Update 2/26/2015: I Just came across a fantastic, step by step tutorial to setting up vim with plugins by jez on github. He uses a great approach of using git commits to walk through the process.