Vim Command Reference

Last modified: 2012.01.01 by Patrick Kelley


Overview

Remember that you begin in COMMAND MODE. The following commands are available in COMMAND MODE. All commands are case sensitive.

Inserting
These commands will take you into TEXT-EDIT MODE. To return to COMMAND MODE hit the [ESC] key.
 i Insert text before the current character.
 a Append text after current character and before succeeding chars.
 I Insert text at the beginning of the current line.
 A Append text at the end of the current line.
 R Replace - overwrite existing text with new text.
 o Open (lower) - create a blank line after the current line and begin inserting text into it.
 O Open (upper) - Create a blank line before the current line for new text.
 s Substitute - delete character under cursor and begin inserting text. "4s" means substitute 4 chars.
 cw  Change Word - deletes the word under the cursor and allows you to type in a replacement word.


Delete, Cut & Paste
 x Deletes a single character under the cursor.
 5x Deletes 5 characters starting with the one under the cursor.
 dd Deletes a whole line of text.
 r Replace the current character with the next char you type.
 d$ Delete text from the current cursor position to the end of the line.
 d0 (d-zero) Deletes all characters from the beginning of the current line up to the current cursor position.
 yy (Yank) Copies the current line into memory.
 4yy Copies 4 lines, beginning with the one under the cursor.
 p Paste previously deleted or yanked text, inserting it after after the current cursor position.
 J Joins the next line to the end of the current line.
 v Visual (lower) - starts visual highlighting mode per character; characters selected by cursor movement can be deleted with "d", copied with "y", changed with "c", etc.
 V Visual (upper) - starts visual highlighting mode per line; selected lines can be copied, deleted, saved with ":w filename", or filtered through a Unix command (such as "cat -n") with "!" - for example, "!cat -n" (Unix/Linux).
 ^v (Control+v) Starts a visual block. A highlighted rectangular block of text can be deleted with "d", copied with "y", be altered on all lines with "s", have letters inserted in front of the highlight on each line with "I", or have letters appened to the end the highlight on each line with "A".


Miscellaneous
 . (Dot) Repeats the last command that did not start with a colon. For example, typing I[SPACE][SPACE][ESC] will insert two spaces at the beginning of a line (& indent the line). Moving the cursor down and typing . (dot) will repeat that command and indent the next line too.
 2 (Number) The next command you type will be performed 2 times instead of 1. To delete 26 lines, type "26dd".
 u Undo the last editing command. The undo command may be repeated as many times as desired (see also "Configuring Vim" below).
 ^r (CTRL+r) Redo the last editing command that was undone with "u".
 qa Start recording macro "a" (this can be "b..z" as well). Any keystrokes you type are remembered.
 q Stop recording macro.
 @a Play back macro "a" as if you just typed the keystrokes all over again.
 @@  Play back the most recently played macro again.


Moving the Cursor
 ←,↓,↑,→
 h,j,k,l
(Arrow keys) Move the cursor around. h, j, k, and l can be used instead, and don't require your hand to be lifted off the keyboard.
 :5 Move the cursor to line 5.
 w Move to the next word
 b Move back to the previous word
 0 (Zero) Moves the cursor to the beginning of the current line.
 $ Moves the cursor to the end of the current line.
 gg Move the cursor to top of the file.
 G Move the cursor to the bottom of the file.
 ^G (Control+g) Tells you what line number the cursor is currently on.
 /pattern Move the cursor forward (search) until the word pattern is found. Type ":help usr_27" and ":help pattern" for information on using regular expressions in your searches. For example, using a regular expression you can search for words that start with a capital letter and end with a number.
 ?pattern Perform a reverse search for the word pattern.
 n Find the next occurrence of the previous search. Searches forward if the prior search was "/" or in reverse if it was "?".
 :%s/pat1/pat2/g Replaces all occurrences of pat1 with pat2. For example, to change all occurrences of the word "srcx" with the word "src_x" in the file, type ":%s/srcx/src_x/g". If you are in visual mode (v, V, or ^v), you can type ":s/pat1/pat2/g" to search and replace within the highlighted text (appears on screen as if you typed ":'<,'>s/pat1/pat2/g").


Saving
 :w (Write current) Save the current file.
 :w filename (Write as) Saves the file, calling it "filename" instead of what it's currently called. Unlike the "Save As" in most programs, you are still editing the original file after you give this command, not the new file. Use ":e" to switch to the new one.
 :q Quit Vim. You will be unable to quit if the file has unsaved changes (see ":q!").
 :q! (Forced Quit) Quit vim even if the file hasn't been saved (change are lost).
 :wq (Write + Quit) Save and quit at the same time.
 :e filename Switch to editing the specified file. You can type part of the filename and hit [TAB] to cycle through files that match the partial name. You must save first (":w") or else use ":e! filename" to discard changes to the current file.
 :sp filename (Split) Use a splitscreen view to edit both the current file and the named file. Any number of files may be split over the same view using multiple consecutive "sp" commands. Use the mouse or CTRL+w plus an arrow key to move the cursor between the windows. Type ":help sp" for more info.
 :r filename (Read) Inserts contents of file "filename" at current cursor pos.


Configuring Vim
Vim gets its configuration values from a file called "_vimrc" (Windows) or ".vimrc" (Unix and Linux).

Here's an example configuration file that may be useful to you. Save it with the appropriate name (as mentioned above), or cut and paste interesting bits into your existing vimrc: vimrc.txt

Afterwards, you can edit this file in Vim itself by typing:

      gvim "c:\Program Files\Vim\_vimrc" (Windows), or
      vim ~/.vimrc                       (OS X, Unix, Linux)

to edit the configuration file.

Note: you should definitely create a .vimrc file if you're using Vim on our Unix computer. On our Unix computer, Vim reads some defaults from a system configuration file. One of these defaults limits it to a single level of undo, where Vim actually supports effectively infinite undos. To enable infinite undos on Unix, all you have to do is create a .vimrc configuration file, even if it's empty.