Written by Chris Gregg and Dominique Yahyavi, with modifications by Peter Johnston, Nick Troccoli, and Lisa Yan
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.
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. There is a third mode, VISUAL mode, that is used to highlight and edit text in bulk.
- To go into INSERT mode from COMMAND mode, you type
i
. To go back to COMMAND mode, you type theesc
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 (or you can also use :x
to do the same thing in one fewer keystroke)
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 previous word ("Back")
b
move to the beginning of the word ("beginning"). e
move to the end of the word ("end")
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")
Ctl-u
half a page up. Ctl-d
half a page down.
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
:noh
to turn off highlighting until the next search. Alternatively, search for a bogus string, like /thiswordwontexist
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. To paste above the current line, use P
.
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
Highlighting text
If you want to highlight multiple lines or particular words in a line, you can do so in VISUAL mode. There are three different visual modes:
v
enters character-wise visual mode to highlight one character at a time.
V
enters line-wise visual mode to highlight entire lines at a time.
ctrl-v
enters block-wise visual mode to highlight over rows and columns, etc.
Once you enter these modes by typing these in COMMAND mode, you can use the keyboard navigation (e.g., hjkl
or w
or b
) to highlight text). Then you can batch delete (d
), yank (y
), or paste (p
) whatever is highlighted. To exit VISUAL mode, use esc
.
VISUAL mode is also useful for adjusting indent to a batch of text. For example, if you wanted to reduce the indent of a set of lines, you can enter block-wise visual mode (ctrl-v
) to select a vertical column of indents (using keyboard navigation), and then batch delete with d
. To increase the indent of a set of lines, enter block-wise visual mode and select a vertical column, and then batch insert at the beginning of this block with I
, type in your indentation, and then use esc
. You should see all highlighted lines get the same inserted space.
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, hitd
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).