LFCA practice question 14 of 15

When using vi or vim to edit a file, how do you exit without saving?

Full set: 60 questions

Domain System Administration Fundamentals

Answer options
A: :q! Correct answer
B: ZZ
C: :wq!
D: :exit
Correct Answer:
A. :q!
Explanation

In the vi or vim editor, commands are issued in command-line mode, which is accessed by pressing the colon (:) key. The command :q! is used to exit the editor without saving any modifications made to the file. The q stands for "quit," and the exclamation mark (!) is a force modifier that instructs the editor to discard all changes since the last save and terminate the session. This is the standard and correct method to abort an editing session and revert to the last saved state of the file.

Why Incorrect

B. ZZ: This command saves the file if it has been modified and then exits. It is equivalent to the :wq command, not a quit-without-saving action.
C. :wq!: This command forces a write (save) to the file, even if it's read-only (permissions allowing), and then quits. It explicitly saves changes.
D. :exit: This command, an alias for :x, saves the file only if it has been modified and then exits. It is a "save and exit" command, not a "discard and exit" command.

References

1. Vim Official Documentation (User Manual): In the section on exiting, the manual explicitly states: "To throw away your changes, use: :q!".

Source: Bram Moolenaar, Vim User Manual, usr10.txt, Section 10.2 "Exiting". Available at: https://vimdoc.sourceforge.net/htmldoc/usr10.html#10.2

2. Linux Man Pages (vi): The official manual page for vi describes the :q! command as the way to quit and discard all changes.

Source: vi(1) Linux manual page. Under the "Exiting vi" or "Commands" section, it specifies: ":q!" - Quit vi, discarding all changes."

3. Stanford University Courseware (CS 107): The course materials for Programming Paradigms include a guide to essential tools, which lists vi/vim commands.

Source: Stanford University, CS 107 Programming Paradigms, "Using Vim". The command list specifies: ":q!" - Quit without saving changes. Available at: https://web.stanford.edu/class/cs107/guidevim.html

Scroll to Top