Open In App

How to Save a File in Vi / Vim Editor & Quit

Last Updated : 25 Sep, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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:

  1. 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.
  2. 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:

  1. :w – Save changes but do not exit.
  2. :wa: – Save all open multiple files without quitting.
  3. :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.

Screenshot-from-2023-09-20-12-08-46

2. Quit Vi/Vim Editor

There are four ways to quit the editor:

  1. :q – To quit the editor
  2. :q! – To force quit useful when changes are made to a file but the user doesn’t want to save them
  3. :qa! – Quit all open files without saving changes (force-quit)
  4. 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

Screenshot-from-2023-09-20-12-04-12

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:

  1. :wq – Save changes and exit the editor
  2. 😡 – Same as :wq used for save changes and quit
  3. 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

Screenshot-from-2023-09-20-00-52-10

All the above-mentioned commands are mentioned below in a tabular format:

Commands 

Description

: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)

😡

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.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads