How to Save a File in Vi / Vim Editor & Quit
Last Updated :
25 Sep, 2023
In this article, we will cover how to save a file in vi editor. First, we look at Linux and Vi editors why we use them, and what their are features then we deep dive into the topic of saving a file and quitting the editor along with the other options, we understand the different commands with an example.
What is Linux?
Linux was developed by Linus Torvalds in 1991 as a hobby project. It is an open-source (source code that can be used by anyone freely) kernel that is most popular and widely used in the industry as well as in personal systems. There are various operating systems based on the Linux kernel, some of the popular Linux distributions are Ubuntu, CentOS, Red Hat, Debian, and Kali Linux.
What is Vi Editor
Vi Editor is a widely used text editor in Unix/Linux systems and is known for its efficiency and flexibility. Vi editor was developed in 1976 by Bill Joy and later in 1991, an improved version of Vi editor was released which is known as VI Improved (VIM).
Modes in Vi Editor
There are two modes in Vi Editor:
- Insert Mode: In this mode, you can directly input and edit text. When in Insert Mode, you can type characters, and add, modify, or delete text content as needed. You can initiate Insert Mode by pressing keys such as 'i' (to insert before the cursor) or 'a' (to insert after the cursor). There are also variations like 'I' (to insert at the beginning of the line) or 'A' (to insert at the end of the line). To exit Insert Mode and return to Command Mode, simply press the 'Esc' key.
- Command Mode: In Command Mode, you navigate and manipulate text without directly typing characters. This mode is designed for controlling the editor's behavior and performing various editing tasks efficiently. You can move the cursor, delete text, copy and paste, search for text, and execute a wide range of other editing operations using keyboard commands. To transition from Insert Mode to Command Mode, press the 'Esc' key. The clear distinction between these two modes in Vi is a foundational feature that enables users to edit text swiftly and effectively while minimizing the need for complex keyboard shortcuts.
How to Save a file in Vi/Vim Editor & Quit?
To execute any command you must have to be in command mode to enter command mode if you are in insert mode press Esc.
1. Save a file in Vi/Vim Editor
There are 3 ways to save a file in the vi/vim editor which are mentioned below:
- :w - Save changes but do not exit.
- :wa: - Save all open multiple files without quitting.
- :w filename - Save the file with a different name and continue editing the current file.
Example:
Step 1: Open a file or create using the vi editor
Step 2: Enter in command mode by pressing Esc if you are in insert mode
Step 3: Use any of the above commands (w, wa, w [filename]) to save the file, in the below screenshot we have used :w [filename] to save the file using a new name in the newFile.txt file.

2. Quit Vi/Vim Editor
There are four ways to quit the editor:
- :q - To quit the editor
- :q! - To force quit useful when changes are made to a file but the user doesn't want to save them
- :qa! - Quit all open files without saving changes (force-quit)
- ZQ (Shift + ZQ) - Quit without saving changes
Example:
Step 1: Open a file or create using the vi editor
Step 2: Enter in command mode by pressing Esc if you are in insert mode
Step 3: Use any of the above commands (q, q!, qa or ZQ) to quit the editor in the below screenshot we have used:q to quit the editor

3. Save the file and Quit Editor
There are 3 ways to save a file and quit the vi/vim editor which are mentioned below:
- :wq - Save changes and exit the editor
- :x - Same as :wq used for save changes and quit
- ZZ (Shift + ZZ) - Save changes and exit
Example:
Step 1: Open a file or create using the vi editor
Step 2: Enter in command mode by pressing Esc if you are in insert mode
Step 3: Use any of the above commands (wq, x, ZZ) to save the file and quit the editor in the below screenshot we have used :wq command to save the file and quit the editor

All the above-mentioned commands are mentioned below in a tabular format:
|
:q
| Quit
|
:q!
ZQ (Shift + ZQ)
| Quit without saving changes i.e. discard changes
|
:qa
| Quit all open files without saving changes (force-quit)
|
:w
| Save Changes
|
:wa
| Save changes to all the open multiple files
|
:w [filename]
| Save the file with a different name
|
:wq
ZZ (Shift+ZZ)
:x
| Save changes and exit the editor
|
Conclusion
In this article we covered different ways to save a file, to quit the editor, and commands that do both save and quit along with shortcut keys. Apart from that a basic vi editor guide is also appended so that new users get familiar with the editor.
Similar Reads
How to Create a VIM File in Linux CMD
In Linux, there are many ways to open and create files, but Vim stands out as a powerful and versatile text editor. It comes pre-installed on most Linux systems and allows you to create, edit, and manage files directly from the terminal.In this article, we will explain you, how to create a new file
3 min read
How to Copy Paste in Vim Editor
Copying and pasting in Vim Editor is an essential operation for efficient text editing. Vim offers several methods to copy and paste content, including visual mode selection, yanking, and putting. Understanding these techniques can significantly enhance your productivity when working with Vim. In th
3 min read
How to Hide Line Numbers in Vim Editor
In this article, we will cover how to hide line numbers in the Vim editor. First, we look at what Linux and VIM editors and why we use them, what are its features and how can we use them followed by various options to make the Vim editor hide line numbers and how we can change the default settings o
3 min read
How to Edit Multiple Files in Vim Editor in Linux
Vim, short for "Vi Improved," is a highly configurable and powerful text editor built upon the foundation of Vi. Like its predecessor, Vim offers robust support for editing multiple files simultaneously, making it an indispensable tool for developers, system administrators, and anyone working extens
4 min read
How to Paste in Vim Editor from Notepad?
Vim is a robust text editor found in many Unix-like systems. Copying text from an external source, such as Notepad, and copying it into Vim can be challenging, especially for inexperienced users. In normal mode, every character entered will be read as a command in Vim. Place the cursor where you wan
4 min read
How to Exit VIM Quickly - A Step-by-Step Guide
Exiting VIM quickly and easily can be done in multiple ways, depending on whether you want to save changes or quit from VIM without saving. Exiting VIM (a text editor commonly used in Linux) can confuse new users. Here's a step-by-step guide to help you exit VIM safely, whether you've made changes o
5 min read
How to Install Vim Editor in MacOS?
Vim editor is an open-source software developed by "Bram Moolenaar" in 1991. Vim, short for "Vi IMproved," is a highly customizable and powerful text editor that is most commonly used by developers, system administrators, writers, and anyone who works extensively with text files. Vim is created as a
3 min read
How to Edit Multiple Files in Vi Editor in Linux
Vi, short for "visual editor," is a powerful and ubiquitous text editor in the Linux ecosystem. Among its many features, one of the most useful is its capability to edit multiple files simultaneously. This ability streamlines the editing process, especially when dealing with projects that span multi
4 min read
How to Install Vim Editor in Ubuntu?
Vim is a highly configurable and powerful text editor used by developers and system administrators on Ubuntu and other Linux distributions. Whether youâre coding, writing scripts, or editing configuration files, Vim offers efficiency with its vast features and keyboard shortcuts. In this guide, we'l
3 min read
How to Indent Code in Vim Editor?
Vim is a popular text editor used by many developers for writing code, creating readme files, and writing scripts. It's open-source and offers a lot of ways to customize your experience. This makes it easier and more efficient to work with. Properly formatting and indenting your code is important be
4 min read