Vim is the text editor of choice for many users that spend a lot of time on the command line. Unlike other editors, Vim has several modes of operation, which can be a little intimidating for new users.
Vim or its precursor Vi comes preinstalled on macOS and almost all Linux distributions. Knowing the basics of Vim will help you when you encounter a situation where your favorite editor is not available.
This guide explains how to save a file in Vim / Vi and quit the editor.
Vim Modes
When you launch the Vim editor, you’re in normal mode. In this mode, you can use vim commands and navigate through the file.
To type a text, you need to enter the insert mode by pressing the i key. This mode allows you to insert and delete characters in the same way you do in a regular text editor .
To go back to the normal mode from any other mode, just press the Esc key.
Open a File in Vim / Vi
To open a file using Vim, launch your terminal and type vim followed by the name of the file you want to edit or create:
vim file.textAnother way to open a file is to start the editor and type :e file_name, where file_name is the name of the file you want to open.
Save a File in Vim / Vi
The command to save a file in Vim is :w.
To save the file without exiting the editor, switch back to normal mode by pressing Esc, type :w and hit Enter.
- Press Esc
- Type :w
- Press Enter
There is also an update command i3, which writes the buffer to the file only if there are unsaved changes.
To save the file under a different name, type i4 and hit Enter.
Save a File and Quit Vim / Vi
The command to save a file in Vim and quit the editor is i6.
To save the file and exit the editor simultaneously, press Esc to switch to normal mode, type i6 and hit Enter.
- Press Esc
- Type i6
- Press Enter
Another command to save a file and quit Vim is Esc3.
The difference between these two commands is that Esc3 writes the buffer to the file only if there are unsaved changes, whereas i6 always writes the buffer to file and updates the file modification time.
Quit Vim / Vi without Saving the File
To exit the editor, without saving the changes, switch to normal mode by pressing Esc, type Esc7 and hit Enter.
- Press Esc
- Type Esc7
- Press Enter
Conclusion
In this guide, we have shown you how to save a file in Vim and exit the editor. If you are new to Vim, visit the Open Vim site where you can practice Vim with an interactive tutorial.
Vim is a Linux text editor based on the older Vi editor. Vim is an enhanced version of Vi; many commands work for both Vi and Vim.
This guide will show you how to exit in the terminal or using a shortcut key.
Prerequisites
- A Linux system
- Access to a terminal window / command line (Activities > Search > Terminal)
- A user account with sudo or root privileges (editing some files may require elevated privileges)
Note: If you need a test file for practicing, open a terminal window and enter vim test.txt. Next, press the letter i to switch to insert mode and type a few lines of text.
Exit Vim in Terminal
To issue commands in Vi/Vim, switch to command mode.
1. Press the Esc key.
2. You should see the ––INSERT–– label vanish from the lower-left.
3. To save your changes before you exit, type :w , and then Enter. This will save any changes made. Vim will respond by outputting the filename, whether it’s new, and how many characters were written to the hard drive.
4. To exit Vi/Vim, type :q and hit Enter.
Exit Vim Using a Shortcut Key
In addition to command mode, Vim also has the option for shortcut keys:
- To save a file in Vim and exit, press Esc > Shift + ZZ
- To exit Vim without saving, press Esc > Shift + ZX
More Command Options to Quit Vim
Here’s a list of commands for quitting Vim:
- Esc – switch to command mode
- :w – write out changes that were made
- :q – exit Vim
- :q! – exit Vim and discard any changes
- :wq – saves the changes, and exits Vim
- :i0 – save the changes made, and exits Vim
Note: Make sure to type a colon (i1) first to signify a command. Use the Enter key to activate it.
Conclusion
You should now have several methods to exit Vi/Vim text editor. Next, learn other essential Vim commands.