So You Want to Learn Vim… First Correct Decision You’ve Made All Week

Congratulations, newbie. You’ve made the first step towards the side of righteousnous. Vim will guide you towards a place beyond your wildest dreams. Oh, the road shall not be easy. It may test your faith at times, but the rewards will be magnificent.

The cool thing about vim is that it was designed ground up to require the fewest keystrokes possible. Its philsophy is speed when typing and editing, and any time you’re required to move your fingers away from the base row, you’re wasting time.

Basics

Unlike most other editors, vim has what are called modes. In particular, vim has 3 modes: visual mode, normal mode, and insert mode.

Normal mode is the default mode - it’s meant for fast navigation and large changes in many lines of text.

Insert mode is meant for? You guessed it, inserting text. This is what we think of when we consider most text editors.

Visual mode is what you do when you select text. Imagine you wanted to select a glob of text and replace it with a word. That’s a job for visual mode.

It seems needlessly complicated at first, but this is at the core of vim’s advantage.

There are several ways to open vim. vim by itself opens up a new buffer with nothing loaded. vim [filename] opens up a buffer with that file loaded. If that file doesn’t exist, then it creates a new buffer named [filename].

Let’s go over normal mode basics:

Command Description
j Down by 1 line
k Up by 1 line
h Left by 1 character
l Right by 1 character
C-f Down by a page
C-b Up by a page
w Next word
b Beginning of a word
e End of a word
gg Top of file
G Bottom of file
0 Begining of the line
$ End of the line

Great, now we can do basic things in normal mode like navigate. How do we quit vim? To quit vim, you must be in normal mode. In normal mode, use :q. If your file has unsaved changes, it won’t let you quit (vim doesn’t want you to lose changes by accident - how nice!). If you really want to quit without saving changes, use :q! in normal mode.

But how do we save? :w. w for write. If we want to write and then immediately leave vim, we can chain together :w and :q with :wq.

How do we switch between modes?

The best part about vim is the many ways in which we can enter insert mode.

Command Description
i Insert before the cursor
a Insert after the cursor
o Insert at the beginning of the next line
O Insert at the beginning of the previous line
S Delete the entire line and insert on the same line
C Delete from the cursor to the rest of the line and begin inserting
A Insert at the end of the line

And there are many more!

Once in insert mode, you simply type like a normal text editor and it simply adds the text. C-W deletes the previous word, just like on the terminal, when you’re already in insert mode.

No matter what mode you’re in, pressing Esc will take you back to Normal mode. However, Esc is rather cumbersome, and that violates Vim’s entire philosophy. Thus, C-[ (control-left bracket) is often much easier to do, and equivalent. For convenience, I would recommend remapping Caps Lock and Control.

There are 2 kinds of visual modes. To enter regular visual mode, just press v from normal mode, and begin moving around just as you would with regular normal mode commands. It will begin to select text as normal. Then, when you enter insert mode, your inserts will only apply to that particular block.

For example, if I have

def stuff():
    print "Omg. So many lines of code."
    print "Dude, seriously though."
    return 5

and I highlight the entire block with visual mode, when I press S, it will delete the entire block and place the cursor at the beginning of that block, and I will be in insert mode.

Another way to enter visual mode is using C-V. This is used for column-wise highlighting. The classic situation is commenting out or commenting in a block of code.

def doge():
    print "Such code."
    print "Much python."
    print "Wow."

Let’s say I wanted to comment out the entire doge function. Obviously, I wouldn’t shift to insert mode in front of each line, insert #, and then go back to normal mode, and do the same thing for the other lines. That sounds miserable.

Instead, I’ll press 0 on the first line, taking me to the 1st character. Then, I’ll press C-V (that’s Control, and while holding it, Shift-V). Now I’m in column-wise visual mode. Then I press G which takes me to the end of the file, highlighting everything in the first column along the way. Now, pressing I allows me to insert at the beginning of the line. I add a # and shift back to normal mode. So much easier!

Text Manipulation

This stuff takes a lot of time to learn, so if you’re feeling overwhelmed, don’t worry you’re not alone. Vim is no pushover. Keep using it, and anytime you find yourself doing a repetitive keystroke, search for how that could be faster.

Over time, you’ll find yourself memorizing many shortcuts and learning new ones along the way (I still learn new ones every day). Did you know that pressing C-A in normal mode over a number automatically increments it? Just learned that one yesterday. Apparently it even works on dates! Vim is incredible, and you really never stop learning.

This goes back to that “Hacker Spirit” we spoke of last time. Never stop learning, never stop being curious.

Anyways, vim has many more complicated ways to manipulate text. You can often times string commands together. For example, caw deletes the word you’re currently on (in normal mode), and automatically shifts you to insert mode. This is an easy way to change a word to something else.

daw on the other hand, simply deletes it while keeping you in normal mode.

If you don’t know regular expressions, you really should consider learning them. The reason languages like Perl are considered the most powerful for text-editing is because of regular expressions, and tools like grep utilize them as well.

Vim is no exception.

Imagine you find yourself in a situation where you need to change every if statement in some section of code from if (a == 5) to if (b == 5). Would you want to do that by hand? What if you miss one single if statement. Ouch.

This is where regular expressions come in. In normal mode, this is a single line in vim. :%s/if (a ==/if (b ==/g. Can you believe such a small line can do something so powerful? Of course, there are much cooler things vim can do - this is simply to give you a taste.

Buffers? Files? Windows? Tabs?

What if you want to edit multiple files in Vim? Should we open multiple terminals and have each one have a single vim file? Of course not.

Vim has many solutions for this. Most novice users will use tabs. :tabnew in normal mode will create a new tab in vim. Close tabs like you would close vim, with :q or :wq. :gt can be used to cycle through tabs.

Once you create a new tab, it will be empty at first. What if you wanted to load a file onto that tab? Use :e [filename] to open a particular file. You’ll have to put its relative path from the directory in which you initially opened vim.

That’s decent, but what if you wanted to have multiple files on the same screen? For example, I wanted to have the file a.c in the top half of my screen and b.c in the bottom half? Of course, we can do that as well. I open the first file by vim a.c. Then, in normal mode, I can do :split or :sp for short. :sp Creates a horizontal split where, if I supply no parameters, it uses the current buffer. So there will be 2 a.c’s. Since I wanted b.c, I instead type :sp b.c, and voila! My screen is split.

But I have an extra long monitor, you say. I want to split vertically, not horizontally! Not to worry, that is what vertical splits are for! :vsplit, or :vsp for short will do just what you desire.

You can navigate between windows in normal mode. If you want to move to the window above you, instead of k you would do C-w-k. Similarly, to go to the window below you, instead of j, use C-w-k. Easy enough!

But what if I want to view multiple files without using tabs or windows? What if I just wanted one buffer open, but at times I wanted to rapidly switch between files inside that one buffer?

Absolutely. Do note that there are plugins that make this significantly easier (like Unite and Control-P), but of course Vim has a native solution.

How do we even get many files in one buffer? Imagine you did vim file1.txt file2.txt file3.txt. All three of the files wouldn’t be opened in tabs. Instead, there would be a single buffer with file1.txt showing. :buffers lists all buffers currently open - it would show file2.txt and file3.txt as well. :ls and :files also do the same. (Normal mode, remember!)

Switch to any buffer by doing :buffer <name> where <name> is the name of the buffer.

Plugins Galore!

There are many many plugins out there. Vim has been around for longer than most of us have been alive. There are plugins to make installing other plugins easier.

Some are clearly better than others. There are autocomplete plugins (so vim can do things like eclipse and fill in words). There are many syntax highlighting options.

If you ever feel unsatisfied with vim, edit your ~/.vimrc file. There are tons of sample vimrc files out in the interwebs, and many people have extremely useful tips. Feel free to steal them and make it your own! I’ve posted my own vimrc (minus plugins) on Piazza.

Several of the top plugins include:

Installation? Psh. Would You Like a Spoon in your Mouth?

See Recitation 1. At the bottom, there’s a section entitled “The Hacking Spirit”. I’ve decided after much deliberation that although I could go into exactly how I installed this stuff (on a Mac), it would spoil your educational opportunity!

Much of learning is done through Googling, Stack Overflow-ing, and mucking around the terminal to see what works. So go, explore! And if you have any truly dastardly bugs, well then you know where to find me (Sudi 005) :)

MUST READ Resources on Vim

Seriously, these are must reads. You will learn much.

Buffers, windows, and tabs

Using Vim’s tabs like buffers

5 Plugins Some Dude Thought were Cool

Vim Bible Part 1

Vim Bible Part 2

The number 1 rated Vim Plugin in the World

A replacement for PowerLine and every other plugin

Vim Wiki Website

I’ll add one last thing - whatever you’re looking for, guaranteed there’s a plugin out there somewhere. Just look for it first. Vim can do most everything an IDE can do, but sometimes there is such a thing as too much. At one point I was having vim do literally everything Eclipse did - debugger and all - and vim was just as slow. There’s a reason to use vim - it’s fast. Don’t lose sight of that amidst the sparkle of new plugins.