To save a file and exit the vi/vim editor, you need to switch to command mode by pressing the Esc key, then type a colon (:) followed by a command. There are two commands that can save a file and quit the editor: x and wq. The x command writes the buffer to the file only if there are unsaved changes, while the wq command always writes the buffer to the file and updates the file modification time. Both commands are followed by an exclamation mark (!) to force the operation and override any warnings. Therefore, the correct sequences are:
Esc ZZ: This is equivalent to typing :x and hitting Enter.
Esc :wq!: This writes the buffer to the file and quits the editor, ignoring any warnings.
The other options are incorrect because:
Ctrl :w! is not a valid sequence. The Ctrl key does not switch to command mode, and the w command only saves the file without exiting.
Esc zz is not a valid sequence. The zz command does not exist in vi/vim.
Ctrl XX is not a valid sequence. The Ctrl key does not switch to command mode, and the XX command does not exist in vi/vim.
References:
How to Save a File in Vi / Vim Editor & Quit - phoenixNAP
How to Save a File in Vim / Vi and Quit the Editor | Linuxize
How To Save a File in Vi / Vim Text Editor - nixCraft
How to Save a File in Vi / Vim Editor & Quit - GeeksforGeeks
Contribute your Thoughts:
Chosen Answer:
This is a voting comment (?). You can switch to a simple comment. It is better to Upvote an existing comment if you don't have anything to add.
Submit