Read in 3 minutes
How to Save a File in Vim / Vi and Quit the Editor
Vim is the text editor of choice for many users that spend a lot of time on the command line. Unlike other editors, Vim has several modes of operation which can be a little intimidating for new users.
Vim or its precursor Vi comes preinstalled on macOS and almost all Linux distributions. Knowing the basics of Vim will help you if you encounter a situation where your favorite editor is not available.
In this guide, we will explain how to save a file in Vim / Vi and quit the editor.
When you launch the Vim editor, you’re in command mode. In this mode, you can use vim commands and navigate through the file.
To be able to type text you need to enter into insert mode by pressing the
i key. This mode allows you to insert and delete characters in the same way you do in a regular text editor.
To go back to command mode from any other mode, just press the
Open a File in Vim / Vi
To open a file using Vim type
vim followed by the name of the file you want to edit or create:
Another way to open a file is to start the editor and type,
:e file_name, where
file_name is the name of the file you want to open.
Save a File in Vim / Vi
The command to save a file in Vim is
To save the file without exiting the editor, switch back to command mode by pressing
:w and hit
There is also an update command
:up which will write the buffer to the file only if there are unsaved changes.
To save the file under a different name, use
:w filewname and hit
Save a File and Quit Vim / Vi
The command to save a file in Vim and quit the editor is
To save the file and exit the editor simultaneously, press
Esc to switch to command mode, type
:wq and hit
Another command to save a file and quit Vim is
:x which will write the buffer to the file only if there are unsaved changes.
Quit Vim / Vi without Saving the File
To exit the editor, without saving the changes, switch to command mode by pressing
:q! and hit
In this guide, we have shown you how to save a file in Vim and exit the editor. If you are new to Vim visit the Open Vim site where you can practice Vim with an interactive tutorial.
Feel free to leave a comment if you have any questions.