Users who want to save a file in Linux after using the vi command have a few options. To proceed, you may look for the option that best suits your needs, or check out all three.

Saving a file

The command sequence for saving a file in the vi text editor is as follows:

  • Saving a file.
  • Saving as another file name.
  • Overwriting a read-only file, or a file that already exists.

:w

  • Pressing Esc exits any editing mode.
  • The colon (":") begins command-input mode.
  • The lowercase w stands for “write file”.
  • Pressing Enter executes the write command.

Saving as another file name

To perform a “Save as” operation, writing the text to a new file name, specify the new file name after the w, separated by a space. The command is displayed below.

:w file name

Overwriting a read-only file, or a file that already exists

To save a file marked as read-only, add an exclamation mark ("!") after the w, to indicate you want to overwrite the file:

:w!

Using an exclamation mark also confirms you want to “save as” over a file that already exists. For example:

:w! file name

You can only overwrite a file if you have the proper permissions to do so.

  • Unix help and command listing.
  • Guide to using the vi text editor.
  • Guide to using vim, the “improved” version of vi.
  • See our Unix definition for additional information.
  • Linux help and support.