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 changes and exits.
:e!Incorrect. :e! discards changes and reloads the file without exiting.
Incorrect. :e! discards changes and reloads the file without exiting.
ZZCorrect. ZZ saves the file and exits.
Correct. ZZ saves the file and exits.
:w!Incorrect. :w! writes the file but keeps the editor open.
Incorrect. :w! writes the file but keeps the editor open.
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操作 / 一時ファイル
- Command output formatting can vary slightly by distribution or tool version.
- Run the example in a temporary directory or process when possible.
Foundation review
Read the scope first
Check whether the command acts on the current shell, a new process, an existing process, or a file.
Verify the observable result
Use the supplied command and compare the output with the expected result.