How to Save a File in Vim / Vi and Quit the Editor
Introduction
Vim (Vi IMproved) is a highly configurable text editor that can be operated efficiently using keyboard commands.
For many users who spend a lot of time on the command line, Vim is the text editor of choice. Vim, unlike other editors, has a variety of modes of operation, which might be scary to newcomers.
On macOS and practically all Linux distributions, Vim or its predecessor Vi comes preinstalled. Knowing the fundamentals of Vim will come in handy if you find yourself in a circumstance where your preferred editor is unavailable.
In this tutorial, you will save a file and exit Vim / Vi. We will also address a few FAQs on how to save a file and exit Vim / Vi.
Vim Modes
You're in regular mode when you first open the Vim editor. You can traverse through the file using vim commands in this mode.
By pressing the i
key, you can enter the insert mode and type text. You can insert and remove characters in this mode just like you would in a standard text editor.
Simply use the Esc
key to return to regular mode from any other mode.
Open a File in Vim / Vi
To use Vim to edit or create a file, open your terminal and type vim
followed by the name of the file you want to edit or create:
vim file.text
To open a file, open the editor and type :e file_name
, where file_name
is the file's name.
Save a File in Vim / Vi
In Vim, the command to save a file is :w
.
Switch back to the regular mode by clicking Esc
, then type :w
and push Enter
to save the file without closing the editor.
- Press
Esc
- Type
:w
- Press
Enter
There's also an update command :up
, which only writes the buffer to the file if there are changes that haven't been saved yet.
Type :w new_filename
and hit Enter
to save the file with a new name.
Save a File and Quit Vim / Vi
The :wq
command in Vim saves a file and exits the editor.
To save the file while also exiting the editor, use Esc
to return to normal mode, then type :wq
and press Enter
.
- Press
Esc
- Type
:wq
- Press
Enter
:x
is another command that saves a file and exits Vim.
The difference between these two commands is that :x
writes the buffer to the file only if there are any unsaved modifications, whereas :wq
writes the buffer to the file all of the time and updates the file modification time.
Quit Vim / Vi without Saving the File
Switch to the regular mode by clicking Esc, then type :q!
and press Enter
to exit the editor without saving your changes.
- Press
Esc
- Type
:q!
- Press
Enter
FAQs to Save a File in Vim / Vi and Quit the Editor
Can I save a file with a different name in Vim/Vi?
Yes, you can save a file with a different name by using the command :w new_filename
and pressing Enter.
What if I want to save and quit Vim/Vi at the same time?
You can use the command :wq
or :x
to save the file and quit the editor.
How can I force-save a file without confirmation in Vim/Vi?
To force-save a file without confirmation, use the command :w!
and press Enter.
What is the difference between saving and quitting Vim/Vi?
Saving a file writes the changes to the disk, while quitting Vim/Vi exits the editor without saving any unsaved changes.
How do I quit Vim/Vi without saving any changes?
You can use the command :q!
or :qall!
to quit Vim/Vi forcefully without saving any unsaved changes.
Can I save and quit multiple files simultaneously in Vim/Vi?
Yes, you can save and quit multiple files by typing :wqall
or :xa
and pressing Enter.
How can I save a file in Vim/Vi and simultaneously view the file listing?
You can save a file and view the file listing by using the command :w | :ls
and pressing Enter.
Conclusion
We've taught you how to save a file in Vim and exit the editor in this tutorial. If you're new to Vim, go to the Open Vim site to learn how to use it through an interactive tutorial.
If you have any queries, please leave a comment below and we’ll be happy to respond to them.