Knowledgebase

Find frequently asked questions and setup guides by searching or by browsing the categories.

The Short Answer

  1. Press Esc to return to normal mode.
  2. Type :wq and press Enter to save and quit.
  3. Or type :q! and press Enter to quit without saving. Every change since the last save is lost.

Vim has modes. In insert mode, which you enter with i, every key types text, so :wq would land in your file. Commands work in normal mode. If you are not sure which mode you are in, press Esc once or twice; in normal mode it does no harm.

All the Ways to Save and Quit

CommandUnsaved changesResult
:wSavedSaves, stays open.
:wqSavedSaves and quits.
:xSavedSaves only if changed, quits.
ZZSavedSame as :x.
:qKeptRefuses to quit if any.
:q!LostQuits without saving.
ZQLostSame as :q!.

Type all of them in normal mode, after Esc. Commands that start with a colon end with Enter; ZZ and ZQ are two capital letters that act at once. The difference between :wq and :x: :wq always writes the file, so its modification time changes even when you edited nothing, while :x writes only when there is something to save.

Why :q Will Not Quit

When the file has unsaved changes, :q stops with the message E37: No write since last change (add ! to override). Vim is protecting your edits. Decide: :wq keeps them, and :q! throws them away. To step back through what you changed before you decide, see how to undo and redo in Vim.

Save Under Another Name

In normal mode:

:w /etc/nginx/nginx.conf.new
:saveas /root/nginx.conf.copy

:w with a file name writes a copy and keeps you in the original, which still counts as unsaved, so :q still refuses. :saveas writes the copy and continues editing the copy. After saving a server configuration, test it with the service's own check, such as sudo nginx -t, before you reload the service.

Several Files Open

When you opened more than one file, for example with vim a.conf b.conf, use these in normal mode:

  • :wa: saves every changed file.
  • :qa: quits all, but refuses if any file has unsaved changes.
  • :wqa: saves all and quits.
  • :qa!: quits all and discards every unsaved change.

Read-Only Files and Permission Denied

  • E45, the readonly option is set: the file was opened read-only, for example with view or vim -R. If your user may write the file, :w! saves it anyway.
  • E212, cannot open file for writing: your user may not write the file, typically a system file under /etc opened without sudo.

In the second case, write the text through sudo without leaving Vim:

:w !sudo tee % > /dev/null

Press Esc first, as for every command here. % stands for the current file name, and tee running under sudo writes your text to it. sudo may ask for your password. Vim then warns that the file changed on disk; choose to load it, and :q closes Vim. If it still refuses, your text is already on disk and :q! is safe. Next time, open system files with sudoedit /etc/nginx/nginx.conf or sudo vim.

When Your SSH Connection Drops

Vim keeps unsaved changes in a swap file, usually next to the original and named like .nginx.conf.swp. If the connection drops while you edit, the next time you open the file Vim shows E325: ATTENTION and offers choices. First make sure no other session is editing the same file. Then press R to recover the unsaved text, check it, and save with :w, or with :w and a new name if you want to compare first. The prompt appears again on the next open because the swap file is still there; once your work is saved, choose D to delete it. Vim offers D only when the process that made the swap file is no longer running. Editing inside tmux avoids the problem; see the tmux cheat sheet.

vi, Vim, and Neovim

On most servers the vi command starts Vim or a small build of it, and the commands above work the same in Neovim (nvim). One difference: Neovim does not run interactive commands through :w !, so the sudo tee line fails when sudo needs to ask for a password. Save to a file in your home folder with :w ~/nginx.conf instead, and move it into place with sudo. To find a line before you save, see how to search in Vim.

Was this answer helpful?
Loading...