Categories
Life hack

How to exit the vi or vim editor

Last updated October 30, 2020 By Abhishek Prakash 13 Comments

“How to exit Vim?” “How to quit Vim?” “How do you exit Vi editor?” “How to save and quit Vim?”

These are some of the most googled questions about the Vim editor. Vim, one of the best terminal based editors, is known for its powerful features. Its ardent users swear by it, but it leaves new users baffled because of its “unusual shortcuts.” This even leaves them wondering how to exit from the Vim editor.

In this article, I’ll show you several ways to exit Vim. We’ll also see some interesting fun facts about exiting Vim.

How to exit Vim?

As you have already seen in advanced Vim tips, there are multiple ways to quit Vim. Let me list them one by one, along with the procedure.

Step 1: Enter the command mode by pressing Esc key

Press Esc key: This is very important, because you must exit the edit mode first before typing the exit command(s).

Step 2: Quit vim using commands

Next, you can type one of the following commands to exit Vim in different mode:

  • :q (yes, the colon is included in the command) – This will quit the editor. It may prompt you if you are trying to exit Vim and there are unsaved changes.
  • :q! – Quit Vim without saving the data file (all unsaved changes are discarded).
  • :wq – Save the file and exit Vim.

Step 3: Press enter key

Once you have made your choice of the exit command, press enter to finally quit Vim and close the editor (but not the terminal).

Do note that when you press “:” the editor will have the next keystrokes displayed at the bottom left of the terminal.

How to exit the vi or vim editor

Other ways to exit Vim

Normally, you should remember the above three commands and you should be good with exiting Vim. But as I said earlier, there are more shortcuts to quit Vim. These are the following:

  • Esc + 😡 + Enter (Save and exit)
  • Esc + :qa + Enter (Quit all open files)
  • Esc + Shift ZZ (Save and exit)
  • Esc + Shift ZQ (Exit without saving)

Now it is really up to you to choose how you want to quit the Vim editor. If you ask me, the first method is what you should opt for.

You save (without exiting) with :w command and then you can use :wq to save and quit (w for save and q for quit). It is easier to remember q for quit.

Facts and fun about exiting Vim

If you are feeling a little low because you didn’t know how to quit Vim, don’t, because you are not the only one. According to Stack Overflow, over a million developers worldwide searched for how to exit Vim.

In fact, exiting Vim has become a talking point in the popular culture. Take this tweet, for example. This is one of the most popular jibes at the “complexity” of exiting Vim:

I’ve been using Vim for about 2 years now, mostly because I can’t figure out how to exit it.

The widespread difficulty is somewhat surprising, because if you run Vim you’ll see the information about how to exit it at the splash screen:

But then, obvious things are not that obvious at times. It’s funny that we are so accustomed to Ctrl+S, Ctrl+X shortcuts that it baffles us when we don’t find the same “standard” shortcuts elsewhere.

Vim is an excellent editor. If you can master its features, there will be nothing else like it. SysAdmins particularly spend a lot of time on the command line. Mastering Vim can really be beneficial. At least, you should master the basic vim commands.

I hope this tutorial helped you to save and exit from Vim. Do share your views and thoughts on it.

Like what you read? Please share it with others.

PHP Code

  1. Home
  2. SysAdmin
  3. How to Exit (Quit) Linux Vim/Vi editor

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 vi or vim editor

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.

How to exit the vi or vim editor

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

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.