The vi tip sheet
Unlike most word processing software, the vi editor uses two different modes:- command mode (also called edit mode)
- insert mode
The commands you type in command mode
- allow you to alter text -- to fix typing errors, add or subtract lines, etc.
- are not usually printed on the screen
- do not usually require you to press the RETURN key after you type them
- are "case-sensitive," which means there is a difference between upper- and lowercase letters.
To add text to your file, use a vi command such as i (insert). What you type in insert mode
- becomes part of the file you are editing
- requires you to press the RETURN key to begin a new line of text.
To return to command mode, press the ESCAPE key.
Command mode commands
File saving and Loading
| :e filename | edit new file filename |
| :r filename | read in contents of filename |
| :q | quit editing |
| :q! | quit editing and discard changes |
| :wq | write file and quit |
| :w filename | write to the file named filename |
| :w | write to the file already named |
| :w! filename | overwrite the file filename |
Insert mode commands (text entry)
| a | append text, after the cursor position |
| A | append text to end of the line |
| i | insert text, before the cursor position |
| I | insert text at the beginning of the line |
| o | open new line below cursor, enter insert mode |
| O | open new line above cursor, enter insert mode |
| R | enter overstrike mode, press the ESCAPE key to exit |
Press the ESC key to exit insert mode and return to command mode.
Search commands
| /text | search forward for first occurrence of text |
| ?text | search backward for first occurrence of text |
Cursor control commands
| b | go to beginning of word |
| e | go to end of word |
| G | go to the last line of the file |
| ##G | go to the ##th line of the file |
| h | move left one character |
| j or RETURN | move down one line |
| k | move up one line |
| l or SPACEBAR | move right one character |
| { | move to the previous paragraph |
| } | move to the next paragraph |
| $ | move to the end of the line |
| ^ | move to the beginning of the line |
Basic text manipulation commands
| dd | delete one line (place in general buffer) |
| dj | delete current line and one below |
| d} | delete up to the end of the paragraph |
| D | delete the rest of the line |
| J | join two lines |
| r | replace character on which the cursor rests |
| s | enter insert mode to substitute text for the character on which the cursor rests |
| ##s | substitute ## characters, enter insert mode |
| u | undo the last change |
| x | delete one character |
| ##x | delete ## characters |
Advanced text manipulation commands
| cw | deletes word starting at the cursor, enter insert mode |
| yy | yank (copy) line into the general buffer |
| ##yy | yank ## lines into general buffer |
| ##yl | yank ## characters to left of cursor |
| "ay | yank line into the buffer named "a" |
| p | put contents of general buffer after current line |
| P | put contents of general buffer before current line |
| "ap | same as p but from buffer "a" |
| "aP | same as P but from buffer "a" |
| > | shift line right |
| < | shift line left |
Miscellaneous commands
| CONTROL-G | print information about file: name, current line number |
| :## | go to the line numbered ## |
| :%s/pattern/text/ | substitute first occurrence of pattern on each line with text |
| :%s/pattern/text/g | substitute every occurrence of pattern with text |
| :g/pattern/d | delete every line containing pattern |
| :10,50 ya | yank the contents of lines 10 through 50 and place in the general buffer |
| :%!sort | execute the UNIX command sort on every line, replace contents with output of command |
