Quick Links

Key Takeaways

  • To exiting vi or vim, press Esc a few times to enter Command mode, then type :q! and Enter to quit without saving changes.
  • To exit vi or wim and commit your changes, press the Escape key to ensure you're in Command mode, then type :wq and hit Enter.
  • vi is a modal editor, meaning editing is in Insert mode and commands are in Command mode. Switch between modes with the Esc and "i" keys.

The vi editor is confusing if you're not used to it. It takes a secret handshake to escape this application if you've stumbled into it. Here's how to quit vi or vim on Linux, macOS, or any other Unix-like system.

How to Exit Vim or Vi Instantly

If you're in vi or vim and need to get out — with or without saving your changes — here's how:

  • First, press the Esc key a few times. This will ensure vi is out of Insert mode and in Command mode.
  • Second, type :q! and press Enter. This tells vi to quit without saving any changes. (If you do want to save your changes, type :wq instead.)

If you want to learn the Linux command line, you'll need to know a lot more than that. Read on and we'll show you just how vi works and why the instructions for quitting are so unusual. vi is an important, powerful tool and the learning curve is worth it.

vi, The Ubiquitous Editor

Because vi is everywhere the chances are you're going to run up against it. You can even find yourself inside vi by accident. Perhaps someone asks you to look at their Linux computer for them. You issue a command like crontab -e, and vi pops up. Surprise, someone has configured the default editor for crontab to be vi.

Perhaps you're administering a system where vi is the only editor, or the only one that will work through a remote SSH session, and you need to edit a user's .bashrc file.

The command to start vi and open a file is straight forward. Type vi, a space, and then the filename. Press Enter. The program that is launched might be vi or it might be vim, an 'improved vi'. It depends on your Linux distribution — for example, Ubuntu uses vim. All of the instructions in this article apply equally to vim.

        vi .bashrc
    
.bashrc in vi

The immediately noticeable difference between vi and other editors is that when vi launches you can't just start typing text. That's because vi is a modal editor. Editing is performed in one mode, the Insert mode, and issuing commands is performed in the Command mode. vi launches into Command mode.

If you're unfamiliar with the concept of Insert mode and Command mode, it can be baffling. A great many of the commands that you can issue in Command mode affect the file you're typing. If you are in Command mode but you're mistakenly trying to type text into your file, it isn't going to end well. Some of the keystrokes you issue will be recognized as commands. Those commands are liable to delete or split lines, move the cursor around, or delete text.

And, no matter what you type, you can't find a way to exit or quit from the editor. Meanwhile, your file is getting pretty mangled and the seemingly random beeps are driving you crazy.

Bad attempt at editing in vi

Command Mode and Insert Mode

You need to switch vi into the appropriate mode for what you're trying to accomplish.

Command mode is the default mode when vi launches. Unless you know better, you'll start trying to type. If you happen to hit the 'i' key, or any of the other 10 keys that invoke Insert mode (a, A, c, C, I, o, O, R, s, and S) you'll suddenly see what you're typing. You're now in Insert mode.

This might feel like progress until you hit one of the arrow keys. If you do that, A, B, C, or D will appear as the only letter on an otherwise blank new line. At the top of the file.

It's OK, we've got your back. This is surprisingly easy when you know how. Remember these two keystrokes: Esc takes you to Command mode and "i" takes you to Insert mode.

You need to be in Command mode, and to enter the correct command to leave the editor.

Enter Command Mode on Vi to Get to Safety

To enter Command mode, hit the Esc key. Nothing visible will happen. Hit it a few more times. If you hear a beep when you hit the Escape key, then you're in Command mode. The beep is telling you "Stop pressing Esc, you're in Command mode, already." If you hear a beep when you hit Esc, we're good.

Type a colon, the letter "q," and an exclamation point, without any spaces. These three characters should appear at the far left of the bottom line of the terminal. If they don't, hit Esc until you hear a beep, and try again. Press the Enter key when you can see them:

:q!

:q! command in vi

In this command q is an abbreviation for quit . The exclamation point adds emphasis, so it's like you're shouting "Quit!" at vi. That might make you feel a little better.

The exclamation point also instructs vi to not save any of the changes you may have made to the file. If you've been blundering about in vi and not knowing what you're doing you probably don't want to save the havoc you've wreaked.

Once you're back at the command line you might want to double-check to make sure the file hasn't been altered. You can do this with the following command:

cat .bashrc | less

.bashrc in less

When you are exiting vi, if you see a message saying "no write since last change," it means you missed the exclamation point off the command. To prevent you quitting and losing any changes you might wish to keep, vi is giving you the chance to save them. Just reissue the :q! command with the exclamation point in place to exit from vi and abandon any changes.

no write since last change message

If You're Sure, Save Your Changes

If you're happy with the changes you've made to your file, you can exit and save the changes using the :wq (write and quit) command. Make sure you are entirely satisfied that you want your screen edits written to the file before you proceed.

Type a colon, the letter w (write) and the letter q (quit). Press the Enter key when you can see them in the lower left of the terminal:

:wq

:wq command in vi

Learning vi Is Worth It.

Using vi is a bit like using a piano. You can't just sit down and use it; you've got to put in some practice. Sitting down to it cold and trying to learn on the fly when the pressure is on you to get something edited is not the way to do it. It makes as much sense as sitting down to a piano for the first time just as the curtain raises for your inaugural concert.

Much of the power of vi comes from its many keystroke combinations that each perform a common editing task. That's great, but you can't benefit from them until you have memorized them, practiced them, and they're part of your muscle memory.

Until then, if you find yourself in vi and looking at an important file, just :q! and exit gracefully. Your important file will thank you.