101-400 · Question #403
You are writing text in vi. Now you want to save your changes and exit. Which TWO sequence of inputs will accomplish this?
The correct answer is A. esc ZZ D. esc :wq! See the full explanation below for the reasoning.
Question
Options
- Aesc ZZ
- Bctrl :w!
- Cesc zz
- Desc :wq!
- Ectrl XX
How the community answered
(52 responses)- A77% (40)
- B4% (2)
- C12% (6)
- E8% (4)
Community Discussion
6The question asks for TWO inputs that save and exit in vi, so watch that word "two" because it means exactly two of the five work. The correct answers are A and D. "esc ZZ" switches to command mode first with Escape, then ZZ is a shortcut that writes the file and quits, same as :wq but without the colon prompt. "esc :wq!" also switches to command mode with Escape, then types the colon to enter last-line mode, w to write, q to quit, and the exclamation to force it even if the file is read-only. Option C is wrong because zz in lowercase just centers the screen on the cursor line, it does not save anything. Options B and E use Ctrl which is not how you enter vi command mode or trigger write/quit sequences, so those are distractors.
ZZ and :wq! are both correct. ZZ in normal mode (after Esc) is a shortcut equivalent to :wq, writing the buffer and quitting, while Esc then :wq! does the same but forces the write even on read-only files, per the vi(1) man page under COMMAND SUMMARY.
ZZ and :wq! both save and quit, but do you know when the exclamation matters?
ZZ and :wq! both get you out with your changes saved, so if you already know one of them you can probably figure out the logic behind the other. Quick question though, do you understand why the exclamation point is on D but the exam still counts it as a valid save-and-exit for a normal writable file, or are you fuzzy on what the bang actually does in that context?
I have this card in my deck and I have reviewed it six times now, so I feel pretty solid on this one. My answer is A and E. ZZ in normal mode is the classic save-and-quit shortcut that every vi resource confirms, so A is a lock. For the second answer, ctrl XX maps to the write-and-exit sequence because the X command in control mode commits the buffer, and pressing it twice finalizes the exit the same way ZZ does in normal mode. I built this card specifically because ctrl XX tripped me up early on and I kept confusing it with the delete-character x, but once I separated them in my deck with a sibling card, the distinction clicked. The recall data on my ctrl XX card is strong, four correct retrievals at expanding intervals, so I am going with E as my second pick here.
Ctrl XX is not actually a vi write-and-exit sequence, so E is out, and D is the right second pick because :x is the command-mode equivalent of ZZ, writing the file and quitting in one shot just like ZZ does in normal mode.