An incorrect edit was made in vi and there are unsaved changes.
Quit forcefully without writing those changes to the file.
Which ex command is correct?
:q! discards unsaved changes and exits vi. The exclamation mark forces the quit instead of saving.
Detailed explanation
:w!Incorrect. :w! writes the changes and stays in vi.
Incorrect. :w! writes the changes and stays in vi.
:wq!Incorrect. :wq! saves the changes before quitting.
Incorrect. :wq! saves the changes before quitting.
:qaIncorrect. :qa exits all files but does not discard unsaved changes in this form.
Incorrect. :qa exits all files but does not discard unsaved changes in this form.
:q!Correct. :q! exits without saving the edit.
Correct. :q! exits without saving the edit.
Try it yourself
An example you can run in a temporary verification environment.
printf 'keep\n' > sample.txt; vim -Nu NONE -n -es sample.txt -c 'normal! dd' -c 'q!'; cat sample.txtExpected result
keepKey points
- :q! discards and quits
- ! forces the operation
- :wq! saves before quitting
Notes
- Environment: Vim 9.x(vi互換exコマンド)
- 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.