Guide to vim

Written by Dominique Yahyavi, updated by Peter Johnston

This quick guide is intended to get you up and running using Vim: creating, basic editing, and saving files. Learning these commands, especially the ones which move your cursor and search, will really make things go a lot faster. There are many great Vim guides available on the web when you're ready for more advanced features.

Configuration

First you'll want to set up a vimrc file to give you some sane editing defaults. If you don't have one yet, view our sample vimrc or download ours into your account using wget http://cs107.stanford.edu/resources/sample_vimrc -O ~/.vimrc

Open

vim myprogram.c open the file myprogram.c if this file exists in your current directory, or will create myprogram.c if it does not already exist in your current directory.

:e filename open a new file if you are using Vim. Again, if this file exists in your current directory it will open it, or it will create it if it does not already exist in your current directory.

Save

:w myprogram.c save the current changes to a file. If you don't specify a name, changes will be saved to the current file. If you would like to save the file under a different name, specify a filename.

Quit

:q quit Vim. If you have unsaved changes, you will be asked whether or not you'd like to save your changes before quitting.

:q! quit without saving unsaved changes.

:wq write and quit.

Move Cursor

Up/Down arrow keys will move your cursor one line up or down.

Left/Right arrow keys will move your cursor one character left or right

b move to the beginning of the word

e move to the end of the word

0 move to the beginning of the line

$ move to the end of the line

:123 jump to line number 123.

crtl-f jump a page forward

crtl-b jump a page backward

gg jump to the first line in file

G jump to last line in file

Search

Search is another great way to move your cursor.

/foo search the file for foo

n search for the next search match later in the file

N search for the next search match higher in the file

Edit Text

i insert before the cursor

I insert at the start of the current line

a append after the cursor

A append to the end of the current line

dd or :d deletes the current line

yy or :y or Y Yank the current line

p Paste the text you yanked or deleted. This will put characters after the cursor and put lines below the current line depending on what you yanked.

Undo/Redo

u undo the last action

U undo all recent changes made to the current line

ctrl-r redo

Common questions about vim

How do I configure tab settings to my liking?

The .vimrc file in your home directory allows you to configure your settings. See our sample_vimrc for an example. The options tabstop and shiftwidth dictate the number of spaces for tab and indent respectively (default is 8 for both). We recommend that you set expandtab to substitute spaces for tabs. This ensures your file looks the same everywhere. You might also be interested in autoindent and smartindent which control how Vim attempts to guess the right indentation based on syntax.

When I do vim filename, I get a big scary message that says something about "Found a swap file." What does this mean, and what do I do?

This most often happens when Vim quits unexpectedly, e.g. if you lose your connection to myth while editing a file. (It can also happen if you try to edit the same file in two terminal windows; you shouldn't do this.)

Here's the recommended process for resolving it: First, hit o for "open read-only". This shows you the version of hte file that was last saved. Look through it. Does it have your most recent changes?

Yes, this is the latest version of the file: Great! To clean up the swap file, :q out, then run vim on the file again. This time, hit d to delete the swap file. (Warning: you can't undo this.)

No, some of my changes are missing: OK, don't panic. Quit out of vim, and start it again. This time, hit r for "recover." This will show you the version of the file from the interrupted session. If that's (closer to) the version you want to keep, save it, then follow the steps above to clean up the swap file.

How do I avoid the above issue coming up next time?

Make sure you exit Vim cleanly. Don't just hit the "X" button on your terminal. Also, don't close your laptop lid while Vim is open (more generally, putting your laptop to sleep while connected to myth could lead to other problems, because it generally turns off your internet connection).