A file is being edited in vi with unsaved changes.
Write the changes to the current file and then quit vi.
Which ex command is appropriate?
:wq writes the file and quits in sequence, saving the pending changes before exit.
Detailed explanation
:qIncorrect. :q refuses to quit with unsaved changes.
Incorrect. :q refuses to quit with unsaved changes.
:wqCorrect. :wq writes the changes and exits vi.
Correct. :wq writes the changes and exits vi.
:q!Incorrect. :q! quits without saving the changes.
Incorrect. :q! quits without saving the changes.
:wIncorrect. :w saves but leaves vi open.
Incorrect. :w saves but leaves vi open.
Try it yourself
An example you can run in a temporary verification environment.
printf 'old\n' > sample.txt; vim -Nu NONE -n -es sample.txt -c 'execute "normal! ggccnew\<Esc>"' -c 'wq'; cat sample.txtExpected result
newKey points
- :w saves
- :q quits
- :wq saves and quits
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.