Got it

Linux ESC: The difference between :wq and :wq!

Latest reply: Aug 15, 2022 06:33:45 10376 13 9 0 0

Hi, Community!

This article describes the difference between :wq and :wq!.


What is wq

To save a file, you must first be in Command mode. Press Esc to enter Command mode, and then type :wq to write and quit the file. The other, quicker option is to use the keyboard shortcut ZZ to write and quit.


How to use the :wq/:wq! command

1. Use cd command to enter the path of the object file.

2. Use vi object file to edit the file.

3. The vi has three mode: Command mode, Insert mode, and Last line mode.(Linux ESC: The difference between vi and vim)

  • Command mode: When you start vi, you enter command mode. The keystroke action in this state is recognized by Vim as a command, not as an input character.

    • i: Switch to insert mode.

    • x: Deletes the character where the current cursor is located.

    • : : Toggles the last line mode to enter commands on the bottom line

  • Insert mode

  • Last line mode:

    • q: Exit the program.

    • w: Save the file.

    • ESC: exit the Last line mode.

4. Enter i to enter the insert mode.

5. Edit the file.

6. Enter : to enter last line mode, and enter wq/wq! to save and exit.

The difference between :wq and :wq!

  • :Wq (Save edit operation and exit): Writing even if the file has not been modified, and updates the modification time of the file.

  • :Wq! (Save edit and force exit): Mandatory write to file and forced exit, ! for enforcement.

    Some files are set to read-only. Generally, they are not modified. However, if you are the owner or root of the file, you can run :wq! to save the file and exit the file. If you do not have permission on the file, it does not force a write. In other words, you must have the permission on the file, whether it is read-only or read-write, before you can use :wq!

 

The difference between :wq and :x

:x Write to the file and exit. Write only when the file is modified, and update the file modification time; otherwise, the file modification time will not be updated.

 

Both :wq and :x are generally the same, but in terms of programming, there may be a significant impact on editing source files. Because the file is not modified, ":wq" forces the update time of the file to be updated. This will cause make to compile the entire project, thinking that the file has been modified, and then recompile the link to generate the executable. This can have misleading consequences and, of course, unnecessary system resource costs. Therefore, we have to get used to :x to save resources.


The difference between :q and :q!

:q in vim indicates that the user exits. :q! indicates that the file is not saved and exits forcibly.

The differences between :q and :q!" are as follows:


  • If the file is modified, the :q prompts you whether to exit. Enter y to exit.

  • If the file is modified, run the :q! command. will force a direct exit, and the file will not be retained.


The difference between :wq and ZZ
After ZZ command is executed, the old file can still be written. 

After :wq is used, the file names are the same, but the iNode has changed. Therefore, the new file replaces the old file.

Therefore, the ZZ and :wq commands are different.



Thanks!

  • x
  • convention:

Y_T_Z
Admin Created Jul 4, 2022 10:10:04

  • x
  • convention:

user_4655977
user_4655977 Created Jul 18, 2022 08:30:22 (0) (0)
Thanks!
vi editor  
little_fish
Admin Created Sep 30, 2020 11:29:51

Thank you for this excellent sharing! The difference between wq and wq!
View more
  • x
  • convention:

Y_T_Z
Admin Created Mar 30, 2021 02:19:21

After it, you can know the wq, wq!, x.
Linux ESC: The difference between :wq and :wq!-3847543-1
View more
  • x
  • convention:

Unicef
MVE Created May 15, 2021 03:46:51

GOOD
View more
  • x
  • convention:

PaPu
Created Jul 15, 2021 07:32:00

I usually use wq: in linux
View more
  • x
  • convention:

Anno7
Moderator Author Created Sep 24, 2021 04:51:15

informative
View more
  • x
  • convention:

Vien
Created Sep 24, 2021 08:29:22

Good share
View more
  • x
  • convention:

Y_T_Z
Y_T_Z Created Jan 7, 2022 08:00:25 (0) (0)
Thanks!
This command is used very frequently.  
Y_T_Z
Admin Created Jul 4, 2022 10:10:04

  • x
  • convention:

user_4655977
user_4655977 Created Jul 18, 2022 08:30:22 (0) (0)
Thanks!
vi editor  
user_4655977
Created Jul 18, 2022 08:30:44

wq ,vim, and vi!
Thank!
View more
  • x
  • convention:

user_4655977
Created Jul 20, 2022 08:45:52

wq is a very common command in linux.
View more
  • x
  • convention:

Y_T_Z
Y_T_Z Created Jul 21, 2022 01:09:46 (0) (0)
Yes, wq, ls, vi, cd.....  
12
Back to list

Comment

You need to log in to comment to the post Login | Register
Comment

Notice: To protect the legitimate rights and interests of you, the community, and third parties, do not release content that may bring legal risks to all parties, including but are not limited to the following:
  • Politically sensitive content
  • Content concerning pornography, gambling, and drug abuse
  • Content that may disclose or infringe upon others ' commercial secrets, intellectual properties, including trade marks, copyrights, and patents, and personal privacy
Do not share your account and password with others. All operations performed using your account will be regarded as your own actions and all consequences arising therefrom will be borne by you. For details, see " User Agreement."

My Followers

Login and enjoy all the member benefits

Login

Block
Are you sure to block this user?
Users on your blacklist cannot comment on your post,cannot mention you, cannot send you private messages.
Reminder
Please bind your phone number to obtain invitation bonus.
Information Protection Guide
Thanks for using Huawei Enterprise Support Community! We will help you learn how we collect, use, store and share your personal information and the rights you have in accordance with Privacy Policy and User Agreement.