Perhaps you have thought of using Vim as an editor, but found the list of things required to get it setup too daunting. Vim can be an extremely powerful editing tool, but lots of functionality is tucked away inside plugins and can be hard to discover. This article will skip the basics of Vim and instead focus on establishing a workflow and introducing some popular plugins to make life easier. If you are just starting with Vim, I would recommend trying an interactive Vim tutorial (or even enjoy a Vim based game!) as a starting point before reading this guide.
Vim Configuration Options
Settings in Vim can either be applied in the editor, or can be read in from your
If you are happy with a setting, and wish to apply it to every instance of Vim, you can place it in the Vim configuration file located at
Common Options
You may have seen people sharing their
I do recommend backing up your .
While I don’t recommend blindly copying and pasting these commands, I would like to give a quick list of the most common Vim options:
- - Suppresses the .swp files that would otherwise litter your working directoryset noswapfile
- - Shows line numbers in the left gutterset nu
- - Automatically spaces the caret to the correct indentation level when creating a new lineset autoindent
- - Changes the number of spaces in a soft tabset softtabstop=2
- - Turns on syntax highlightingsyntax on
For more options, check out this article from Linux Journal.
File Management
When working with code, it is common to jump between multiple files and to search for references to function definitions located in various places around the project. This can be easily managed by Vim using tabs, buffers, and a few nice plugins to extend search and file management.
Change your working directory to your project, and open Vim by issuing
You may also use splits to show multiple buffers on the screen simultaneously. If you open a directory, Vim lists the directory using the netrw plugin by default. To open a file from netrw in a split mode, you can press
Vim also offers support for tabs, with each tab mapping to a buffer. A file can be opened in a tab from netrw by pressing
Plugins to extend window functionality
Bufexplorer allows easy access of open buffers (files) inside of Vim. Open buffers can be sorted by most recently used, filename, or file extension. Note: I didn’t have any luck installing this plugin via Pathogen, but you can unzip the package into your Vim directory.
Search
Searching in Vim can be done inside of a file or in a given directory (such as the root of your project). The easier of the search types is searching within a single file. You can search at any time by pressing the
To search across multiple files, Vim includes a built in
Admittedly, the default searching settings in Vim can be difficult to work with, especially given how often performing a search can occur. You can make the default search behavior of Vim work more in line with other editors by setting an option to not escape special regular expression characters. Set the option
Plugins to extend search
CtrlP mimics the fuzzy matching provided by other editors including Textmate, SublimeText, and Rubymine. Pressing Ctrl+P allows you to type any part of a file path—directory or name—and it will locate all matching files. This is similar to the popular Command-T plugin, but without the Ruby dependency for Vim.
Ack is a fantastic search tool that is a solid replacement for
Built-in Functionality
The following is a list of built in features that Vim provides that may aid you in development without the need for plugins:
Spell checking is built in, and some colorschemes like Zenburn support syntax highlighting for misspelled words. A user can move the cursor over a misspelled word and press
Word completion is a convenient way to auto-complete a long word. Start typing the first few characters, then press
Plugin Management
Plugins can be installed into your
Other plugins will come distributed in .vba (Vimball) format, and will refer to you sourcing the file from inside of Vim. This is done by opening the .vba file inside of Vim and typing
Keeping plugins in Vim up to date can be a pain, since there is no built in package management utility to inform you when updates happen. Because of this, a popular solution is to clone the plugins from their repositories, instead of downloading and unzipping. Because many of these plugins share common subfolders, you would have to keep them isolated from one another, and mix them all in together at load time. Fortunately for us, the pathogen Vim plugin does just this. It allows us to place our plugins in separate folders inside of the
Check out pathogen as a starting point for managing all other plugins.
Popular Plugins
Several of Vim’s more popular plugins are listed below for your reference. These are broken down by categories.
Editing
- Ctags
- Tcomment
- Scratch
- Surround
File Management
NerdTree shows a hierarchical listing of all of your files. You can select any file in the list to open, or change directories, and list its contents. This plugins has lots of options for navigating files.
External Services
vim-fugitive integrates Git into your editor so that you can execute many of the commands on a file without having to shell out or change tabs. A great example is running Gblame on an open file and seeing the results in the gutter of the file pane.
Ruby on Rails Specific
vim-rails offers lots of nice awareness with Ruby on Rails projects. Plugin feature highlights include
Final Thoughts
Vim can be a very powerful editor. Its axiom of modal operations makes it an incredible pairing for writing—as long as you stick with it long enough to grok its concepts. If you feel like your past editors have needed too much clicking, or keystrokes to accomplish everyday tasks, Vim may be just for you. This guide will hopefully create a starting point for users familiar with the Vim basics, but who are eager to bridge the gap between understanding of individual operations, and the application of a workflow in their editing needs.
Do you use any plugins that we missed, or have any additional helpful tips to share? We would be happy to hear them, so let us know!
—Ben Simpson