Introduction

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.


Note: Some Linux distributions, such as Ubuntu 18.04 and 20.04, may come with the minimal version of Vim installed. Follow our guide to install the full version of Vim on Ubuntu.


How to exit the Vim in terminal or using shortcuts in linux

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.

Save a file in Vim.

4. To exit Vi/Vim, type :q and hit Enter.

Exit from Vim text editor.


Note: Want to personalize your Vim interface and add syntax highlighting? Learn How to Change and Use Vim Color Schemes.


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
  • :x – save the changes made, and exits Vim

Note: Make sure to type a colon (:) 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.


Next you should also read