How one Exit the Vi or Vim editor

the vi Editor is confusing when you’re not used to it. It takes a secret handshake to escape this application if you stumbled on it. Here’s how to exit vi or vim on Linux, macOS, or any other Unix-like system.

The fast answer

If you are there vi or vim and have to exit – with or without saving your changes – proceed as follows:

  • First, press the Esc key a few times. This will ensure vi is out of insert mode and in command mode.
  • Second, enter :q! and press Enter. That says vi exit without saving changes. (If you want to save your changes, type :wq instead of this.)

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

vi, the ubiquitous editor

because vi Anywhere there is a good chance that you will come across them. You can even find yourself in it vi inadvertently. Someone may ask you to look for their Linux computer. You give an order like crontab -e , and vi pop up. Surprise, someone has configured the default editor for crontab to be vi.

Maybe you manage a system in which vi is the only editor or the only one that works over a remote SSH session and you need to edit a user’s .bashrc file.

The command to start vi and opening a file is easy. Type vi , a space and then the file name. Press Enter. The started program could be: vi or it could be vim , an “improved” vi‘. It depends on your Linux distribution – for example, Ubuntu used vim . All instructions in this article apply equally to vim.

 vi .bashrc

The immediately noticeable difference between vi and other editors is the if vi starts, you cannot just start entering text. That’s because vi is a modal editor. Editing is done in one mode, the insert mode, and commands are issued in command mode. vi starts in command mode.

If you are not familiar with the concepts of insert mode and command mode, it can be confusing. Many of the commands you can run in Command mode affect the file you enter. If you’re in command mode but accidentally try to enter text into your file, it won’t end well. Some of the keystrokes you make will be recognized as commands. These commands can delete or split lines, move the cursor, or delete text.

And no matter what you type, you cannot exit or exit the editor. Meanwhile, your file will get pretty garbled and the seemingly random beeps will drive you crazy.

Bad attempt to edit in vi

Command mode and insert mode

You have to switch vi in the appropriate mode for what you want to achieve.

Command mode is the default mode when vi starts. If you don’t know better, try typing. If you happen to hit the ‘i’ key or any of the other 10 keys that call up insert mode (a, A, c, C, I, o, O, R, s, and S), all of a sudden you see what you’re typing. You are now in insert mode.

This can feel like progress until you press one of the arrow keys. When you do this, A, B, C, or D appear as the only letter on an otherwise empty new line. At the beginning of the file.

It’s okay we have your back. This is surprisingly easy once you know how. Remember these two key combinations: Esc will put you in command mode and “i” will put you in insert mode.

You must be in command mode and enter the correct command to exit the editor.

From command mode to security

To enter command mode, press the Esc key. Nothing visible will happen. Hit it a few more times. If you hear a beep when you press the escape key, you are in command mode. The beep tells you, “Stop pressing Esc, you are already in command mode.” If you hear a beep when you press Esc, we’re good.

Enter a colon, the letter “q”, and an exclamation mark with no spaces. These three characters should appear on the far left of the bottom line of the terminal. If it doesn’t, press Esc until you hear a beep and try again. Press the Enter Keys when you can see them:


: Q!  Command in vi

In this command q is an abbreviation for quit . The exclamation point adds emphasis so it’s like yelling “Quit!” at vi. This may make you feel a little better.

The exclamation point also points vi so as not to save any changes you may have made to the file. If you stumbled into vi And if you don’t know what you’re doing, you probably don’t want to save the mess you made.

Once you’re back at the command line, double-check that the file hasn’t been modified. You can do this with the following command:

cat .bashrc | less

.bashrc in less

When you go out viIf you see the “No typing since last modified” message, it means you missed the command’s exclamation point. To prevent you from exiting the program and losing any changes you might want to keep, vi gives you the chance to save her. Just reissue :q! Command with the exclamation mark to exit vi and discard all changes.

no writing since the last change message

When you are sure, save your changes

When you’re happy with the changes you’ve made to your file, you can click the button to exit and save your changes :wq (write and exit) command. Make sure you are completely confident that your screen edits will be written to the file before proceeding.

Enter a colon, the letter w (write), and the letter q (exit). Press the Enter Button when you can see it in the lower left corner of the terminal:


: wq command in vi

The learning curve is worth it

Using vi is a bit like with a piano. You can’t just sit down and use it; you have to practice something. Sitting down cold and trying to learn on the fly when the pressure is on you to get something done is not the way to go. It makes so much sense to sit down at a piano for the first time while the curtain rises on your inaugural concert.

Much of the power of vi comes from its many keyboard shortcuts, each performing a common editing task. This is great, but you cannot benefit from them until you have memorized and practiced them and they are part of your muscle memory.

Until then, when you find yourself again vi and view an important file, easy :q! and gracefully abandoned. Your important file will thank you.

Related Posts