wq Command in Linux

wq-command-in-Linux-Feature-Image.png

If you have used the vi or vim editor in Linux, you might have been in a situation where you couldn’t figure out how to save your work or exit. That is where the wq command in Linux comes into the picture. This command is a combination of “write” and “quit” operations in a file, and it is essential for working with files in the terminal. In this guide, we will explore what wq does, how to use it, and why it is so important in the Linux world.

Table of Contents:

What is the wq Command in Linux?

The wq command in Linux is used within the vi or vim text editor to save changes made to a file and exit the editor in one step. In the wq command,

  • w stands for write, which is used to save the current file
  • q stands for quit, which is used to quit the editor.

So, the wq command in Linux tells the editor to save the changes made in a file and quit the editing session, and return to the terminal.

Syntax of the wq command in Linux

Below is the syntax of the wq command in Linux:

:wq
  • w stands for write
  • q stands for quitting the editor mode
Master DevOPs Today - Accelerate Your Future
Enroll Now and Transform Your Future
quiz-icon

What are the vi and vim commands in Linux?

The vi and vim commands in Linux are used to open and edit text files in a terminal-based text editor. vi comes from “visual mode” of the ex editor, and is one of the oldest and powerful terminal editors in Linux OS. It is used to

  • Create and edit text files
  • Modify system configuration files
  • Write and debug scripts or code

vim stands for Vi IMproved and is an enhanced version of the vi command in Linux. It has features like,

  • Syntax highlighting
  • Search and replace
  • Code indentation

Different Modes in the vi command in Linux

There are 3 modes present in the vi command in Linux. There are as follows:

1. Normal Mode: When you first open a file with vi, you are placed in normal mode. In this mode, you can only move around the file, delete lines or words, copy and paste text, and so on.

2. Insert Mode: You cannot type text in normal mode, you have to enter insert mode to type and edit your content in the file. To enter insert mode, press any of the keys below:

  • i: To start inserting before the cursor
  • a: To insert after the cursor
  • o: To open a new line below the current one

3. Command-line Mode: Also called Ex Mode, is used to give more advanced commands like saving, quitting, or searching and replacing text. To enter a command-line mode, press : (colon) from normal mode, and you will see the colon appear at the bottom of the screen.

Example:

Now, let us make a directory named intellipaat and inside it a text file named wqcommand.txt. After this, we will use the vi command to write to the file.

Example1

After entering the file, press i for the insert mode, and start writing your text as below.

Example1

Once your text is over, press ESC and then :wq to save and quit the editor.

Example1

Returning to the terminal, use the cat wqcommand.txt to print the output as above.

Different Options in the wq Command in Linux

There are several useful variations and related options in the wq command in Linux:

1. : x Option

The : x option in the wq command in Linux is used to write and quit. It is similar to the :wq command, but only writes the file if there are changes. It is used to save and quit, but skip writing if no changes were made, and is slightly more efficient than :wq.

Below is the syntax of it:

: x

2. ZZ Option

The ZZ option in the wq command in Linux is used to quickly save and quit the file without typing a colon (:). You just have to press Shift + Z + Z to save the file.

Note: ZZ is a normal mode command in Vim, not part of the :wq family

3. :wq! Option

This option of the wq command in Linux is used to forcefully write and quit the file, even if it is marked as read-only or opened with restricted permissions. It is mainly used if you have edited a read-only file and want to override it.

Below is the syntax of it:

:wq!

Now, let us see the difference between the options.

Option Description When to Use Key Difference
: wq Writes (saves) the file and quits vi/vim When you want to save changes and exit normally Standard save and exit
: x Writes the file only if changes were made, then quits When you want to avoid unnecessary writes More efficient than :wq if no changes occurred
ZZ Saves changes (if any) and exits vi Quick save and exit using Shift + ZZ Shortcut key, no colon needed
:wq! Forces a write (even if read-only) and quits When the file is read-only or restricted Overrides permission with force save

Note: Be careful while using it, as it can overwrite protected files.

Get 100% Hike!

Master Most in Demand Skills Now!

Example Usage of wq Command in Linux

Now, we will look at some real-world examples of the wq command in Linux.

1. Saving a new file

When you create a new file using the vi command, after typing some content in it, you can save the file using the wq command. It allows the editor to write the buffer content to the file system, create a file if it is not present, and exit the editor once writing is successful.

2. Editing an Existing File

When editing an already existing file, the wq command does two things, overwrites the existing file and closes the editor. This ensures that changes made in the file are preserved and the file remains consistent with your current edits.

3. Forced Save and Exit

When the files only have read-only permissions, the wq command may fail. You can use wq! Command to override the permissions warnings and then save your updated file.

When editing symlinked or remote files (e.g., via SSHFS or NFS), the :wq command ensures that changes are safely written to disk before exiting the file. In environments with delayed I/O, it also helps to ensure atomic and consistent file saving.

Advanced Use Cases of the wq Command in Linux

The wq command in Linux has many use cases apart from just saving the file and quitting it. Some of these are as follows:

  1. Save the File With a New Name and Exit: This command writes the current content to a file in a new file and then quits the editor. It serves as the Save As feature. It should be used when you want to keep the original file unchanged and save your changes in another file. It is useful for creating backups and template files.
  2. Save Using sudo Without Closing and Reopening: If you forgot to open a system file using the sudo command in Linux, you will not be able to save it. For this, you can pipe the file to sudo tee, which helps you write the file with proper permissions.
  3. Save and Quit Multiple Opened Files: If you have opened multiple files at once, you can save and quit all files at once by using :wqall or :wqall! command.

Best Practices of the wq Command in Linux

Some of the tips are mentioned below to use the wq command in Linux effectively.

  • Use the wq command regularly to avoid losing changes during power outages or crashes. You should make sure that edits are stored in the file system, and do not just quit using the :q command.
  • Use the sudo vi command for protected files because it ensures you have valid permission to write the file.
  • Avoid using the wq command unnecessarily because sometimes it forces writing and quitting the file, which can overlook warnings and unsaved states of the files.
  • Learn related commands like :w, :q, :x, etc, to increase your flexibility and efficiency.
Unlock Your Future in Linux
Start Your Linux Journey Today
quiz-icon

Conclusion

From the above article, we learned that the wq command in Linux is one of the widely used commands in Linux. It not only saves the file you are working on, but also quits it at the same time. There are many options present in the wq command like x, ZZ, and wq!, which can be used as per the user’s choice and needs. But, it should be used wisely, because it can lead to overwriting some important files, which may not have write permission.

If you want to learn more about the wq command in Linux, you can refer to our Linux Course.

wq Command in Linux – FAQs

Q1. What is the use of the wq command in Linux?

The :wq command in vi or vim saves (writes) the file and then quits the editor.

Q2. How do I quit vi without saving?

Use :q! to quit without saving any changes.

Q3. How do you save and quit WQ?

Type :wq in command mode to save the file and exit vi or vim.

Q4. What are Q and WQ in Linux?

In vi/vim, :q quits the editor, while :wq writes (saves) the file and quits.

Q5. What is the difference between ZZ and WQ?

Both save and quit, but ZZ only saves if changes were made, while :wq always writes and exits.

About the Author

Senior Consultant Analytics & Data Science, Eli Lilly and Company

Sahil Mattoo, a Senior Software Engineer at Eli Lilly and Company, is an accomplished professional with 14 years of experience in languages such as Java, Python, and JavaScript. Sahil has a strong foundation in system architecture, database management, and API integration. 

fullstack