Vim Saving and Exiting
100%

Advanced Text-Fu · Lesson 8

Vim Saving and Exiting

Learn how to write, quit, save under another name, or deliberately discard Vim buffer changes.

Writing and quitting are separate Vim operations. Before entering an Ex command, press Esc to return to Normal mode, type :, enter the command, and press Enter. Read Vim's status or error message before assuming a write succeeded.

Writing the Current Buffer

Use :w to write the current buffer to its associated file without closing the window:

:w

A write can fail because the buffer has no filename, the directory is not writable, the filesystem is full, or another condition prevents the operation. Check the message Vim reports.

Use :w copy.txt to write the current buffer to another pathname while keeping the current buffer's existing name. Use :saveas copy.txt when the buffer should adopt the new pathname.

Which Vim command writes the current buffer to its associated file without quitting?

Quitting an Unmodified Buffer

Use :q to close the current window when doing so will not abandon unsaved buffer changes:

:q

If the current buffer is modified and its changes would be lost, Vim normally refuses and reports a warning. This safeguard gives you a chance to write or reconsider.

Which command quits the current Vim window when no unsaved changes would be lost?

Discarding Unsaved Changes

Use :q! only when you intentionally want to close the current window and abandon changes that would otherwise block quitting:

:q!

The exclamation mark overrides the unsaved-change warning. Those buffer changes are not written, so verify that they are truly disposable before pressing Enter.

The current buffer has changes you deliberately do not want to save. Which command quits the current window and abandons them?

Writing and Quitting Together

Use :wq when the buffer should be written and the current window should close after a successful write:

:wq

If writing fails, Vim does not complete the requested quit. Resolve the error rather than assuming the data reached disk.

Which command writes the current buffer and then quits the current window if writing succeeds?

Using :x and ZZ

:x writes the buffer only if it is modified, then quits. In Normal mode, uppercase ZZ performs the same write-if-modified-and-quit behavior:

:x
ZZ

This differs subtly from :wq, which requests a write even when the buffer is unchanged. Uppercase ZQ is the Normal-mode counterpart for quitting without writing, similar to :q!.

Which Normal-mode command writes only when the buffer is modified and then quits?

When several windows or buffers are involved, a command may close only the current window. Commands such as :qa, :wqa, and :qa! act across windows, but review every modified buffer before using an all-windows force command.

To practice writing and quitting on a disposable file, try this hands-on lab:

  1. Edit Text Files in Linux with Vim and Nano - Practice creating files, editing text, saving files, and navigating with both Vim and Nano. This lab will solidify your understanding of basic Vim operations, including how to save and quit.

Lesson complete

You finished Vim Saving and Exiting

You can now choose a Vim exit command that matches your intent for unsaved data.

  • Write without quitting with :w.

  • Quit safely with :q when no changes would be lost.

  • Discard changes deliberately with :q!.

  • Write and quit with :wq.

  • Use :x or ZZ for write-if-modified behavior.

Keep your learning progress

Create a free account to save this lesson and continue learning on any device.

Create a free account
Next Lesson
Back to Advanced Text-Fu