sodcalculator

10 Tech How-To

How to Exit Vi in Linux: A Simple Guide

Guide 10-13380 · Updated · Step by step

Vi is a powerful text editor that ships with virtually every Linux and Unix system, but its modal interface confuses many newcomers. The most common problem people run into is not knowing how to leave the editor once they are inside it. This guide walks you through every reliable method to exit vi, from the simplest keystroke to forced quits when the editor refuses to cooperate.

What You’ll Need

  • A Linux or Unix system with vi installed (it is almost always present by default)
  • A terminal window open
  • A file open in vi (or the ability to create one by typing vi test.txt and pressing Enter)

Step-by-Step

Vi has two main modes: Command mode (where keys trigger commands) and Insert mode (where typing enters text). You must be in Command mode to exit. Here is the process:

  1. Press the Esc key one or more times. This guarantees you are in Command mode, regardless of what you were doing. If you are already in Command mode, nothing happens—that is fine.
  2. Decide whether you want to save your changes. If you want to keep the edits you made, you will use a command that writes the file. If you want to discard all changes since the last save, you will use a command that quits without writing.
  3. To save and exit: type :wq and press Enter. The colon enters command-line mode, w writes (saves) the file, and q quits. This is the most common way to exit vi after making changes.
  4. To exit without saving: type :q! and press Enter. The ! forces the quit, overriding the warning about unsaved changes. Use this when you want to abandon your edits.
  5. To just quit (only works if no changes were made): type :q and press Enter. If you have unsaved changes, vi will refuse and show an error like E37: No write since last change.
  6. Alternative: save and exit with a shortcut. In many vi builds, you can press ZZ (uppercase, two Zs in a row) while in Command mode to save and quit in one motion—no colon needed.
  7. If vi is completely frozen or unresponsive: press Esc a few times, then try :q!. If that fails, you can press Ctrl+C to interrupt, or in the worst case, open another terminal and run pkill vi to kill the process (this loses unsaved changes).

Common Mistakes

  • Typing while in Command mode: If you press keys like i, a, or o, you enter Insert mode and start typing text into the file. This is why pressing Esc first is essential.
  • Forgetting the colon: Typing wq without the colon just moves the cursor or does nothing visible. The colon is required to enter command-line mode.
  • Using lowercase vs. uppercase: ZZ (save and quit) is uppercase. zz (lowercase) scrolls the screen to center the cursor—it does not exit.
  • Hitting Ctrl+Z instead of Esc: Ctrl+Z suspends vi and returns you to the shell prompt. This is not an exit; vi is still running in the background. To get back, type fg and press Enter.
  • Pressing Ctrl+C expecting it to quit: In vi, Ctrl+C often just beeps or cancels the current command. It does not reliably exit the editor.

Tips

  • Memorize the three essential commands: :wq (save and exit), :q! (exit without saving), and ZZ (save and exit). These cover 95% of situations.
  • Use :w frequently while editing. If you save often, a later :q will work without the force flag, and you lose less work if something crashes.
  • Check your mode with the status line. In many vi versions, the bottom of the screen shows -- INSERT -- when you are in Insert mode. If you do not see that, you are in Command mode.
  • Learn :x as an alternative to :wq. It writes only if the file was modified, which can be slightly faster on large files.
  • If you are new, try vim’s interactive tutorial. Run vimtutor in the terminal for a 30-minute guided lesson that covers exiting and much more.

Troubleshooting

If the standard commands do not work, work through these checks:

  • “E37: No write since last change” error: This means you have unsaved changes and tried :q. Either type :wq to save and exit, or :q! to discard changes and exit.
  • “E32: No file name” error: You opened vi without a filename (just ran vi). To save, you must provide a name: type :w filename.txt then :q. Or use :wq filename.txt to do both at once.
  • Pressing Esc does not seem to work: Some terminals interpret Esc as part of a sequence. Press Esc and wait half a second before typing the colon. If that fails, press Ctrl+[ which is the raw escape sequence in most terminals.
  • You are stuck in Insert mode and Esc is unresponsive: Try pressing Ctrl+C, then Esc, then :q!. If the terminal is truly stuck, you may need to close the terminal window (which loses unsaved changes) or use another terminal to kill the vi process.
  • You accidentally suspended vi with Ctrl+Z: You are back at the shell prompt. Type jobs to see the suspended process, then fg to bring it back to the foreground. Then use :q! or :wq normally.
  • vi says “Not an editor command: q”: This usually means you typed something like :q while in Insert mode. Press Esc first, then retry.

Frequently Asked Questions

Q: How do I exit vi without saving changes?
A: Press Esc to ensure Command mode, then type :q! and press Enter. The exclamation mark forces vi to quit without writing the file, discarding all unsaved edits.

Q: What is the difference between :q and :q!?
A: :q quits only if there are no unsaved changes. :q! quits unconditionally, discarding any changes you have made since the last save. Use :q! when you want to abandon your work.

Q: How do I save and exit in one command?
A: Press Esc, then type :wq and press Enter. This writes the file to disk and exits vi. Alternatively, press ZZ (uppercase) in Command mode for the same result without typing a colon.

Q: Why does vi keep beeping when I type commands?
A: The beep usually means you are in Command mode and the key you pressed is not a valid command, or you are in Insert mode and typed something that vi interprets as a control character. Press Esc to reset, then carefully type the exact command with the colon.

Q: Can I exit vi with Ctrl+D or Ctrl+C?
A: Generally no. Ctrl+C cancels the current operation but does not exit vi. Ctrl+D in Command mode scrolls down half a page. The reliable exits are :q, :q!, :wq, and ZZ.

Q: What if I closed the terminal by mistake while in vi?
A: If the terminal window is closed, vi is terminated and any unsaved changes are lost. If you used Ctrl+Z to suspend instead, you can recover with fg in the shell. To avoid this, get into the habit of saving with :w frequently.

Related

General steps for current mainstream versions; menus can differ by device and version. See the disclaimer or report an error.

More in this bed

The whole Tech How-To bed →