“I’ve been using Vim for about two years now, mostly because I can’t figure out how to exit it.” – @iamdeveloper
Prelude
Vim is one of those strange beasts that have amazed me since I began coding at the University. Trying to use it was a bit crazy, but it sure was spectacular watching versed users get things done at the speed of light in an infinite succession of weird key combinations. It made you feel that working with a ‘modern’ editor like Gedit or SublimeText is like typing with just two fingers and looking at the keyboard, while using Vim is typing with all your fingers at the same time while looking through the window, and still ace everything.
Vim’s learning curve is a bit too much to start with, and most probably you are already comfortable and productive with your editor of choice, but there are several advantages to using Vim:
- Plugins allows you to do console work from inside Vim, so there are less context-switching and thus, productivity is enhanced.
- Plugins add new keys combinations that once memorized and assimilated put the most complex tasks are just a few keystrokes away. Then again, again, productivity is enhanced.
- Works on OS X/Linux/Windows, so you don’t become prisoner of a platform-dependent editor.
- Your configuration is portable.
- You can work on a remote machine via SSH with Vim. Ideal for remote pair programming as per the reasons exposed on this previous article.
- It’s free!
So, with all those things in mind, Nicolas took the opportunity to give Rob and I the basic tutorial and walked us through his configuration file and directory when I visited the team last month. Here’s a rundown of the subjects we covered.
Enter the Vim
Make sure Vim is installed and type vim
in your terminal or double click its icon if you are on Windows.
Going through the basic movements, modes and editing functions is very long to explain, so the best thing to do it is to try out the vimtutor
command on your console, which actually opens a preinstalled text file on Vim. This file gives you instructions regarding basic commands and navigation, and proposes some tasks as you read it. It is very recommended to do it thoroughly from time to time if you are not used to Vim yet because it will help you to assimilate the basic commands. Also, there’s a good chance that you’ll be able to do it on your mother tongue, so there’s no excuse!
Custom configurations
As I said in the introduction, becoming productive in Vim requires using (and being fluent with) plugins. The real power behind Vim is the capability to extend it with plugins and custom configurations, similarly as we used to do with TextMate bundles or more recently, SublimeText packages. As with those examples, these scripts will extend Vim’s functionality and their nature varies: colour themes, syntax highlighters, new commands…
There is a plethora of plugins available in both Vim Online or Github, so sometimes it’s just a matter of googling for what you need in Vim, and chances are that someone has already done it.
Plugins live in a folder named .vim
on your home folder, and the configuration is on a file named .vimrc
. In this file you’ll have options settings like this:
set shell=/bin/zsh
set background=light
Or plugins listings like these:
Bundle 'tpope/vim-dispatch.git'
Bundle 'thoughtbot/vim-rspec'
And functions, bindings, key mappings… almost everything is configurable!
Anyway, before we go any deeper, there are two ways of starting to work with plugins:
Curated configurations
This strategy is good if you need to get up to speed to a way of working with Vim because you’ll be pairing on a shared terminal (as we do), or if you have a Vim sensei willing to share his/hers with you and teach you. It is also good if you are somehow familiar to Vim and its configuration and you want to try and discover new plugins.
There are lots of people that upload their Vim configuration files to Github, to share and as personal backup. Here at CookiesHQ we have also done it, and our company standard Vim configuration is available for public use at our Github repository.
It contains installation instructions and is very much Ruby on Rails oriented so it is worth a look if you work with Rails. Also, it comes from a Linux machine, so you might need to do some fine tuning to get it all working on OS X or Windows.
If you are on your own and reluctant to having so many plugins installed at a time without really knowing what they do, you can follow another strategy, read on!
Something new every day
Although having a curated configuration available is a good start and example, I think it’s probably better to start expanding Vim one/two plugins at a time. Manufacturing a good .vimrc
file is a career-long task, and sometimes it is very personal, as it evolves with you, your skills and your needs.
The first thing you’re going to need is to install a Vim plugins manager, like Vundle. Then a good place to start can be a series of plugins to make the environment a bit more comfortable:
- Airline: adds a fully customizable status bar displaying information abou thte open buffer, git status, etc.
- NerdTree: adds a togglable sidebar that acts as a tree/file explorer.
Once you’ve made these work and configured to your taste, and its functionalities are integrated in your workflow, perhaps you’d like to move into something more tasty:
- Rails.Vim: adds lots of functionalities related to working with Ruby on Rails, from syntax highligting to an enhanced command palette that allows easier file navigation and file switching, interfaces to Rake and the
rails
command… - Fugitive.git: adds commands to manage Git from Vim.
Those two will give you more to chew on until you get totally used to use Rails and Git from Vim, but eventually you’ll get there, and you’ll probably want some more plugins, or while using the aforementioned ones, you’ll feel the need for a certain plugin that does something on Vim.
There are plugins for Bundler
, Cucumber
, Rspec
, RVM
… just pick one! Having a curated configuration on the side might be of help to know what to install and how to configure it, all the mentioned plugins are on the CookiesHQ config so keep it near just in case.
In the end, you’ll be doing less things on the console and much more on Vim, and hopefully, you’ll be more efficient and productive.
I was told I’d be über productive!
It is hard to switch to such a different editor and keep productivity at normal rates. Most probably, you’ll be struggling (and swearing) just to open multiple files at once, navigating and doing ‘simple’ stuff as copy/paste. So, how to get up to speed? The rule of thumb is, again, something new every day. In my opinion, the best thing to do is as follows:
- Have the basic commands noted down on paper so you use it as cheat sheet (also, printing this one from Viemu.com can be of help)
- Use Vim for pet or personal projects that don’t add the stress of the deadline on your free time or weekends. This way you’ll get better and get things done without smashing your company’s productivity.
- Start with small tasks, redo Vimtutor if needed. Try to do things the “Vim-way” if you have any plugins.
- Move on to larger and more complex tasks, with more files opened at the same time, etc.
- If you feel comfortable with it, try integrating Vim in your daily workflow: start with small tasks, one line changes and such.
Optional
Add a new plugin and learn to use it in your presonal/pet projects.- Loop 3 to 6 as needed, adding more complex tasks to the scenario and plugins if/when you feel comfortable.
There’s no magic recipe to this, I think. It is a matter of time, perseverance and discipline. In fact, I am still in the first iteration of step 3 (and sometimes step 4). Anyway, there are others that have also embarked on their “Vim Quest” and are producing content on the web that helps other learners in the form of tips, advice, or just motivation.
Have you got tips for other fellow Vim novices? Or do you need help with our curated configuration? Would you like to suggest us a plugin? Hit us up on Twitter or Google Plus and share!