How to Exit (Quit) Linux Vim/Vi editor

June 17, 2020

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.

Was this article helpful?
YesNo
Sofija Simic
Sofija Simic is an experienced Technical Writer. Alongside her educational background in teaching and writing, she has had a lifelong passion for information technology. She is committed to unscrambling confusing IT concepts and streamlining intricate software installations.
Next you should read
How to Save a File in Vi / Vim & Exit
September 22, 2022

Vim (Vi IMproved) is a well-known, open-source text editor for Linux...
Read more
How to Cut, Copy and Paste in Vim / Vi
April 6, 2020

Complete guide on how to use crucial Vim commands - copying...
Read more
How to Delete Line in Vim on Linux
April 22, 2020

Vim allows you to delete entire lines, words or characters using...
Read more
How to Show or Hide Line Numbers in Vim / Vi
February 28, 2022

You can activate line numbering in Vim by showing absolute, relative...
Read more