The vi editor has two modes: Command mode, which is used for entering commands Edit mode, which is used to type text The a, A, i, I, o, O, c, C, and R commands put vi into edit mode. To return to command mode from edit mode, press Escape. When you are in edit mode, you will see the word INSERT at the bottom left of the Terminal window. The following table shows file-handling commands and their descriptions: Command | Description |
---|
vi file | Edits the file named file. If the file does not exist, it will be created during a save. | vi -R file | Open file file in read-only mode. | :w | Write buffer to current file. | :w file | Write buffer to new file named file. Similar to a Save As. | :e file | Open file file for editing from within vi. | :q | Quit editing session. Will not quit unless changes have been saved. | :q! | Quit editing session. Will quit whether changes have been saved or not. |
Keying a number before many of the navigation commands augments their behavior, as described in this table: Command | Description |
---|
:num | Move to line number num. | G | Move to the last line of the buffer. | 0 | Move to the beginning of the current line. | $ | Move to the end of the current line. | Arrow keys | Move the cursor in the direction of the key. | h j k l | Move the cursor. h and l are left and right; j and k are down and up. | w | Move to the beginning of the next word. | num | Move to column num in current line. | Control-F | Scroll forward one screen length. | Control-D | Scroll forward 1/2 screen. | Control-B | Scroll backward one screen length. | Control-U | Scroll backward 1/2 screen. |
The following table shows miscellaneous commands and their descriptions: Command | Description |
---|
Escape | Return to command mode. | Control-G | Display current line number. | . | Repeat last command. | J | Join current line with next line. | :shell | Bring up a shell from within vi. Type exit to exit the shell and return to vi. | :!shell_command | Execute shell_command. Returns to vi when shell_command has completed executing. |
The following table shows how to set various preferences. Command | Description |
---|
:set all | Display the current preferences settings. | :set showmode | Show or do not show editing mode. | :set noshowmode | | :set list | Display or do not display control characters in file. | :set nolist | | :set number | Display or do not display line numbers. | :set nonumber | | :set ignorecase | Ignore or do not ignore case when searching. | :set noignorecase | |
Keying a number before many of the editing commands augments their behavior: Command | Description |
---|
x | Delete the current character. Deleting using this command places the deleted character in the yank buffer. | i | Start inserting text at the cursor position. | I | Start inserting text at the beginning of the first nonblank character in the current line. | a | Start appending text after the cursor position. | A | Start appending text after the last character in the current line. | o | Open a line following the current line and enter insert mode. | O | Open a line above the current line and enter insert mode. | rchar | Replace the current character with char. | R | Enter replace mode, overwriting all characters. | cw | Change the current word to whatever text follows. Changing using this command places changed characters in the yank buffer. | C | Change all characters from cursor to end-of-line. Changing using this command places changed characters in the yank buffer. | >> | Right indent. | << | Left indent. | :r file | Insert file file at cursor position. | :r !shell_command | Insert the output of the shell command shell_command at the cursor position. | dw | Delete to the end of the current word. Deleting using this command places deleted characters in the yank buffer. | dd | Delete the current line. Deleting using this command places deleted lines in the yank buffer. | D or d$ | Delete to end of line. Deleting using this command places deleted characters in the yank buffer. | u | Undo the last command that changed the buffer. | U | Restore the current line to its original state. Original state is defined as the state of the line when the cursor first moved to the line. When the cursor moves off of a line, the change is not undoable with this command; try the u command instead. | yw | Put the current word into the yank buffer. | "letter yw | By preceding yw with a quote and a letter, you can yank to the letter buffer. letter must be lowercase az. | yy | Put the current line into the yank buffer. | "letter yy | By preceding yy with a quote and a letter, you can yank to the letter buffer. letter must be lowercase az. | p | Paste text from the yank buffer at the cursor position. | "letter p | By preceding p with a quote and a letter, you can paste from the letter buffer. letter must be lowercase az. |
Should there be an intro sentence here, too? Search/Replace Command | Description |
---|
/pattern | Search forward of the cursor for pattern. | ?pattern | Search backward from the cursor for pattern. | n | Repeat last find in the same direction. | N | Repeat last find in the opposite direction. | :x,ys/old_string | Substitution range is lines x through y. In place of x,y, % may be used | /new_string/flags | to signify entire buffer as substitution range. With no x,y, substitution range is the current line. If flag c is used, confirmation is requested for each substitution. If flag g is used, all occurrences of old_string over substitution range are replaced; otherwise, only the first occurrence of each line is replaced. |
|