Skip to Content
Learning the vi and Vim Editors, 7th Edition
book

Learning the vi and Vim Editors, 7th Edition

by Arnold Robbins, Elbert Hannah, Linda Lamb
July 2008
Beginner
492 pages
16h
English
O'Reilly Media, Inc.
Content preview from Learning the vi and Vim Editors, 7th Edition

Saving and Exiting Files

You have learned the vi command ZZ to quit and write (save) your file. But you will frequently want to exit a file using ex commands, because these commands give you greater control. We’ve already mentioned some of these commands in passing. Now let’s take a more formal look:

:w

Writes (saves) the buffer to the file but does not exit. You can (and should) use :w throughout your editing session to protect your edits against system failure or a major editing error.

:q

Quits the editor (and returns to the Unix prompt).

:wq

Both writes the file and quits the editor. The write happens unconditionally, even if the file was not changed.

:x

Both writes the file and quits (exits) the editor. The file is written only if it has been modified.[20]

vi protects existing files and your edits in the buffer. For example, if you want to write your buffer to an existing file, vi gives you a warning. Likewise, if you have invoked vi on a file, made edits, and want to quit without saving the edits, vi gives you an error message such as:

No write since last change.

These warnings can prevent costly mistakes, but sometimes you want to proceed with the command anyway. An exclamation point (!) after your command overrides the warning:

:w!
:q!

:w! can also be used to save edits in a file that was opened in read-only mode with vi-R or view (assuming you have write permission for the file).

:q! is an essential editing command that allows you to quit without affecting the original file, ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Learning the vi and Vim Editors, 8th Edition

Learning the vi and Vim Editors, 8th Edition

Arnold Robbins, Elbert Hannah
Vim Masterclass

Vim Masterclass

Jason Cannon
Mastering Vim

Mastering Vim

Ruslan Osipov

Publisher Resources

ISBN: 9780596529833Errata Page