The E37: No write since last change error hits exactly when you are rushing to close a Git commit message. Knowing the right keystrokes to save or discard your buffer keeps you from scrambling through man pages mid-session.
- Save and quit:
:wqor:x - Fastest save and close:
ZZ(Shift+Z twice, Normal mode only) - Discard changes and quit:
:q! - Save all open files and quit:
:wqa - Always press Esc first before entering any command.
Why Your Commands Are Not Working: Vim Modes
Vim operates in distinct modes, and commands only work in Normal mode. If your keystrokes are appearing as text in the file instead of running, you are in Insert mode. Press Esc once (or a few times if unsure) to return to Normal mode. The cursor style changes and the status bar clears. Your exit command will now register.
Core Commands to Save and Exit Vim
:wq, :x, and ZZ
:wq writes the file and closes the buffer. It is the most widely known exit command and works reliably in every scenario. :x behaves identically but skips the write step if no changes were made, so the file's modification timestamp stays clean. ZZ is the keyboard shortcut for the same operation: press Shift+Z twice in Normal mode. No colon, no Enter required.
:q! to Discard Changes
When you open a file by mistake or want to throw away edits, :q! exits immediately without saving. The exclamation mark tells Vim to skip its safety check. Nothing is written to disk.
:w to Save Without Exiting
:w writes the current file to disk and keeps the editor open. Use it during long editing sessions to checkpoint your work without interrupting your workflow. Combine it with a filename to write a copy: :w backup.conf saves the buffer to a new file while the original stays open.
Managing Multiple Files and Buffers
:wqa and :qa! for Multi-Buffer Sessions
Opening several files at once with vim file1 file2 file3 or working with split panes creates multiple buffers. :wqa saves every modified buffer and exits. :qa! closes everything without saving any of them. Using :q or :wq in a multi-buffer session only closes the current file; the others remain open.
To check which buffers are loaded, type :ls. The current buffer is marked with %. Navigate between them with :bn (next) and :bp (previous).
Real-World Scenarios for Developers
Exiting Vim During a Git Commit or Rebase
Git opens Vim automatically for commit messages and interactive rebases. Write your commit message, press Esc, then type :wq to confirm the commit. Typing :q! aborts it entirely, the same as pressing Ctrl+C during the operation. The terminal returns to your Git prompt as soon as Vim closes.
For interactive rebases (git rebase -i), the same commands apply. Edit the action list, save with :wq, and Git processes the rebase automatically.
Saving Read-Only Files Over SSH
Editing a system configuration file over an SSH connection without the right permissions triggers a read-only warning when you try to save. The workaround is to pipe the buffer through sudo:
:w !sudo tee %The % is a Vim shorthand for the current filename. This command runs sudo tee <filename> as a shell command and passes the buffer content to it as input. Enter your password when prompted, then type :e! to reload the buffer from disk and clear the modified flag.
Troubleshooting Exit Errors
E37: No Write Since Last Change
This error appears when you type :q on a file with unsaved modifications. Vim blocks the exit to protect your work. Either save with :wq or discard with :q!. There is no middle ground.
Recovering from a Swap File
A .swp file is created when Vim opens a file and removed when it closes cleanly. A crashed terminal or lost SSH session leaves the swap file behind. Opening the same file later shows a recovery prompt with these options:
- R to recover the swap file contents into the buffer
- D to delete the swap file and open the original
- Q to quit without doing anything
After recovering with R, save the file immediately with :w. Then find and delete the leftover .swp file from the same directory. Running ls -a in the file's directory shows hidden files including .filename.swp.
Disk Full Errors
A full disk blocks the write entirely. :wq returns an error. Type :sh to drop into a shell without closing the buffer, free up space by removing files, then type exit to return to Vim. Try the save again with :w.
.vimrc Customization: Ctrl+S to Save
Most developers have muscle memory for Ctrl+S. Add these two lines to your ~/.vimrc:
nmap <C-s> :w<CR>
imap <C-s> <Esc>:w<CR>aThe first mapping saves in Normal mode. The second exits Insert mode, saves, and returns to Insert mode so typing continues without interruption. This does not interfere with any default Vim bindings.
Vim vs. Neovim vs. VSCode Vim
Neovim uses the same exit commands as Vim without exception. :wq, :q!, and :x all behave identically. The VSCode Vim extension follows the same pattern for :w and :wq, but :q closes the active editor tab rather than quitting VSCode itself. :qa closes all Vim-managed tabs in the workspace. If you switch between terminal Vim and VSCode Vim regularly, the commands transfer cleanly with that one difference in mind.
For most developers, :x is the best default habit: it saves only when necessary, keeps modification timestamps accurate, and exits in one step.
Comments (0)
Sign in to comment
Report