Written by Chris Gregg and Dominique Yahyavi, with modifications by Peter Johnston and Nick Troccoli
Click here for a walkthrough video.
vim is a text editor that is a clone of the vi editor, which was created around 1976. Despite its age, vim is one of the most popular editors in the world, and it is available on virtually every computing platform (or vi is available). vim has a bit of a learning curve, meaning that you may get somewhat frustrated with it until you have used it a few times.
Getting Started
Before you start using vim, you'll want to configure it to use the CS107 default preferences; this sets up the editor to do things like display line numbers, standardize indentation, etc. To do this, execute the following command after logging into myth:
wget https://cs107.stanford.edu/resources/sample_vimrc -O ~/.vimrc
vim looks for a special .vimrc file on your system to know what preferences you would like, and this command downloads our pre-made .vimrc file and puts it on your system.
Overview
vim has two "modes": COMMAND mode and INSERT mode. In COMMAND mode, you execute commands (like undo, redo, find and replace, quit, etc.). In INSERT mode, you type text. To go into INSERT mode from COMMAND mode, you type i. To go back to COMMAND mode, you type the esc key. vim starts out in COMMAND mode. Over time, you will likely spend more time in COMMAND mode than INSERT mode. Note that all commands mentioned below (except for arrow keys) only work in COMMAND mode.
Opening vim
To open a file in vim (or create a new one if a file with this name does not exist):
$ vim filename
If vim is already open and you would like to edit a different file, use the :e filename command (e.g. :e myOtherFile.c). 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.
Saving and Quitting vim
: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.
:q quits 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 save and quit.
Navigating A File
Arrow keys work in both modes to navigate as you would expect. The preferred mode in vim, however, is to use the j``k``h``l keys instead in COMMAND mode (these do not work in INSERT mode), which allows users to keep their fingers on the home row to move around the screen. Here's how they work:
j: down
k: up
h: left
l: right
There are also several additional keyboard shortcuts to navigate within a file.
w move to the next word
b move to the beginning of the word ("beginning")
e move to the end of the word ("end")
B move to the previous word ("Back")
0 move to the beginning of the line
$ move to the end of the line
:123 jump to line number 123.
123G jump to line number 123 (equivalent to :123)
Ctl-f page down ("forward")
Ctl-b page up ("back")
gg jump to the first line in file
G jump to last line in file
Searching
Search is another great way to move your cursor.
/foo search the file for "foo". This will highlight all matches in the editor for you to see.
n move the cursor to the next search match
N move the cursor to the previous search match
Editing Text
dd or :d deletes the current line
yy or :y or Y "Yank" (cut) 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.
x delete the character underneath the cursor
Nx delete the N characters under and following the cursor (e.g. 3x deletes the 3 characters under and following the cursor).
Entering INSERT Mode
When you are ready to enter text, you should switch to INSERT mode. There are a few ways to do this:
i enter INSERT mode before the cursor
I enter INSERT mode at the start of the current line
a enter INSERT mode appending after the cursor
A enter INSERT mode appending to the end of the current line
Then, just type as normal to enter text. You can use the arrow keys to navigate, or switch back to COMMAND mode and use the shortcuts mentioned earlier.
Undo/Redo
u undo the last action
U undo all recent changes made to the current line
ctrl-r redo
More Resources
The best way to get familiar with vim is to just start using it to edit files - type something, anything! Over time, you'll become more comfortable with the standard commands, and pick up more advanced ones. If you are looking for more references for how to use vim, check out the builtin vimtutor tutorial program on myth to help you learn vim. Run it by doing the following:
$ vimtutor
There is also a fun online vim tutorial, called Vim Adventures that can give you practice with the basic commands as well.
Frequently Asked Questions
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 the 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).