VI Text Editor Reference Sheet Contents
Entering vi Movement Inserting Text Deleting Text Other Commands Saving and Exiting vi Some vi settings
Entering vi
vi file vi +n file vi -r file Open file for editing. Open file and go to line n. Recover a failed editing session.
Movement
backward forward character h l line k j word b w sentence ( ) paragraph { } 1/2 screen CTRL-U CTRL-D 1 screen CTRL-B CTRL-F The above commands can be prefaced by a number to indicate a desired repetition. For example, the command 5w moves the cursor five words to the right and the command 10k moves the cursor ten lines up. 0 Go to beginning of the current line. $ Go to end of the current line. nG Go to line n. (Default = last line)
Inserting Text
i inserts text before the cursor. a appends text after the cursor. A appends text at the end of a line. o opens a new (blank) line below the cursor and enters insert mode. O opens a new (blank) line at the cursor and enters insert mode. :r filename inserts the contents of filename at the current line. Pressing will exit insert mode.
Deleting Text
x deletes the character at the cursor. dw deletes the current word. dd deletes the current line. As with the movement commands, these commands can be prefaced by a number to indicate a desired repetition. The command 3dw deletes the next three words, for example.
Other Commands
. u /string n N :s/from/to :s/from/to/g :%s/from/to/g nY P p rX R finished). redraws the screen. redoes the last command. undoes the last command that changed the buffer. searches for string. searches for the next occurrence of stringi searches for the previous occurrence of string. replaces to for one from on one line. replaces to for every from on one line. replaces to for every from on every line. yanks a copy of the next n lines. (Default = 1) puts yanked or deleted line(s) at the current line. puts yanked or deleted line(s) after the current line. replaces character at cursor with X begin replacing text at cursor (press when
Saving and Exiting vi
The following is a list of saving and exiting commands. Each has its advantages and disadvantages. Decide which ones appeal to you and use them. :w save (don't quit) [:w filename saves to filename] :wq save and quit :q! quit (don't save) :x save if a change has been made, quit regardless ZZ same as :x
Some vi Settings
:set autoindent Automatically indent at the same level as the line above. :set number Display line numbers. :set showmode Indicate if vi is in insert mode. :set wrapmargin=n Automatically wrap word in last n columns. :set wrapscan Searches reaching the bottom continue from the top. To unset any of these options, use the prefix no. For instance, the command :set nowrapscan will turn off the wrapscan feature. To make settings permanent, place them in the file .exrc in your home directory.
vi Tutorial
Beginning Instructions:
VI is a relatively simple editor to use, once you know the basic commands. The most basic thing to remember is that there are really only two modes in VI - the "command mode," where you are able to tell the editor what you need it to do, and the "typing mode," where you are simply inputting the text that you want saved in your file. Any time that you want to get into the "command mode," all you have to do is hit the Esc key, or if your keyboard does not have one, the F11 key. Those keys will ALWAYS take you back to the "command mode." A list of the most commonly used commands is being put here for convenience. If you would like to look at more complex lists of commands, or have a use that is not covered here, please refer back to the Useful Web Sites listed on the page previous to this one.
To Move Cursor on Page:
0 move cursor to beginning of line $ move cursor to end of line + move cursor to first character of next line move cursor to first character of previous line (spacebar) move cursor to next character (return) move cursor to next line ^b scroll back one screen ^f scroll forward one screen ^d scroll down half a window ^u scroll up half a window G move cursor to end of file (arrow keys) move cursor up, down, left or right one position j move cursor down one position (next line) k move cursor up one position (previous line) l move cursor right one space h move cursor left one space
To Insert or Delete Characters, or Move Whole Lines:
a A D dd d#d i J o O p P r u append text one space after the current location of cursor append text at the end of the line delete from cursor position to end of line delete whole line that cursor sits on deletes # of lines specified insert text exactly at current location of cursor join next line with current line open line below current line and insert text open line above current line and insert text paste text just deleted with 'd' on line just below current line paste text just deleted with 'd' on line just above current line replace current character with next character typed undo last change you made
Command Mode Commands:
:r read in another file at cursor position :x write (save) and quit :wq write and quit :w write (save) without quitting :w! overwrite current file :q quit :q! quit, ignore any changes made in text :# moves cursor to line number # /writing searches ahead of cursor position for an instance of the word 'writing' in the file