Cant exit vi? Discover how to quit vi editor properly

Being unable to exit vi/vim is a common frustration, often stemming from confusion between its modes. Follow these precise commands.

Basic Exit Commands (from Normal Mode)

Ensure you are in Normal mode by pressing the Esc key. If unsure, press Esc repeatedly. Then execute one of:

  • :q - Quit vi safely (only works if no unsaved changes exist).
  • :q! - Quit vi immediately, discarding all unsaved changes.
  • :wq - Write (save) changes to the file and then quit.
  • 😡 - Equivalent to :wq (writes only if changes exist, then quits).

Exiting After Making Changes

If you've modified the buffer:

Cant exit vi? Discover how to quit vi editor properly
  • To save changes and quit: Type :wq or 😡 then press Enter.
  • To discard changes and quit: Type :q! then press Enter.

Common Pitfalls & Solutions

  • Accidentally in Insert mode? Press Esc first to return to Normal mode. The cursor should move freely without inserting text.
  • "No write since last change" error? Use :wq to save & quit, or :q! to force quit without saving.
  • Editing a read-only file? To save changes, you need to force the write with :w! first, then :q. Alternatively, save with a different name using :w new_filename, then quit.

Exiting Other Vi States

  • Stuck viewing output/command list? Press q (just the letter 'q') to close the current window or help.

Remember: Mastering the Esc key for mode switching is key. Start in Normal mode before using any command starting with a colon.

Related News