How to Save and Quit a File in Vim
As a beginner learning Vim, one of the essential features to know is how to save and quit a file. Vim is a customizable and powerful text editor, and it can take a while to master its features. However, saving and quitting a file is the most basic skill Vim users should learn.
Here, we will be using Ubuntu or any other Linux-based terminal.
Method 1: Save and Quit a File in Vim
Below are the simple steps to save and quit a file in Vim.
Step 1: Open the file in Vim
Open the file you want to edit using the Vim editor. You can open any file with the Vim editor by typing `vim` followed by the file name and pressing Enter.
“`vim filename.extension“`
Step 2: Edit the file
Once you have opened the file successfully, you can now edit the file as you wish.
Step 3: Saving the file
To save the changes made to the file, type `:w` or `:wq` and press Enter. The `:w` command means write, and it instructs Vim to save the changes made to the file. If the changes are successful, Vim will display “written” at the bottom of the screen.
“`vim
:w # To save the file
:wq # To write the changes and quit Vim
“`
Step 4: Quitting Vim
To exit Vim without saving any changes you made, type `:q!` and press Enter. The `:q!` command means quit without saving, and it instructs Vim to discard any changes and exit the file.
“`vim
:q! # To quit without saving changes
“`
Method 2: Save and Quit a File in Vim (Alternate method)
This method allows you to save and exit Vim using fewer keystrokes.
Step 1: Open the file in Vim
Open the file you want to edit with the Vim editor.
“`vim filename.extension“`
Step 2: Edit the file
Edit the file as you wish.
Step 3: Save and Quit
To save and exit the file in Vim, type `ZZ` and press Enter. The `ZZ` command automatically saves any changes made to the file and quits the Vim editor. If no changes were made, Vim will simply quit.
“`vim
ZZ # To save and quit Vim