Guide to vim

Written by Dominique Yahyavi, CS107 TA

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/sample_vimrc -O ~/.vimrc

Open

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

vim reassemble.c& will allow you to keep using your terminal while using Vim. Note that if you are using a non-windowing ssh client (e.g. putty), this will not work, as the client will be unable to launch a new window.

: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 reassemble.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

:n jump to line number n.

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 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 Past 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

Frequently asked 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.