How to Exit the Vi or Vim Editor
-
To exit vi or vim without saving changes, press Esc a few times to enter Command mode, then type :q! and hit Enter.
-
To exit vi or vim 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.
Thevieditor 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 inviorvimand need to get out—with or without saving your changes—here's how:
-
First, press the Esc key a few times. This will ensureviis out of Insert mode and in Command mode.
-
Second, type:q!and press Enter. This tellsvito quit without saving any changes. (If you do want to save your changes, type:wqinstead.)
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 howviworks and why the instructions for quitting are so unusual.viis an important, powerful tool and the learning curve is worth it.
vi, The Ubiquitous Editor
Becauseviis everywhere the chances are you're going to run up against it. You can even find yourself insideviby accident. Perhaps someone asks you to look at their Linux computer for them. You issue a command likecrontab -e, andvipops up. Surprise, someone has configured the default editor for crontabto bevi.
Perhaps you're administering a system whereviis 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 startviand open a file is straight forward. Typevi, a space, and then the filename. Press Enter. The program that is launched might bevior it might bevim, an "improvedvi." It depends on your Linux distribution—for example, Ubuntu usesvim. All of the instructions in this article apply equally tovim.
The immediately noticeable difference betweenviand other editors is that whenvilaunches you can't just start typing text. That's becauseviis a modal editor. Editing is performed in one mode, the Insert mode, and issuing commands is performed in the Command mode.vilaunches 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.
Command Mode and Insert Mode
You need to switchviinto the appropriate mode for what you're trying to accomplish.
Command mode is the default mode whenvilaunches. 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:
In this commandqis an abbreviation forquit. The exclamation point adds emphasis, so it's like you're shouting "Quit!" atvi. That might make you feel a little better.
The exclamation point also instructsvito not save any of the changes you may have made to the file. If you've been blundering about inviand 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:
When you are exitingvi, 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,viis giving you the chance to save them. Just reissue the:q!command with the exclamation point in place to exit fromviand abandon any changes.
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:
Learning vi Is Worth It.
Usingviis 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 ofvicomes 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 inviand looking at an important file, just:q!and exit gracefully. Your important file will thank you.
