How to use the VI Editor?

There are 3 modes in vi:

  • Command mode
  • Input mode
  • Ex mode

Command mode

By default vi begins in command mode. Regardless what mode you're in, you can always press Esc to return to command mode. Below are the commands that can be made in command mode:

- h left
- j down
- k up
- l right
- x Delete the character the cursor is on
- dd Delete the current line
- u Undelete a line you just deleted
- Ctrl-b Go back one page
- Ctrl-f Go forward one page
-Shift-u Undo all changes to the current line
- Shift-z-z Save your file and exit the vi editor
- Shift-x Delete the character before the cursor
- Shift-d Delete everything from the cursor to the end of the line

Input mode

The input mode lets you insert text. To initiate text mode in command mode, press i. From here you'll be able to add text or edit whatever file you're viewing.

Ex mode

The ex mode is an extension of the command mode.

To initiate ex mode press Esc and then enter colon :
The cursor will go to the bottom of the screen at a colon prompt.

  • To write your file, enter :w
  • To quit enter :q
  • If you combine the two you can simultaneous save and exit the file :wq
  • You can append the command with an exclamation point ! to make it absolute.