An Introduction to Vi (Part One)

11 July 2012 - Vi

I was first introduced to Vi 10 years ago when I first started working in the games industry for Razorworks (an Empire Interactive studio). A lot of the developers there used the Open Watcom editor, which is a Vi-style editor. It certainly took a long time to get used to, and I don't think I would have even tried if it wasn't such a big thing in that studio at the time. Watching some of the other coders use it made me want to stick at it though. Watching the code evolve and morph almost organically gave me the motivation I needed to stick at it. And I'm so glad I did. I quickly moved from the Watcom editor to Vim, which is much more popular and widely used. Now my IDE of choice is VisualStudio as I found Vim lacked lots of IDE features that I wanted. Luckily there are two great Vi solutions for Visual Studio - ViEmu and VsVim.

VsVim is an open source project written by Jared Parsons from Microsoft. It is actively developed, and is what I'd recommend using if you use Visual Studio 2010 onwards. Unfortunately, it's not supported on earlier versions of Visual Studio.

ViEmu is a commercial product which has a 'Classic' version for Visual Studio 2002-2008, and another version for 2010 onwards. There's also a version that adds support for SQL Server Management Studio, Word, and Outlook. I've purchased a licence for two reasons. The first is that whilst I use VS2010 at work, I only have 2008 at home. The second reason is that I use SSMS a lot at work, so having Vi support in that is worth the licence fee. They offer a free trial, so it's worth trying both this and the VsVim to see which you prefer.

I still use Vim all the time when I'm doing something outside our Visual Studio projects. If you're not a Visual Studio user, then either give Vim a go, or try and find an addon for your IDE. For example, Eclipse has ViPlugin, NetBeans has jVi, etc.

So why use Vi?

One of the fundamental ideas behind Vi is that you don't need to move your hands away from the standard touch-typing position. All navigation and commands can be done without constantly moving your hands to the cursor and other navigation keys. This is one thing that always strikes me when I see people code without Vi. It looks to me like they're fighting with the keyboard, constantly having to move back and forth from the cursor keys. When you become fluent in Vi, writing code becomes much more intrinsic and you almost 'think' the code into being.

So if you can't move your hands from the touch-typing position - how do you navigate and manipulate code using the same keys as you type with? The key is that Vi has two modes - insert mode, and command mode. Insert mode means that you can type freely just like in a normal text editor. When you switch to command mode, all the keys (and various combination of keys) can be used together to perform very powerful actions.

Getting Started

As mentioned earlier, Vi operates in two modes - insert mode and command mode. In Insert mode you can type letters, numbers, etc just like you would outside of Vi. To enter command mode, you can either use the escape key, or "ctrl-[". Given that the key to switch to command mode is one of the most commonly used keys, I prefer to globally remap my capslock key to the escape key (I use Key Tweak to do this).

One mindset that is important to get into quickly is that the default mode is command mode. You should only be in insert mode if you're actually typing text. Try and get into the habit of going straight back to command mode after you've finished typing. If you've stopped to look and think about your code for a bit, then aim to get to a stage where you've subconsciously knocked it back into command mode whilst you're idle. The primary reason for this is that all the navigation and action keys require you to be in command mode.

Once in command mode, all the keys (and various combinations) perform different actions. I would start off learning and practicing with just a few commands, and then learning new commands once you've mastered the previous ones. There really is such a huge number of commands available that you won't be able to learn them all anyway. I've been using Vi for 10 years now, and still have plenty of "I didn't know you could do that!" moments.

To get back to insert mode, the most common way is with the "i" command. But as you'll see later in the series, there are plenty of other commands which go back to insert mode in different ways (eg. "shift-a" moves the cursor to the end of the line and puts you in insert mode).

Before moving onto basic navigation, I'll briefly mention the Vi command line. You enter this by pressing the : character. This will display a small text entry area at the bottom of the screen for you to enter commands. I'll go into this in more detail in a later section of this series. But for now, here are a couple of command line commands that you'll need to know:

:w					Save (ie. write)
:w myfile.txt		Save as myfile.txt
:q					Quit
:wq					Save, then quite
:e					New file
:e myfile.txt		Edit myfile.txt

Basic Navigation

The very first commands I would recommend learning are the HJKL navigation keys. These basically replace the cursor keys. The table below describes this mapping. A lot of people start off using Vi and continue to use the cursor keys thinking that it's not really that important. I'd HIGHLY recommend not doing this and getting yourself accustomed to using the HJKL keys straight away. It really is fundamental to using Vi efficiently and to anywhere near its full potential. It'll take a bit of getting used to, but once you do, you'll be able to use them without conscious effort. This is the first thing I'd try and get used to. Don't forget that like all commands, you have to be in command mode.

h		Left
j		Down
k		Up
l		Right

You will of course need to navigate more than just a character or line at a time, so here are a few more basic navigation commands that you'll find yourself using all the time:

w						Move to the start of the next word
e						Move to the end of the current word
gg						Move to the top of the file
shift+g					Move to the bottom of the file
f<character>            Move to the next occurrence of <character> on the current line
$						Move to the end of the current line
^						Move to the start of the current line
shift+a					Move to the end of the current line and enter insert mode
a						Enter insert mode with the caret after the current character.
o						Insert a new line after the current line and enter insert mode on that new line.
shift+o					Insert a new line before the current line and enter insert mode on that new line.

These are of course only a few of many hundreds of commands available. It should however give you a starting point. It's not hard to learn them if you start with just a few at a time and drill those first. Then slowly bring more commands into your arsenal over time. Don't try to learn too much, too quickly.

Searching for text

Before I wrap up the first part in this series, this basic navigation overview wouldn't be complete without showing you how to search for a text string. By simply pressing the forward slash key, Vi enters incremental search mode. Once in this mode, you can type the text you wish to search for, and as you're typing, the cursor will jump to the first position that matches that search string. Pressing escape will cancel the incremental search, and pressing enter will leave your cursor position in this new location. You can then use the n and p keys to navigate to the next and previous matches.

If you just want to move the cursor to a particular character on the same line, then you have the f<character> command. This will move the cursor to the next instance of the specified character on the current line. Pressing the ; key will then move to the next instances one at a time. This becomes a very quick way of moving around your code.

Summary

This is just the first part of a multi-part series. In the next part I'll discuss working with the Vi storage registers which are used for storing clipboard information and macro commands. If you have any requests for upcoming Vi topics in this series, then feel free to let me know.