8. - Commands in Colon-Prompt Mode

If you're in moving-around mode and you wish to change to "colon-prompt" mode to enter commands appropriate to that mode, pres the colon key. A colon will appear at the bottom of the screen, and you'll be positioned to type commands at this colon.

The most common commands are those which involve saving the file to disk and quitting the editor. vi(1) operates on an automatically generated copy of the file, not on the file itself. Changes that you make during the editing session are not written to disk until you write them with ":w". You may write the file at any time, and may write it repeatedly; if you're editing over an unreliable network connection, frequent writes are often a good idea. If you wish to quit the editor (having made no changes or having previously saved your changes), use the ":q". If you have made changes to the copy of the file, but wish to quit without saving them, do this with the ":q!" command. To write and quit, use ":wq" command.

(Sometimes, whether intentionally or by accident, you will edit a file on which the write permission bit isn't set. vi(1) won't let you write this file with the normal ":w" command. If you have write permission in the directory, you can do one of two things: either change the write permissions for the file using the chmod(1) command from another terminal session, or use the vi(1) ":w!" command to force writing. Of course, if you have neither write permission on the file nor write permission in its directory, vi(1) will not let you write the file.)

After the command completes, you are automatically returned to moving-around mode (unless, of course, you just quit the editor).

It is sometimes useful to edit a second file without quitting the current session. (This is useful when copying between files using buffers, as described later in this volume.) This is done with the ":e" command. If there are unsaved changes, you will not be permitted to edit the new file until they are saved (with ":w"). The ":e!" command forces the editing of a new file and discards any unsaved changes.

Summary: Simple Commands in Colon-Prompt Mode
Key or Keys Action
:q quit (usually then warned if unsaved changes present)
:q! force quit, discard unsaved changes
:w write changes to file
:w! force writing (override non-write permissions if possible)
:wq write changes to file and quit the editor
:e [filename][enter] edit a new file without quitting
:e! [filename][enter] edit a new file without quitting (discard unsaved changes)