Technical skill

Linux command: vim: how to exit vim

Subject: .../LINUX

Save your changes before leaving

Vim is a text editor. While you edit, your changes live in a buffer, a working copy of the file in memory. To keep those changes, write the buffer to its file before quitting. If you do not want the edits, choose a command that discards them. Vim uses modes: in Insert mode, typing adds text; in Normal mode, keys run editing commands.

For a simple save-and-quit, press Esc, type :wq, then press Enter:

Esc
:wq
Enter

Esc returns to Normal mode. A colon command opens Vim's command line, and :wq writes the current buffer and closes the current window. If Vim reports a write error, perhaps because of file permissions or a bad path, the file was not saved as requested. Read the message and choose another location or discard the edits deliberately.

Pick the command that matches your goal

A window is a view onto a buffer; more than one window can show the same buffer. :q closes the current window when there are no unsaved changes. If it is Vim's last edit window, Vim exits. When the buffer has changes, Vim warns rather than silently throwing them away.

Use :wq to save and close the current window. Use :w to save and stay in Vim. Use :q! to close the current window without writing. If it is the last window showing that buffer, Vim discards the unsaved edits. In the usual single-window session, :q! therefore abandons the unsaved changes. If another window shows the same buffer, those edits remain visible there. ZZ, typed in Normal mode without a colon or Enter, writes the buffer if it changed and closes the current window.

A plain :q is a safe first try if you are unsure: Vim will tell you the buffer is modified. For example, after that warning, decide whether to save with :wq or keep working. Do not use :q! just to dismiss a write error if you still need the work.

Close several windows or buffers

:q and :wq apply to the current window and buffer; they can leave other windows open or other modified buffers unsaved. :qa quits all windows and protects against unsaved changes. :wqa writes changed buffers and then quits all windows. :qa! quits all and discards changes in modified buffers, so use it only when discarding them is intended.

Vim's built-in help explains the commands: :help :quit, :help :write, and :help ZZ. The tutorial and introductory screen explain modes and command entry. If another program opened Vim, such as a version-control tool, quitting returns control to that program.

Interactive exercises

Materials

Check yourself
Q:

What command is used to exit vim?

Q:

What command could be used to exit vim if file has unsaved changes?

Do you know that ...?
- Command ":x" is the same as ":wq"

It saves any unsaved changes and exists vim

Skillsets
Skillset Skyeng Senior Backend Developer preview
image descriptionSkyeng Senior Backend Developer
by Artem Naumenko (slack2)
39 users
Skillset Linux administrator preview
image descriptionLinux administrator
by Infinite Education
3080 users
Skillset Devops preview
image descriptionDevops
by Ajay Kumar
2004 users
Skillset Linux for backend developer preview
image descriptionLinux for backend developer
by Artem Naumenko (fb)
16 users
Skillset Skyeng QA Automation Hard Skillset v09.21 preview
image descriptionSkyeng QA Automation Hard Skillset v09.21
by Aleksandra Kulikova
16 users
Skillset Skyeng SRE preview
image descriptionSkyeng SRE
by Yuliya Marochkina
12 users
Show more