Please note: this blog has been migrated to a new location at https://jakesgordon.com. All new writing will be published over there, existing content has been left here for reference, but will no longer be updated (as of Nov 2023)

Getting started with Vim

Sat, Feb 12, 2011
Embracing Vim

As mentioned in an earlier post, I am switching to Vim as my editor of choice. Like most people, my first attempts were a struggle, but after a little perseverance I began to appreciate the power and to feel the advantages of keeping my hands on the keyboard. So I am going to keep at it for a while longer and see if it sticks…

Installing

Most of my work involves coding in Ruby and Javascript on Ubuntu Linux. There are a variety of Vim flavors available as debian packages. A quick search gives us a number of options:

sudo aptitude search vim

p   vim-gnome        # GUI version for Gnome
p   vim-gtk          # GUI version for GTK
p   vim              # terminal version
v   vim-perl         # terminal version with perl bindings
v   vim-python       # terminal version with python bindings
v   vim-ruby         # terminal version with ruby bindings
p   vim-tiny         # lightweight terminal version 

Your choice depends on what environment you are using: GUI or terminal, and whether or not you require any language specific bindings (typically for plugins built with that language)

In my case I need:

I spend most of my time in a terminal, rather than on the desktop, so I don’t really need a GUI version. However, once you start experimenting with color schemes you will discover that most of them do NOT work in the terminal version which has a limited color palette. In order to get color schemes to work in the terminal version you need another plugin (CSApprox) that will convert the color scheme to the closest available terminal color. The CSApprox plugin requires a version of VIM that is built with the GUI bindings, so even though I will be using Vim in a terminal, my choice ended up being vim-gnome. It is built with all the necessary GUI and Ruby bindings required for the plugins which I would like to use.

sudo apt-get install vim-gnome

Once installed, you can investigate what is included in the build by checking the version

vim --version

Then use it to create and edit a new file:

vim

… or to edit an existing file:

vim [FILE]

… and away we go!

First stumbling attempts

Unlike most editors, Vim is based around modes, primarily

This is one of the hardest parts to get used to. If you are not careful, you can easily start typing away, thinking you are writing code, only to trigger a random command that needs to be undone. Luckily u(ndo) is an easy command key to remember! (redo is <C-r>)

Switching between command and insert mode becomes more natural over time as you come to grips with basic commands like:

… along with the [command][number]object pattern followed by most vim commands.

A common tip is to get in and out of insert mode as quickly as possible. insert your text and hit <Esc> to go back to command mode gets you thinking about editing in terms of commands.

Enter the <C-v> command to switch to visual mode where you can use either the mouse or the arrow keys to make a visual selection. Once a selection is made, it can be acted on by more commands such as:

Y(ank) is the Vim equivalent of copy. Once you have Y(anked), you can P(ut). Putting with a lowercase p will insert the text after the cursor, using an uppercase P will insert the text before the cursor. There are many Vim commands that have subtle behavior differences between the lowercase and uppercase version and in order to be efficient you should be aware of those differences.

In addition to key commands, Vim can be given explicit commands in the command area at the bottom of the screen starting with the : character, e.g:

 :write
 :quit

many of these can be shortened:

 :w
 :q

and combined:

 :wq

One of the most important commands to learn is, of course, help.

 :help (topic)

This post is not meant as a Vim tutorial, its more of my initial thoughts and actions while trying to get up to speed on Vim. I would highly recommend anyone coming to Vim for the first time go spend some time with a book or on the Vim wiki.

Breaking MS Windows Habits

There are 2 commands that can be used to ease transition for people coming from a Windows background. You can ask Vim to behave like mswin, you can also setup some windows like shortcuts

 :behave mswin
 :source $VIMRUNTIME/mswin.vim

This allows you to use shift-arrow keys to perform selections, ctrl-c/v for copy & paste and ctrl-z/y for undo/redo.

The problem here though, is it gives you a false sense of comfort. It feels close, but it doesn’t quite work exactly the way you expect, and when it’s different its shocking.

I would recommend NOT turning on mswin behaviors. Learn the vim commands first, then think about how you might want to setup key mappings for your personal preferences later, and if you find yourself trying to map commands to windows-like keystrokes then come back and visit the mswin behavior again after you know a little more about Vim.