What is the command to quit vi even though latest changes have not been saved for this vi call?


Solaris Advanced User's Guide

  • Previous: Two Modes of vi
  • Next: Printing a File

vi is rich in substantively synonymous commands that control the save of the buffer contents to a file and the exit from vi. These commands give you the option of saving, saving-and-quitting, or quitting-without-saving.

Saving

Save the contents of the buffer (write the buffer to the file on disk) by typing:

Press Return.

Saving and Quitting

Save and quit by typing:

Press Return. Alternatively, type ZZ.

Note that the command ZZ is neither preceded by a colon nor followed by Return.

Quitting Without Saving

When you've made no changes to a file and want to quit, type:

Press Return. If you have made changes, vi does not let you quit with :q. Instead, it displays the following message.


No write since last change (:quit! overrides)

If you do not want to save your changes, type:

Press Return.

  • Previous: Two Modes of vi
  • Next: Printing a File

What is the command to quit vi even though latest changes have not been saved for this vi call?

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

What is the command to quit vi even though latest changes have not been saved for this vi call?

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.

What is the command to quit vi even though latest changes have not been saved for this vi call?

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!

What is the command to quit vi even though latest changes have not been saved for this vi call?

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

What is the command to quit vi even though latest changes have not been saved for this vi call?

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.

What is the command to quit vi even though latest changes have not been saved for this vi call?

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

What is the command to quit vi even though latest changes have not been saved for this vi call?

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.

Introduction

Vim (Vi IMproved) is an open-source text editor for Unix and Linux systems. It is a powerful and stable tool for editing and writing text, both in the command-line interface and as an independent application in a GUI.

Take the first step in learning Vim with the basic save and exit commands. Follow this guide and learn how to save a file in Vi/Vim and exit the editor.

What is the command to quit vi even though latest changes have not been saved for this vi call?

Prerequisites

  • A system running Linux.
  • A user with sudo privileges.
  • Access to a terminal/command line.
  • Vim installed (alternatively, use the default Vi).

When Vim launches, files are opened in command mode by default. This means that you can move around and edit the file, but cannot insert new text.

All alphanumeric keys in this mode are equal to commands, and pressing them will not display their value on the screen. For example, pressing the letter w will move the cursor one word forward.

To type text, you must be in insert mode. To switch to insert mode, press the i key. Now you can type text into a file.

To switch back to command mode, press the Esc button.

Vim offers different commands to save changes to a file, depending on whether you need to exit the text editor or not. Below are examples demonstrating how to save and exit, save without exiting, and exit without saving.

Start by opening a new text file using Vim:

vim example_file.txt

Next, press i to enter insert mode and add some text.

To save a file and exit Vim, do the following:

1. Switch to command mode by pressing the Esc key.

2. Press : (colon) to open the prompt bar in the bottom left corner of the window.

3. Type x after the colon and hit Enter. This will save the changes and exit.

What is the command to quit vi even though latest changes have not been saved for this vi call?

Alternatively, a command that will also save a file and exit the text editor is:

:wq

In which case the w stands for write (save) and q stands for quit.

To save a file without exiting in Vim:

1. Switch to command mode by pressing the Esc key.

2. Type : (colon). This will open the prompt bar in the bottom left corner of the window.

What is the command to quit vi even though latest changes have not been saved for this vi call?

3. Type w after the colon and hit Enter. This will save in Vim the changes made to the file, without exiting.

You can also rename an existing file by adding the new name after the commands.

:w [newfilename]

In the picture below, we renamed the existing example1.txt into newexample.

What is the command to quit vi even though latest changes have not been saved for this vi call?

To exit Vim without saving changes:

1. Switch to command mode by pressing the Esc key.

2. Press : (colon) to open the prompt bar in the bottom left corner of the window.

3. Type q! after the colon and hit Enter to exit without saving the changes.

What is the command to quit vi even though latest changes have not been saved for this vi call?

The q! quits the text editor.

Conclusion

This article explained how to save files in Vi / Vim, with multiple command options.

The more comfortable you feel using keyboard shortcuts, the more you will understand why Vim earned its worldwide reputation. Next learn how to copy and paste in Vim, and how to go to the end of the file!