How do I save a file in Linux after using the 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:
- 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
<Esc>:w file name<Enter>
Overwriting a read-only file, or a file that already exists
Using an exclamation mark also confirms you want to "save as" over a file that already exists. For example:
<Esc>:w! file name<Enter>
You can only overwrite a file if you have the proper permissions to do so.