In vi you have made changes you do not want. Which command leaves the editor and discards them?
LPIC-1 Exam 101-500, objective 103. GNU and Unix commands medium
Draft — not checked yet. This question was written from the published exam objectives and cites the clause it comes from, but nobody has yet read it against that clause and signed for it.
It is kept out of search results and out of mock exams until they have. Read the source below before you take the answer as settled.
The options
Not correct :w!
Wrong. :w! forces a write even to a read-only file. It saves the changes and does not exit.
Not correct :q
Wrong when the buffer is modified. Plain :q refuses to quit and warns that there are unsaved changes.
Not correct :wq
Wrong — that is the opposite. :wq writes the buffer to the file and then quits, making the unwanted changes permanent.
Correct :q!
Correct. The exclamation mark forces the action, so vi quits and throws away the unsaved buffer.
Why
The four commands to keep straight are :w (write), :q (quit, refused if modified), :q! (quit discarding changes) and :wq (write then quit). In vim :x and ZZ also write and quit, but they write only if the buffer was actually modified. Searching is separate again: /pattern searches forward, ?pattern backward, and n repeats the last search.
Where this comes from
- Cited
- LPI exam objective 103.8
- What it says
- Save and exit files in vi.
Practise this
Reading one question is not practice. The trainer will draw a set from objective 103 and space the ones you get wrong.