How do I save my changes and exit Vim?

Learn how do i save my changes and exit vim? with practical examples, diagrams, and best practices. Covers git, vim, github development techniques with visual explanations.

Mastering Vim: How to Save Your Changes and Exit

Hero image for How do I save my changes and exit Vim?

Learn the essential commands to save your work and gracefully exit Vim, from basic write-and-quit to force-quitting unsaved changes.

Vim is a powerful, highly configurable text editor that is ubiquitous in the Unix-like world. While its efficiency and extensibility are legendary, the initial learning curve, especially for basic operations like saving and exiting, can be a hurdle for newcomers. This article will guide you through the fundamental commands to ensure you can confidently save your modifications and exit Vim, whether you're a beginner or just need a quick refresher.

The Basics: Saving and Exiting

The most common scenario involves saving your changes and then exiting the editor. Vim operates in different modes, and most commands are executed from Normal mode. If you're in Insert mode (typing text), press Esc to return to Normal mode before issuing any commands.

flowchart TD
    A[Start in Insert Mode] --> B{Press Esc}
    B --> C[Normal Mode]
    C --> D[":w" (Write/Save)]
    D --> E[":q" (Quit)]
    E --> F[Exit Vim]

Basic Vim Save and Exit Workflow

:w
:q

Separate commands to save and then quit

You can combine these two commands for convenience. The :wq command will first write (save) the file and then quit Vim. This is one of the most frequently used commands.

:wq

Combined command to save and quit

Exiting Without Saving

Sometimes you make changes you don't want to keep, or you opened a file just to view it and made accidental modifications. In such cases, you'll want to exit Vim without saving your current changes. Vim is designed to prevent accidental data loss, so it will warn you if you try to quit with unsaved changes.

:q

Attempting to quit with unsaved changes (will likely fail)

If you try :q with unsaved changes, Vim will display an error message like E37: No write since last change (add ! to override). This is Vim's way of asking if you're sure. To force quit without saving, you need to add an exclamation mark ! after the q.

:q!

Force quit without saving changes

Advanced Saving and Exiting Options

Vim offers more granular control over saving and exiting, especially when dealing with multiple files or specific saving requirements.

1. Saving to a New File

To save the current buffer's content to a new file without changing the current file's name, use :w new_filename. The original file remains open with its original name, and the new file is created with the specified content.

2. Saving and Quitting All Buffers

If you have multiple files open in different buffers and want to save all modified buffers and then quit Vim entirely, use :wqall or :xall. The :xall command is similar to :wqall but only writes files that have been modified.

3. Force Quitting All Buffers

To quit Vim and discard changes in all modified buffers, use :qall!. This is the most aggressive way to exit and should be used when you are certain you want to abandon all work across all open files.

:w new_file.txt
:wqall
:qall!

Examples of advanced save and quit commands