vi Editor [Command] [How To]
Basic vi commands
| Command | Description |
|---|---|
| TERM = terminal_name export TERM |
Setting Terminal configuration |
| tput init | Initialize terminal as terminal_name |
| vi filename | Open the vi text editor to edit the file filename |
| a | Add text after the cursor |
| h | Moves the cursor one character left |
| j | Moves the cursor down one line |
| k | Moves the cursor up one line |
| l | Moves the cursor one character right |
| x | Delete character |
| Carriage return | |
| Back from the input to command mode | |
| :w | Write to the file |
| :q | Quit Editor |
| :wq | Write buffer to the file and exit the vi editor |
| ZZ | Save changes to the file and exit from vi |
Cursor positioning commands
| Command | Description |
|---|---|
| Symbol positioning | |
| h | Moves the cursor by one character to the left |
| l | Moves the cursor one character right |
| BACKSPACE | Moves the cursor one character left |
| SPACEBAR | Moves the cursor one character right |
| fx | Searches the specified character x from the right of the cursor |
| Fx | Searches the specified character x from the left of the cursor |
| tx | Moves the cursor on the right symbol of the specified character x |
| Tx | Moves the cursor on the left symbol of the specified character x |
| ; | Repeats previous search symbol. The command ; remembers the character and looking for the next occurrence of character in the current row |
| , | Repeats previous search for a character, but in the opposite direction |
| j | Move the cursor down one line |
| k | Moves the cursor up one line |
| – | Moves the cursor up one line, positioning it at the first symbol |
| + | Moves the cursor down one line, positioning it at the first symbol |
| Moves the cursor down one line and positioned on the first non-empty character | |
| Line positioning | |
| $ | Move the cursor to the last character in the string |
| 0 (zero) | Move cursor to first character in string |
| ^ | Move the cursor to the first nonzero character in |
| Word positioning | |
| w | Moves the cursor to the beginning of next word |
| W | Moves the cursor to the next word after space (ignores all characters except space) |
| e | Moves the cursor to the last character in the next word . |
| E | Moves the cursor to the last character in the word (ignores all characters except space) |
| b | Moves the cursor to the first character of the previous word |
| B | It is used like a command b, except that the words are separated by only space and a new line |
| Sentence positioning | |
| ( | Moves the cursor to the beginning of the current sentence |
| ) | Moves the cursor to the beginning of the next sentence |
| Paragraph positioning | |
| { | Move the cursor to the beginning of this paragraph, which is limited by an empty string |
| } | Move the cursor to the start of the next paragraph |
| Positioning in window | |
| H | Move the cursor to the first line of the screen |
| M | Move the cursor to the middle line of the screen |
| L | Move the cursor to the last line of the screen |
Moving the cursor in the vi editor
| Command | Description |
|---|---|
| Scrolling | |
| ^f | Scrolls the screen forward to the full window. Opens new window with the text located below the current window |
| ^d | Scroll text down on the half-windows. Opens the lines below the current window |
| ^b | Scrolling the screen back to full window. Opens new window with the text located above the current window |
| ^d | Scroll text up a half window. Opens lines located above the current window |
| Position with the line number | |
| 1G | Go to the first line in file |
| G | Go to the last line in the file |
| ^g | Show a line number and state of the file |
| Search pattern | |
| /pattern | Search forward in the buffer to the first appearance of the pattern.The cursor is placed on the first chapter of pattern |
| ?pattern | Search back in the buffer before the first appearance of the pattern.The cursor is placed on the first chapter of pattern |
| n | Repeat the last search |
| N | Repeat last search command in reverse |
Commands creating the text
| Command | Description |
|---|---|
| a | Write the text after the cursor |
| A | Write a text at the end of the current row |
| i | Write the text before the cursor |
| I | Write text before first non-zero symbol in the current row |
| o | Write the text from the beginning of a new line below the current row |
| O | Write the text from the beginning of a new line, located above the current row |
| Return the vi in command mode from text mode |
Commands deleting the text
| Command | Description |
|---|---|
| Input mode | |
| BACKSPACE | Delete current character |
| 1w | Delete current word |
| @ | Delete the current line of the new text or delete all new text in the current line. Command mode |
| u | Undo the last command |
| U | Restore the current line in the previous condition |
| x | Delete current character |
| ndx | Delete the n-th object of the text x |
| dw | Delete the word above the cursor and the following space |
| dW | Delete the word and punctuation above the cursor followed by a space |
| dd | Delete current line |
| D D | Remove part of string to the right of the cursor |
| d) | Delete the current sentence from the current cursor position to the end |
| d} | Delete the current paragraph from the current position to the end |
Commands for changeing the text
| Command | Description |
|---|---|
| r | Replace the current character |
| R | ESCAPE Replace the characters are inserted in the ESCAPE mode |
| s | Delete the character and add text. To end the input mode, press ESCAPE |
| S | Replace all characters in the string |
| cc | Replace all characters in the string |
| ncx | Replace the n objects in text to type x (sentence, paragraph) |
| cw | Replace the word or the remaining characters in the word with the new text |
| C | Replace the remaining characters in the row from the cursor to end of line |
Command y
| Command | Description |
|---|---|
| nyx | Copy the n-th object of the text type x (sentence or paragraph) |
| yw | Creating a copy of the words |
| yy | Creating a copy of the current row |
| nyy | Copy n lines |
| y) | Creating a copy of the text to end of sentence |
| y} | Creating a copy of the text before the end of paragraph |
| p | Puts the contents of the temporary buffer, obtained by using the y |
Special commands
| Command | Description |
|---|---|
| . | Repeat the last command |
| j | Connect the current row with the row below |
| ^l | Clear and restore the current window |
| ~ | Switch between lowercase and uppercase |
Line editor commands
| Command | Description |
|---|---|
| : | Indicates that following it command is the command of line-editor |
| :sh | Temporary return to the shell to execute shell commands |
| ^d | Returning from a temporary shell in the current window to continue editing vi |
| :n | Go to the n-th row of buffer |
| : x, yw data | Write a string of numbers x to number y in the new file named “data” |
| :$ | Go to the last line of the buffer |
| :.,$d | Delete all rows in the buffer, starting from current to the last |
| :r shell.file | Paste the contents shell.file after the current line in buffer |
Commands to end work
| Command | Description |
|---|---|
| ZZ | Write buffer to file, if after the last entry in the buffer changes were made, and exit from vi |
| :wq | Write buffer to file and exit from vi |
| :w filename | Write buffer to new file “filename” |
| :q | Exit from vi |
| :q! | Quit vi without recording buffer to a file |
Special options of vi
| Options | Description |
|---|---|
| vi file1 file2 file3 | Enter the three files (file1 file2 and file3) in the buffer of vi for editing |
| :w | Save the current file and start |
| :n | Edit next file |
| vi -r file1 | Recover changes made in the file “file1” |
| view file | View the file using the “read only” to prevent accidental changes to the file “file “ |