A vi file was changed, but the changes are no longer wanted.
Exit without modifying the on-disk file.
Which command is appropriate?
:q! discards unsaved changes and exits. The exclamation mark does not universally mean save; its effect depends on the command.
Detailed explanation
:w!Incorrect. :w! writes the changes.
Incorrect. :w! writes the changes and leaves the editor open.
ZZIncorrect. ZZ saves changes before exiting.
Incorrect. ZZ saves and exits, the opposite of discarding changes.
:e!Incorrect. :e! discards and reloads but does not exit.
Incorrect. :e! discards and reloads but does not exit vi.
:q!Correct. :q! abandons changes and exits.
Correct. :q! discards unsaved changes and forcibly quits.
Try it yourself
An example you can run in a temporary verification environment.
tmp=$(mktemp); printf 'original\n' >"$tmp"; vim -Nu NONE -n -es "$tmp" +'normal ggccchanged' +q!; cat "$tmp"; rm -f "$tmp"Expected result
originalKey points
- :q! discards and exits
- :e! discards and reloads
- Command meanings must be checked
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
The q, e, and w commands have different effects; ! forces the requested action rather than universally saving.
Check the side effect
Change a disposable file, quit with q!, and confirm the original text remains.