How to exit from vi editor in linux

How to exit from vi editor in linux

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.

The Quick Answer

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

How to exit from vi editor in linux

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.

How to exit from vi editor in linux

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.

From Command Mode 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!

How to exit from vi editor in linux

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

How to exit from vi editor in linux

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.

How to exit from vi editor in linux

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

How to exit from vi editor in linux

The Learning Curve 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.

In this article, we will learn how to exit Vi/Vim (after referred to as Vim) text editor using simple commands. In a previous article, we explained a simple tip on how to save a file in Vi or Vim after making changes to a file.

Before we move any further, if you are new to Vim, then we recommend reading through these 10 reasons why you should stick to using Vi/Vim text editor in Linux.

To open or create a new file using Vi/Vim, simply type the commands below, then press i to switch to insert mode (insert text):

$ vim file.txt OR $ vi file.txt
How to exit from vi editor in linux
Press ‘i’ to Insert Mode in Vim Editor

After making changes to a file, press [Esc] to shift to the command mode and press :w and hit [Enter] to save a file.

How to exit from vi editor in linux
Save File in Vim

To exit Vi/Vim, use the :q command and hit [Enter].

How to exit from vi editor in linux
Exit File in Vi Editor

To save a file and exit Vi/Vim simultaneously, use the :wq command and hit [Enter] or :x command.

How to exit from vi editor in linux
Save and Exit File in Vi

If you make changes to a file but try to quite Vi/Vim using ESC and q key, you’ll receive an error as shown in the scrrenshot below.

How to exit from vi editor in linux
Vi Error Quitting File

To force this action, use ESC and :q!.

How to exit from vi editor in linux
Force Quit Vi File in Linux

Additionally, you can use shortcut methods. Press the [Esc] key and type Shift + Z Z to save and exit or type Shift+ Z Q to exit without saving the changes made to the file.

Having learned the above commands, you can now proceed to learn advanced Vim commands from the links provided below:

In this article, we learned how to exit Vim text editor using simple commands. Do you have any questions to ask or any thoughts to share? Please, use the feedback form below.

TecMint is the fastest growing and most trusted community site for any kind of Linux Articles, Guides and Books on the web. Millions of people visit TecMint! to search or browse the thousands of published articles available FREELY to all.

If you like what you are reading, please consider buying us a coffee ( or 2 ) as a token of appreciation.

How to exit from vi editor in linux

We are thankful for your never ending support.