Exiting the vi editor

Yes, this page contains the way to exit the vi (or more modernly, vim) editor, just scroll to the end of this page. But please, before you just quit and forget the best text editor the GNU/Linux and UNIX platforms have to offer, please take a look at what you can do with it.
This page will use vim to refer to the editor since it is newer and on newer systems, vi is basically just vim in backwards compatibility mode.

Modes

First, you need to know that vim has several modes:

  • Normal
    For navigation and manipulation of text. This is usually the default mode after starting vim. Press Esc to revert to this mode.
  • Insert
    For inserting new text.
  • Visual
    For navigation and manipulation of text selections, this mode allows you to perform most normal commands -and a few extra commands- on selected text.
  • Select
    Similar to visual, but behaves more like Windows.
  • Command-line
    For entering editor commands - like the help commands in the 3rd column.
  • Ex-mode
    Similar to the command-line mode but optimized for batch processing.

Jump around

To navigate around a file.
h: Left one column
l: Right one column
j: Down one line
k: Up one line
:65: Jump to line 65
:1: Jump to line 1
gg: To jump to the start of a file
G: To jump to the end of the file

Other commands

%: When the cursor is on <, >, {, }, (, ), [ or ], go to the matching other characters
zz: Scroll the current screen so that your current line ends up vertically in the middle

Some nice vim hacks

:%TOhtml: Create an HTML version of your current document.
:%!python -m json.tool: To make a JSON file look somewhat readable

Finally, really exiting the vi editor

To exit the vim editor, first enter the Normal mode by pressing the Esc key, then enter the following:

:q!

This quits the editor, ignoring any changes made to the file.
To quit the editor and save your changes, use the following:

:wq