Exiting vim in Linux can be accomplished with the `:q` command, though you may need `:q!` to force quit without saving. Knowing how to exit vim in linux is a rite of passage for anyone learning the command line, as this powerful editor can feel confusing at first.
Many new users get stuck inside vim and don’t know how to leave. This guide will show you every method to exit vim, from basic commands to advanced tricks.
How To Exit Vim In Linux
Vim has different modes, and you must be in the right one to exit. The normal mode is where you type commands. If you are typing text, you are in insert mode.
Press the Esc key to return to normal mode. Then you can use the colon commands to quit.
Basic Exit Commands
These are the most common ways to leave vim. Memorize these first.
- :q – Quit if no changes were made
- :q! – Force quit without saving changes
- :wq – Write (save) and quit
- 😡 – Save and quit (same as :wq)
- ZZ – Save and quit (no colon needed)
- ZQ – Quit without saving (no colon needed)
Type the colon first, then the letter, then press Enter. For example, type :q and press Enter.
When You Cannot Exit Normally
Sometimes vim will refuse to quit. This happens when you have unsaved changes. The editor shows an error message like “No write since last change.”
Use :q! to override this. The exclamation mark forces vim to ignore the warning and exit.
Another common issue is a modified buffer. If you opened multiple files, you might need :qa! to quit all at once.
Exiting With Multiple Files
When you edit several files in one vim session, quitting gets more complex.
- :q – Quits the current file only
- :qa – Quits all files
- :qa! – Force quit all files without saving
- :wqa – Save all files and quit
You can also use :wall to save all files, then :qall to exit.
Using The Mouse To Exit
If you have mouse support enabled in vim, you can click on the menu bar. This is rare in terminal vim but works in gvim.
In gvim, click File then Quit. Or click the X button on the window title bar. This is the easiest method for graphical users.
For terminal vim, mouse support must be turned on with :set mouse=a. Then you can click on the bottom line to select commands.
Keyboard Shortcuts For Fast Exit
Power users prefer keyboard shortcuts. These save time once you learn them.
- ZZ – Save and quit (like :wq)
- ZQ – Quit without saving (like :q!)
- Ctrl + w + q – Quit the current window
- Ctrl + c – Cancel current command
Notice that ZZ and ZQ use uppercase letters. Hold Shift while pressing Z, then press Z or Q.
Exiting Vim When Stuck In Insert Mode
New users often get stuck because they are in insert mode. The colon commands do not work here.
Press Esc once to leave insert mode. If that does not work, try Ctrl + c or Ctrl + [. These also return you to normal mode.
If you still cannot exit, try Ctrl + z to suspend vim. This puts you back in the shell. Then type fg to return to vim, or kill %1 to terminate it.
Exiting Vim Without Saving Changes
Sometimes you want to discard all edits. Use :q! to force quit.
If you already saved some changes but want to undo them, you cannot simply quit. You need to revert the file. Use :e! to reload the original file, then quit.
Another option is :cq which quits with an error code. This tells scripts that the edit failed.
Exiting Vim And Saving Changes
To save your work and leave, use :wq or :x. Both write the file and exit.
If you want to save to a different filename, use :w newfile.txt then :q. Or combine them: :w newfile.txt | q.
You can also use :saveas newfile.txt which saves and keeps editing the new file. Then quit normally.
Exiting Vim With A Specific Line Number
You can tell vim to go to a line number when you open a file. But you can also exit and return to a specific line.
Use :wq normally. When you reopen the file, vim remembers your cursor position if you have the viminfo feature enabled.
To force a specific line on next open, use :wq then reopen with vim +10 filename.txt. This opens the file at line 10.
Exiting Vim When Using Tabs
Vim supports tabs like a web browser. Each tab can have multiple windows.
- :tabclose – Close the current tab
- :tabclose! – Force close without saving
- :tabonly – Close all other tabs
- :q – Quit the current window, may close tab if last window
To exit all tabs at once, use :qa or :wqa.
Exiting Vim When Using Windows
Vim windows split the screen. Each window shows a different file or part of a file.
- :q – Quits the current window
- :close – Closes the current window (same as :q)
- :only – Keeps only the current window
- Ctrl + w + q – Quit the current window
If you have unsaved changes in a window, vim will warn you. Use :q! to force close that window.
Exiting Vim When It Freezes
Rarely, vim may become unresponsive. This can happen with large files or network issues.
First, try Esc several times. Then try Ctrl + c. If nothing works, use Ctrl + z to suspend vim.
From the shell, you can kill vim with killall vim or pkill vim. This loses unsaved changes.
Another method is to open a second terminal and run kill -9 $(pgrep vim). This forces termination.
Exiting Vim With A Custom Command
You can create custom key mappings to exit faster. Add these to your .vimrc file.
:nnoremap <Leader>q :q!<CR>
:nnoremap <Leader>w :wq<CR>
Now pressing \q (backslash then q) will force quit. Pressing \w will save and quit.
You can also map F2 to exit. Add :nnoremap <F2> :wq<CR> to your config.
Exiting Vim From Insert Mode Directly
Some users want to exit without pressing Esc first. This is possible with mappings.
Add this to your .vimrc:
:inoremap jj <Esc>:wq<CR>
Now typing jj in insert mode will save and quit. Change jj to any key sequence you prefer.
Be careful not to use common letter combinations. Typing “jj” in normal text will trigger the exit.
Exiting Vim In A Script Or Pipeline
When using vim in scripts, you need non-interactive exit. Use the -c flag to run commands.
Example: vim -c "wq" filename.txt opens the file, saves, and exits.
For batch processing, use vim -c "%s/old/new/g" -c "wq" filename.txt. This replaces text and saves.
You can also use ex or vi for scripting. These are minimal versions of vim.
Exiting Vim When You Opened A Directory
Vim can open directories with netrw. You see a file list instead of file contents.
To exit, use :q normally. If you are in the file explorer, press Enter on a file to open it, then quit.
You can also press i to change the listing style, then :q to exit.
Exiting Vim And Returning To A Shell
Sometimes you want to run a shell command without leaving vim. Use :!command to run a command.
For example, :!ls shows the directory listing. Press Enter to return to vim.
To get a full shell, use :shell or :sh. This opens a subshell. Type exit to return to vim.
You can also suspend vim with Ctrl + z, run commands, then type fg to return.
Common Mistakes When Exiting Vim
Beginners make these errors. Avoid them to exit smoothly.
- Typing
qwithout the colon – This enters ex mode, not command mode - Forgetting to press Enter after the command
- Being in insert mode and typing colon – The colon appears as text
- Using
:Qinstead of:q– Vim is case-sensitive - Typing
:wwhen you want to quit – This saves but does not exit
If you make a mistake, press Esc and start over.
Exiting Vim On Different Operating Systems
Vim works the same on Linux, macOS, and Windows. The commands are identical.
On macOS, the Esc key works the same. Some Mac users remap Caps Lock to Esc for easier access.
On Windows, vim runs in the command prompt or PowerShell. Use the same colon commands.
If you use gvim on any system, you can use the mouse to click the close button.
Exiting Vim With A Specific Error Code
Scripts check vim’s exit code. A zero means success, non-zero means error.
Use :cq to quit with an error code of 1. This tells the calling script that something went wrong.
You can specify a custom code: :cquit 5 exits with code 5. This is useful for automated workflows.
Exiting Vim When Editing System Files
System files like /etc/passwd require root permissions. If you opened them without sudo, vim shows a warning.
You can still view the file. To save changes, you need to use :w !sudo tee %. This writes with sudo.
To exit without saving, use :q!. Vim will not save changes to protected files.
Exiting Vim After A Crash
If vim crashes, it leaves a swap file. When you reopen the file, vim asks what to do.
You can recover changes with :recover. Or delete the swap file and quit.
To exit the recovery prompt, use :q. Then manually remove the .swp file with rm .filename.swp.
Exiting Vim With Multiple Undo Levels
Vim remembers undo history. If you undo changes and then quit, the undo history is lost.
To keep undo history between sessions, enable persistent undo. Add set undofile to your .vimrc.
Now when you exit with :wq, the undo history is saved. Next time you open the file, you can undo previous edits.
Exiting Vim And Keeping Terminal Clean
After quitting vim, the terminal shows the file contents. Some users prefer a clean screen.
Use :q normally. To clear the screen after exit, add alias vim='vim -c "set t_ti= t_te="' to your shell config.
Or use clear after vim exits. You can also run vim filename.txt && clear.
Exiting Vim With A Plugin Manager
Plugin managers like vim-plug or Vundle can affect exit behavior. Some plugins add custom commands.
If a plugin prevents normal exit, check its documentation. You may need to use :qa! to force quit.
Common plugins like NERDTree or CtrlP do not interfere with exit commands.
Exiting Vim In A Docker Container
When using vim inside a Docker container, the same commands work. But you may have network issues.
If vim freezes in Docker, use Ctrl + z to suspend, then exit the container.
You can also kill the container from the host with docker kill container_name.
Exiting Vim In A Tmux Session
Tmux users often run vim in a pane. Exit vim normally with :q.
If vim hangs, you can kill the tmux pane with Ctrl + b then x. Confirm with y.
This kills the pane and any running process, including vim.
Exiting Vim With A Single Keystroke
For maximum efficiency, map a single key to exit. Add this to your .vimrc:
:nnoremap <Space>q :wq<CR>
Now pressing Space then q saves and quits. Choose a key that does not conflict with other mappings.
Some users map Q to quit. Add :nnoremap Q :q!<CR> to force quit with Shift+q.
Exiting Vim And Learning More
Once you master exit commands, learn more vim features. Use :help for documentation.
The built-in tutorial is :help tutor. Run vimtutor