How to Save a File in Vim / Vi and Quit the Editor?
Vim is an editor commonly used in Linux for editing. Vim stands for Vi Improved. It is an improved version of vi editor.
It is one of the most popular editors that can be used for text editing and coding as well since 1991. Vim comes preinstalled in all Linux distributions and macOS.
In this article, we will learn to save and exit file in Vim editor with some examples.
The key combination used to save and quit a file in vim editor. First, press Esc
and type the following:
:wq
After that press Enter
. This is a single-line solution for your questions. Let's get to dive into understanding the whole process.
To begin with, first, open the terminal by pressing Ctrl+Alt+t
from desktop and a terminal window will open in your system. Now, see the step-by-step process.
1. Open File with Vim Editor
To open a file in Vim editor, write vim fileName
command in the terminal and press Enter
. It will open a file that you have specified in the command.
If the file does not exist, then it will first create a file with the same name and then open the file.
vim fileName
The filename can be any text, HTML, or a doc file. So, we must specify the file with extension like file.txt
, file.xml
, file.log
, etc. Although it's not required.
2. Write into Vim Editor
After opening the file, we can modify the file by inserting text (writing).
To write into the file, press i
that convert the vim editor to insert mode, and here you can write to the file.
You can see the below screenshot where we insert a line and the editor mode can be seen at the bottom left, it is insert now.
3. Save into Vim Editor
After writing/inserting text, we can save the file by pressing the following keys in the same sequence:
-
Esc
-
: (colon)
-
w
-
Enter
First press Esc
to get back to normal mode from insert mode, then :
(colon) and w
to write changes to original file and lastly, press Enter
to execute the command.
It will save your file data into the vim editor. See the screenshot for help.
4. Save and Quit Vim Editor
If you want to quit the vim editor after saving the file, you can use this key combination:
-
Esc
-
:(colon)
-
w
-
q
-
Enter
First press Esc
, then :
(colon), w
and q
which stands for write and quit and last press Enter
.
It will save your file and quit the vim editor. See the below screenshot for help.
5. Quit Without Save Vim Editor
If we want to quit vim without saving the file, then you can use the following key combination:
-
Esc
-
: (colon)
-
q
-
!
-
Enter
First press Esc
, then :
(colon), q
, and !
to discard changes and quit, and last press Enter
.
It will quit the vim editor without saving your file. See the below screenshot for help.
Apart from saving and quitting the vim editor, you can do a lot more with this powerful editor. You can read our exiting article,
FAQs
Finally, here are some related questions that you might have related to Vim:
Q. How to save all files opened in Vim / Vi and quit the editor?
Use :wqa
or :xa
command in normal mode. This will write all modified files to their respective file names and exit the editor.
Q. How do I quit all files in Vim without saving them?
Use :qa!
command in normal mode. This will discard any changes made to all buffers and exit the editor.
Q. How to undo changes made to a file in Vim?
Simple use u
command in normal mode.
Q. How to redo changes made to a file in Vim?
You can redo (undone by u
) using the Ctrl+R
command in normal mode.
Q. How to open multiple files in Vim / Vi?
To open multiple files i.e, filename1
, filename2
, etc. with Vim
run the below command in your terminal.
Switch between files by using :bn
(for next file) and :bp
(for previous file) commands.
vim filename1 filename2 …
In this article, we focused to learn how to save and file and quite the vim editor after saving. We learned from scratch, to open vim editor, write text, save text, and quit after saving or without saving as well.