Edit a file in vi and return to normal mode.
Save the changes and exit the editor.
Which key sequence is appropriate?
ZZ in normal mode writes the file when needed and exits vi. It is a two-uppercase-letter normal-mode command, not an Ex command.
Detailed explanation
:q!Incorrect. :q! discards changes and exits.
Incorrect. :q! discards the changes instead of saving them.
:e!Incorrect. :e! discards changes and reloads the file without exiting.
Incorrect. :e! reloads and keeps vi open.
ZZCorrect. ZZ saves the file and exits.
Correct. ZZ writes the changed buffer and exits the editor.
:w!Incorrect. :w! writes the file but keeps the editor open.
Incorrect. :w! writes the file but does not quit.
Try it yourself
An example you can run in a temporary verification environment.
tmp=$(mktemp); printf 'old\n' >"$tmp"; vim -Nu NONE -n -es "$tmp" +'normal ggccnew' +'normal ZZ'; cat "$tmp"; rm -f "$tmp"Expected result
newKey points
- ZZ saves and exits
- :w! writes but stays
- :q! exits without saving
Notes
- Environment: Vim 9.x互換のvi操作 / 一時ファイル
- Use a temporary file and harmless edits when testing vi commands.
- Confirm the behavior on the target vi/Vim implementation.
Foundation review
Place the operation
Saving and quitting can be combined, while writing alone leaves the editing session open.
Check the side effect
Make a harmless change, use the command, and inspect the file after vi exits.