Tools of Trade – Vim

Make it right

Sometimes when you read a book, you wish you have done it earlier. Vim is a tool I wish I have started using earlier. It is an advanced text editor which has been around for more than two decades.

Vim is highly configurable, fast and lightweight. Whilst it is a first class citizen in Unix world, Vim runs well on some exotic platforms like Windows too.

Neither I have intention to brag about how productive I am at writing code with my both hands on home-row nor, to praise Vim for its awesomeness. You can skim on what others have already said and find reviews on the Web yourself.

Despite being a very powerful tool, many who tried to master it have failed. Vim has a steep learning curve, some say it takes about two weeks to get used to it. Honestly, I have been using Vim for more than half a decade and I am still learning. For this reason, I want to give some tips for newcomers who dare to try it.

Whenever you are about to do something it’s a good thing to have some plan. My plan for learning new tools is very simple:

  1. Understand user interface.
  2. Learn to use documentation and grasp key concepts.
  3. Master keyboard shortcuts.
  4. Customize.

User Interface

When you first install and launch Vim, don’t be deceived by its initial dreadful appearance. Vim’s engineers did it on purpose to scary off cowboy coders. In customization part we will put some makeup on it so your eyes don’t hurt anymore.

Here is an example of gvim (vim with GUI) for Windows.

gvim on Windows   GUI elements

In the picture on the right I have colored the main elements of the user interface, leaving out menu bar and toolbar (let’s start ignoring presence of mouse). The blue areas are called windows, the yellow areas are status lines for each window, orange bar is a window splitter and the green one is a command-line window. Altogether they make-up GUI or as  Vim users call it the screen.

When you open file for editing it is first loaded into a buffer and then displayed in the window. So window serves a purpose of viewport on the buffer. Until you write the buffer to a file, original file remains unchanged. You can use multiple windows for one buffer, or several windows for different buffers. Also, you can split windows horizontally or vertically, and rearrange them the way you wish.

Status line displays information for the window. You can customize which information to display and have a different status line in each window.

Using comman-line window you can pass commands to the editor.

The Documentation

Although, Vim has an excellent built-in documentation which can be accessed using :help command (or just :h), I do not recommend using it yet. Learning process should be fun, so far you don’t know how to move around and work with windows efficiently, and could easily get frustrated. Better you use your favorite web browser and have html version of help at hand, and don’t forget to visit FAQ.

I am not going to copy-paste and present here what is already available in official documentation, paper is expensive and I do care about the Environment. So you have to read it yourself. Vim’s documentation is very comprehensive and you can probably easily find answers to all of your questions. However, the biggest mistake you could make is trying to rush things. Make sure you read all “Getting Started” parts, take notes while practicing (best with Vim) and get yourself a good cheatsheet. Most importantly be patient and when you feel desperate use the last resort command :h!.

Mastering Keyboard Shortcuts

Great, two weeks have already passed. By now you have spent many hours practicing the first steps in Vim, you know Vim modes by heart, and you don’t even blink when switching from mode to mode. You love recording macros and playing them in other windows, you don’t think yanking is a witchcraft anymore, and merging 3 files at the same time never have been easier.

Hooray! You can start customizing and extending Vim’s powers.

Customization and Plugins

As described in documentation to customize Vim you have to tinker its runtime configuration file called vimrc. In Unix like systems it is in your home directory ~/.vimrc. In Windows it is either $VIM/_vimrc or $HOME/_vimrc (in installation location or user profile directory respectively).

Additionally, other runtime files (plug-ins, code snippets, color schemes etc.) are used to customize Vim and extend it’s functionality. Vim looks for them in a runtime path. Just make sure to keep your personal runtime files separated from Vim’s, and don’t mess with the things you don’t own. I recommend using a revision control system to manage your configuration files too. For example I share same configuration on different systems and keep it like this:

on Unix on Windows
~/.vimrc
~/.vim/
$VIM/.vimrc
$VIM/vimfiles/

vimrc

In vimrc settings are defined as command instructions and are executed when Vim is started. You can execute the same instructions in the command-line window manually e.g. command set number will turn on line numbers.

I am sharing my vimrc with brief comments, it should serve as a good starter for newcomers. To get long description and list all available options use :help <buzzword> e.g. :help number.

Color Schemes

Time has come to turn the ugly duckling into a beautiful swan. It’s not a secret Vim users prefer dark color schemes. When you find yourself writing code in the oddest places with nothing but a candle by your side you start to appreciate the absence of visual noise and non-blinding colors. Vim Ninjas have a nice list of them, make sure to try them. To install a color scheme put it in colors directory in your personal runtime path e.g. ~/.vim/colors and use :colorscheme <name> command.

I prefer something even more darker and have made my own color scheme using Vivify. That’s how it looks.

Customized Vim

Plug-ins

Standalone Vim is already a very capable editor but with a vast assortment of plug-ins you can improve its functionality to a great extent. I recommend to look into the following:

Command-T Provides fast file navigation. Requires Vim compiled with ruby support. On Windows I use Wu Yongwei’s build.
ctrlp Fuzzy file, buffer, Most Recently Used (MRU), and tag finder.
fugitive Provides Git integration.
MatchTag Shows matching HTML tag pairs.
NERD Commenter Provides code commenting operations.
NERD Tree Tree view style file system browser.
syntastic Syntax checking plugin that runs files through external syntax checkers and displays any resulting errors to the user.
TagBar Plugin for browsing the tags (classes, methods etc.) of source code files. Requires ctags available in the system path.
UltiSnips Ultimate snippet management for the Vim.
ZenCoding Hi-speed HTML and CSS coding.
ZoomWin Maximize current window like in IDE.

Vim as IDE?

Vim is not an IDE and is not trying to pretend to be one. For big and complex projects it is not sufficient. However, if you are a front-end developer Vim is more than enough. For serious development it lacks advanced debugging and project specific refactoring with context awareness. Without refactoring your code base will likely rot and bad smells will give you many painful headaches. So be sane and use the right tool for the right job.

Whilst you cannot have all shiny features in Vim, you can bring some of Vim to IDEs. I use Vim extensively as external tool in Visual Studio. Syntastic plug-in with JSHint and W3C CSS validator is a must-have for me. Also, you can get Vim-modes in editor using ViEmu or VsVim extensions.

MonoDevelop has Vi Mode built-in but it is still in development and with limited functionality. I really miss custom key mapping.

Eclim brings Vim to Eclipse and Xcode has XVim.

Conclusion

To wrap up, I surely can live without Vim but I cannot work without it anymore. Give it a try and decide for yourself if you want to add Vim to your personal toolbox.

2 Responses to “Tools of Trade – Vim”
  1. Spajus

    Vim is more than sufficient for big and complex projects.

    • slicklash

      I would really like to agree with you but I cannot. It depends on how we define *sufficiency* and *complexity*, not to mention everyone perceives things differently even in the same environment. What type of the projects are you referring to?

Leave a Reply