
| k | Up one line | j | Down one line | h | Left one character | l | Right one character (or use |
w | Right one word | b | Left one word |
| i | Enter text entry mode | x | Delete a character | dd | Delete a line | r | Replace a character | R | Overwrite text, press |
| :set nu | Display line numbers | :set nonu | Hide line numbers |
| :set wm=number | Set Wrap Margin number of spaces from right edge of screen | :set wm=10 | Set Wrap Margin 10 spaces from right edge of screen | :set wm=0 | Turn off Wrap Margin |
| ZZ | Write (if there were changes), then quit | :wq | Write, then quit | :q | Quit (will only work if file has not been changed) | :q! | Quit without saving changes to file |
| To enter vi, type: vi filename |
To enter insert mode, type: i | Type in the text: This is easy. | To leave insert mode and return to command mode, press: |
In command mode, save changes and exit vi by typing: :wq |
You are back at the Unix prompt. |
| e | Move to end of current word | $ | Move to end of current line | ^ | Move to beginning of current line | + | Move to beginning of next line | - | Move to beginning of previous line | G | Go to last line of the file | :n | Go to line with this number (:10 goes to line 10) | | Scroll down one-half screen | | Scroll up one-half screen | | Scroll forward one full screen | | Scroll backward one full screen | ) | Move to the next sentence | ( | Move to the previous sentence | } | Move to the next paragraph | { | Move to the previous paragraph | H | Move to the top line of the screen | M | Move to the middle line of the screen | L | Move to the last line of the screen | % | Move to matching bracket: ( { [ ] } ) |
| i | Insert text before current character | a | Append text after current character | I | Begin text insertion at the beginning of a line | A | Append text at end of a line | o | Open a new line below current line | O | Open a new line above current line |
| Format | Example |
| operator number object | c2w |
| number operator object | 2cw |
| c | change | d | delete | y | yank |
| w | one word forward | b | one word backward | e | end of word | H, M, L | top, middle, or bottom line on screen | ), ( | next sentence, previous sentence | }, { | next paragraph, previous paragraph | ^, $ | beginning of line, end of line | /pattern/ | forward to pattern |
| r | Replace only the character under the cursor. (Note: using r you remain in command mode.) |
| R | Beginning with the character under the cursor,
replace as many characters on this line as you
want. (You are in overtype mode until you
press |
| cw | Beginning with the character under the cursor,
change a word to whatever you type. (You are
in insert mode until you press |
| c$ | Beginning with the character under the cursor, |
| C | change a line to whatever you type. (You are
in insert mode until you press |
| x | Delete a character |
| dw | Delete an alphabetic word and the following space (6dw deletes six words) |
| dW | Delete a blank-delimited word and the following space |
| dd | Delete a line (6dd deletes six lines) |
| d$ | Delete all characters to the end of the line. |
| d} | Delete all characters to the end of the paragraph. |
| :5,30d | Delete lines 5 through 30 |
| yy | Copy (yank) the current line |
| 6yy | Copy (yank) six lines, beginning with the current line |
| yw | Copy the current word |
| p | Put the text after the cursor position |
| P | Put the text before the cursor position |
| . | Repeat last command |
| n. | Repeat last command n number of times |
| J | Join next line to current line |
| u | Undo last single change |
| U | Restore current line |
| ~ | Change letter's case (capital to lower and vice versa) |
| p | Put words from temporary buffer after cursor or put lines from temporary buffer below current line |
| P | Put words from temporary buffer before cursor or put lines from temporary buffer above current line |
| "ayy | Copy (yank) a line into buffer a | "Ayy | Appends to buffer a | "a10yy | Copies 10 lines into buffer a | "a10dd | Deletes 10 lines of text into buffer a | "ap | Put contents of lettered buffer a below the current line |
| :5,10 co 105 | Copy lines 5-10 to the line after 105 | :5,20 m $ | Move lines 5-20 to end of file | :7,300 d | Delete lines 7-300 (to buffer) |
| /text | Search forward (down) for text (text can include spaces and characters with special meanings.) | ?text | Search backward (up) for text | n | Repeat last search in the same direction | N | Repeat last search in the opposite direction | fchar | Search forward for a charcter on current line | Fchar | Search backward for a character on current line | ; | Repeat last character search in the same direction | % | Find matching ( ), { }, or [ ] |
| n1 | is the beginning line | n2 | is the ending line number | s | means to substitute text matching the pattern (old) with text specified by (new) | g (global) | is optional. It indicates you want to substitute all occurrences on the indicated lines. If you use g, the editor substitutes only the first occurrence on the indicated lines. | c (confirm) | is optional. It indicates you want to confirm each substitution before vi completes it. |
| :%s/old/new/g | Substitutes old with new throughout the file | :.,$s/old/new/g | Substitutes old with new from the current cursor position to the end of the file | :^,.s/old/new/g | Substitutes old with new from the beginning of the file to the current cursor position | :& | Repeats the last substitute (:s) command |
| :w file | Write current file to file | :w>>file | Append current file to file | :5,10w file | Write lines 5 through 10 to file | :5,10w>>file | Append Lines 5 through 10 to file | :r file | Read a copy of file into current file | :!ls | See a list of files in your current directory |
| :set all | Display all options | :set | Display current settings of options | :set nooption | Unset option | :set ai | Set Auto Indentation during text entry | :set ic | Set Ignore Case during searches | :set nu | Show line Numbers | :set sm | Show Matching ( or { when ) or } is entered | :set wm=10 | Set Wrap Margin 10 spaces from right edge of screen |
| :n | Move to file2 from file1 | :rew | Rewind back to file1 | :e! | Restore original file1 file2 (start all over) | ZZ | Save and quit file. (Must be done for each file.) |
| :1,$> | Move entire file 1 shift width (eight spaces) to the right | :1,$< | Move entire file eight spaces to the left | :%s/^/ /g | Insert any number of spaces at the beginning of each line in the entire file. Simply press the space bar the desired number of times. | :20>> | Moves next 20 lines over 1 shift width. |
| :w | Write changes to your file (just in case). | :!ls | List contents of your current directory on the screen.
Press |
:w | Write changes to your file. | :sh | Return to the shell to enter a number of commands without leaving vi. |
| :w | original.backup Save a backup copy of the original file | :%! sed G | Double space the entire file. | :1,5! sed G | Double space the lines from 1-5. |