vi is a Visual Editor (hence the name -- vi for VIsual). vi is a widely-used and popular UNIX-based text editor. Like most UNIX system interfaces and other text editors, it lets you control the system by using the keyboard rather than a combination of mouse selections and keystrokes.
Edit a File
| Edit a file from command prompt | |
| Edit a file from command prompt for reading only | vi -R |
| Edit a file from within vi | :e |
| Edit a new file from within vi, discard changes to current file | :e! |
| Reload current file, discarding changes | :e! |
| Go forwards a page | Ctrl+F (or PgDn) |
| Go backwards a page | Ctrl+B (or PgUp) |
| Move around single lines or characters | Arrow keys |
| Save changes | :w |
| Save changes and override protected (read-only) files | :w! |
| Save changes and exit vi | ZZ |
| Quit | :q |
| Quit and discard changes | :q! |
| Get general help | :help |
| Get help on a command (eg. :set) | :help set |
Go to lines, find matching text
| Go to line 1234 (do not see typing) | 1234G |
| Go to line 1234 (see typing) | :1234 |
| Go to start of file | 1G |
| Go to end of file | G |
| Find (forwards) a line containing "swordfish" | /swordfish |
| Find (forwards) a line using a regular expression | /you see .* here |
| Repeat last search | n |
| Repeat last search in opposite direction | N |
| Find (backwards) a line containing "swordfish" | ?swordfish |
| Find (backwards) a line using a regular expression | ?you see .* here |
| Search case insensitive (Ignore Case) | :set ic |
| Search with case sensitivity | :set noic |
| Wrap searches back to start of file | :set wrapscan |
| Do not wrap searches | :set nowrapscan |
Insert Text
| Insert after cursor | i |
| Insert before cursor | a |
| Insert at beginning of line | I |
| Insert at end of line (append) | A |
| Open (start) new line below cursor | o |
| Open (start) new line above cursor | O |
Advance Deletion
| Delete next 5 lines | 5dd |
| Delete to end of line | d$ |
| Delete to start of line | d0 |
| Delete to word "swordfish" | d/swordfish |
| Delete to the letter "x" | dfx |
| Delete lines 10 to 20 | :10,20d |
| Delete current line and another 5 lines | :.,+5d |
| Delete all lines | :%d |
| Delete current word | dw |
| Find line containing pattern, delete it | :/pattern/d |
| Find line containing "dog", delete until line containing "cat" | :/dog/,/cat/d |
| Delete from current line to line containing "foo" | :.,/foo/d |