How To Exit From Vi Editor In Linux – Saving And Quitting Vi With Commands

When you need to leave the vi editor, pressing `Esc` followed by `:q!` will discard any changes and return you to the terminal. Learning how to exit from vi editor in linux is one of the first skills every command-line user must master, because vi is notorious for trapping beginners who cannot figure out how to quit. This guide will show you every safe way to exit vi, whether you want to save your work or abandon changes.

Vi has two main modes: command mode and insert mode. If you are stuck typing text, you are probably in insert mode. Press `Esc` once or twice to return to command mode before trying any exit command. This single step solves most exit problems.

How To Exit From Vi Editor In Linux

To exit vi and save your file, use the command `:wq` from command mode. This writes the file to disk and quits the editor. If you made changes you want to keep, this is the safest option.

To exit without saving, use `:q!`. This forces vi to quit even if there are unsaved changes. It discards everything you did since the last save.

To save without exiting, use `:w`. This writes the file but keeps vi open so you can continue editing.

Basic Exit Commands Summary

  • `:q` – Quit if no changes were made
  • `:q!` – Force quit, discard all changes
  • `:wq` – Write and quit
  • `:x` – Write and quit (same as `:wq`)
  • `ZZ` – Save and quit (capital Z, no colon needed)
  • `ZQ` – Quit without saving (capital Z and Q)

Step-By-Step: How To Exit Vi When Stuck

  1. Press `Esc` at least once to ensure you are in command mode
  2. Type `:` to enter command-line mode (you will see a colon at the bottom left)
  3. Type `q!` and press `Enter`
  4. You should return to the terminal prompt

If nothing happens after pressing `Esc`, try pressing it three or four times. Some terminal emulators lag, and vi might not register the first press.

Common Exit Problems And Solutions

Why Can’t I Exit Vi?

Most people cannot exit vi because they are still in insert mode. The screen shows `– INSERT –` at the bottom when this happens. Press `Esc` to leave insert mode, then try your exit command again.

Another reason is that the file is read-only. If you opened a file without write permission, `:wq` will fail. Use `:q!` to quit without saving, or use `:wq!` to force write if you have sudo access.

What If Vi Freezes?

If vi becomes completely unresponsive, you can kill the process from another terminal. Open a second terminal window and run `killall vi` or `pkill vi`. This is a last resort and may leave temporary files behind.

You can also use `Ctrl+Z` to suspend vi and return to the shell. Then run `kill %1` to terminate the suspended job.

Using Vi Ex Commands For Advanced Exits

Vi’s command mode supports ex commands, which are powerful tools for file management. You can combine exit commands with other operations.

Write And Quit With A New Filename

Use `:w newfile.txt` to save the current buffer to a different filename. Then use `:q` to quit. This is useful when you want to keep the original file unchanged.

Quit All Open Files

If you have multiple files open in vi (using `:e` or `vim file1 file2`), use `:qa` to quit all. Add `!` to force quit without saving: `:qa!`.

Save And Quit With Error Checking

Use `:wq!` to force write even if the file is read-only (requires appropriate permissions). Use `:x` as a shorter alternative to `:wq`.

Vi Vs Vim: Exit Command Differences

Vi and Vim share most exit commands, but Vim has additional features. Vim supports `:qall` to quit all windows, and `:cq` to quit with an error code (useful for scripts).

Vim also has a graphical mode (gvim) where you can use the mouse to click the close button. But the keyboard commands work the same way.

Checking Your Vi Version

Run `vi –version` or `vim –version` in the terminal to see which editor you are using. Most modern Linux systems alias vi to vim, so you likely have Vim installed.

Exiting Vi Without Saving Changes

Sometimes you open a file just to look at it, or you make changes you regret. Use `:q!` to exit without saving. This is the emergency exit command every user should memorize.

If you already saved changes and want to undo them, you cannot use `:q!` alone. You would need to revert to a previous version using version control or backup files.

Using ZQ For Quick Exit

In command mode, type `ZQ` (capital Z, then capital Q) to quit without saving. This is faster than typing `:q!` because you do not need to press Enter. But it only works if you are already in command mode.

Exiting Vi And Saving Changes

To save your work and exit, use `:wq` or `ZZ`. The `ZZ` command is two capital Z’s typed in quick succession. It writes the file and quits automatically.

If you want to save to a different filename, use `:w newname.txt` followed by `:q`. Or combine them: `:w newname.txt | q`.

What Happens To Swap Files?

When you exit vi normally, it deletes the swap file (`.filename.swp`). If vi crashes or you kill it, the swap file remains. Next time you open the file, vi will warn you about the recovery file. You can delete the swap file manually with `rm .filename.swp`.

Exiting Vi In Different Modes

Exiting From Insert Mode

You cannot exit vi directly from insert mode. Press `Esc` first to return to command mode. Then use any exit command. If you are typing and suddenly need to quit, press `Esc` and immediately type `:q!`.

Exiting From Visual Mode

Visual mode is used for selecting text. Press `Esc` to leave visual mode and return to command mode. Then exit normally.

Exiting From Command-Line Mode

If you are typing a command (like `:wq`), you can press `Esc` or `Ctrl+C` to cancel the command and return to command mode. Then start over.

Keyboard Shortcuts For Exiting Vi

Some users prefer keyboard shortcuts over typing commands. Here are the most useful ones:

  • `ZZ` – Save and quit (like `:wq`)
  • `ZQ` – Quit without saving (like `:q!`)
  • `Ctrl+W Q` – Close current window in Vim
  • `Ctrl+C` – Cancel current command

These shortcuts work only in command mode. If you are in insert mode, press `Esc` first.

Exiting Vi When Using Multiple Windows

If you have split windows in Vim (using `:split` or `:vsplit`), you can close each window individually or all at once.

Close Current Window

Use `:q` to close the active window. If it is the last window, vi will exit.

Close All Windows

Use `:qa` to quit all windows. Add `!` to force quit: `:qa!`. Use `:wqa` to write all files and quit.

Exiting Vi In Recovery Mode

If vi crashed and you are prompted to recover a file, you have a few options:

  • Press `R` to recover the file
  • Press `D` to delete the swap file
  • Press `Q` to quit without recovering

After recovering, save the file and exit normally. Then delete the swap file manually.

Exiting Vi From A Remote Session

If you are using vi over SSH, the same commands work. But network issues can cause problems. If your SSH session freezes, you might need to kill the remote vi process.

Open a new terminal and SSH into the same machine. Run `ps aux | grep vi` to find the process ID, then `kill -9 PID` to force quit.

Exiting Vi In A Docker Container

Inside a Docker container, vi works the same way. But if the container is running without a terminal, you might not be able to interact with vi. In that case, use `docker exec -it container_name bash` to get an interactive shell.

Exiting Vi In A Script

If you are writing a script that uses vi, you can automate exiting with `vi -c ‘wq’ filename`. This opens vi, runs the command, and exits. Useful for batch editing.

For example: `vi -c ‘%s/old/new/g’ -c ‘wq’ file.txt` replaces all occurrences of “old” with “new” and saves.

Common Mistakes When Exiting Vi

Typing Commands In Insert Mode

If you type `:wq` while in insert mode, vi will insert those characters into your file. Always press `Esc` first.

Using Wrong Case

Commands like `ZZ` and `ZQ` are case-sensitive. Lowercase `zz` does nothing. Make sure you use capital letters.

Forgetting The Colon

Commands like `q` and `wq` require a colon at the beginning. Typing just `q` in command mode does nothing. Always start with `:`.

Exiting Vi In Different Linux Distributions

Vi works the same across all Linux distributions because it is a standard tool. However, some distros alias vi to vim, while others use the original vi. The exit commands are identical.

If you are using a minimal installation like Alpine Linux, vi might be the BusyBox version, which has fewer features but the same exit commands.

Exiting Vi In Terminal Emulators

Different terminal emulators handle vi differently. Some have issues with the `Esc` key. If `Esc` does not work, try `Ctrl+[` instead, which sends the same escape signal.

If you are using a terminal like PuTTY on Windows, the `Esc` key might be mapped differently. Check your terminal settings.

Exiting Vi Without A Mouse

Vi is designed for keyboard-only use. You do not need a mouse to exit. All commands are typed on the keyboard.

If you are using gvim (graphical Vim), you can click the close button, but it is faster to use keyboard commands.

Exiting Vi And Undoing Changes

If you accidentally save changes and want to undo them, you cannot use `:q!` because the file is already saved. You need to revert using version control (like Git) or restore from a backup.

Vi does not have an undo-after-save feature. Always double-check before using `:wq`.

Exiting Vi When The File Is Read-Only

If you opened a file without write permission, `:wq` will fail. Use `:q!` to exit without saving. If you must save, use `:wq!` with sudo privileges, or save to a different location.

Example: `:w ~/tempfile.txt` saves a copy to your home directory.

Exiting Vi With Multiple Buffers

If you have multiple files open in buffers (using `:e` or `:badd`), use `:q` to close the current buffer. Use `:q!` to force close without saving. Use `:wq` to save and close.

To close all buffers and exit, use `:qa` or `:wqa`.

Exiting Vi In A Hurry

If you need to exit vi immediately, press `Esc` then type `:q!` and press Enter. This is the fastest way to quit without saving.

If you want to save quickly, use `ZZ` (two capital Z’s). This saves and quits in one keystroke.

Exiting Vi And Preserving Undo History

Vi’s undo history is lost when you exit. If you want to keep undo history between sessions, you need to use Vim’s persistent undo feature. Add `set undofile` to your `.vimrc` file.

Then when you exit with `:wq`, the undo history is saved to a file. Next time you open the file, you can undo changes from the previous session.

Exiting Vi In A Text-Only Environment

If you are in a text-only terminal (no GUI), vi works the same way. The exit commands are identical. The only difference is that you cannot use a mouse.

Exiting Vi And Returning To A Specific Line

When you exit vi, it remembers your cursor position if you have the `viminfo` feature enabled. Next time you open the file, vi will place the cursor where you left off.

This is controlled by the `’1000` setting in `.vimrc`. It saves up to 1000 marks across sessions.

Exiting Vi In A Non-English Locale

Vi works the same regardless of language settings. The exit commands are English-based and do not change with locale. However, error messages might be translated.

Exiting Vi And The Terminal History

When you exit vi, your terminal history is unaffected. You can scroll back and see commands you ran before opening vi.

Exiting Vi And Background Processes

Vi does not run background processes, so exiting is safe. There are no cleanup tasks needed.

Exiting Vi In A Chroot Environment

Inside a chroot, vi works normally. But if the chroot does not have vi installed, you cannot use it. Install vi with `apt-get install vim` or `yum install vim`.

Exiting Vi And File Permissions

If you try to save a file without write permission, vi will warn you. Use `:q!` to exit without saving, or use `:w!` to force write if you have sudo access.

To save with sudo from within vi, use `:w !sudo tee %`. This runs the write command through sudo.

Exiting Vi And Symbolic Links

Vi handles symbolic links normally. When you save, it writes to the target file, not the link. Exiting works the same way.

Exiting Vi And Large Files

Large files can cause vi to slow down, but exit commands still work. If vi is unresponsive, wait a few seconds for it to process the command.

Exiting Vi And Binary Files

Vi can open binary files, but saving changes might corrupt them. Use `:q!` to exit without saving. If you need to edit binary files, use a hex editor instead.

Exiting Vi And Encrypted Files

Vi supports encrypted files with the `-x` option. When you exit, the file is saved encrypted. Use `:wq` to save and quit normally.

Exiting Vi And File Backups

Vi creates backup files (ending with `~`) if you have `set backup` enabled. These are deleted when you exit normally. If vi crashes, backup files remain.

Exiting Vi And The Clipboard

Vi does not interact with the system clipboard by default. If you yanked text, it is lost when you exit. Use `”+y` to yank to the system clipboard in Vim.

Exiting Vi And Macros

If you recorded a macro, it is lost when you exit unless you save it to your `.vimrc` or a register file.

Exiting Vi And Syntax Highlighting

Syntax highlighting does not affect exit commands. It is purely visual.

Exiting Vi And Plugins