How to save a file in Linux after using the vi command

Updated: 01/24/2018 by Computer Hope
vi command

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:

<Esc>:w<Enter>
  1. Pressing Esc exits any editing mode.
  2. The colon (":") begins command-input mode.
  3. The lowercase w stands for "write file".
  4. 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.

<Esc>:w file name<Enter>

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:

<Esc>:w!<Enter>

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

<Esc>:w! file name<Enter>
Note

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