I use Vim mainly for quick edits rather than long work sessions. In that sense, I find the keyboard sequence for quitting especially laborious: Esc, Shift + ;, w, q, Enter.

How to quit Vim (possibly saving the document) with the least keystrokes? Especially from Insert mode.

  • 45
    The power button is just one click away, you know. – Renan Oct 1 '13 at 16:35
  • 2
    @jasonwryan is right - ZZ is pretty quick if you are in command mode, but the comparison isn't quite right. Your key sequence assumes that you are in insert mod. To get out of insert mode Ctrl-[ is quicker than Esc. Shift-: is one keystroke - you can't type : any quicker on its own. So you save some, but not much. – Dominic Cronin Oct 1 '13 at 22:48
  • 2
    You should not be using Esc to exit insert mode, as the Esc key is far from the others on modern keyboards. Use Ctrl + ] or create your own mapping. – void-pointer Oct 2 '13 at 2:01
  • 3
    Get a French keyboard - you can eliminate the shift. – Rich Oct 2 '13 at 11:16
  • 1
    void-pointer, doesn't Ctrl+C work instead of Ctrl+[ as a substitute for Esc? – zumalifeguard Mar 6 '15 at 22:55

Shiftzz in command mode saves the file and exits.

  • 4
    Does this absolutely need Shift or is it simply Z Z (upper case)? Also, don't you need to be in command mode for it to work, which would make it Esc Z Z? – a CVn Oct 1 '13 at 11:26
  • 28
    Also, ESC Z Q to exit without saving – Carlos Campderrós Oct 1 '13 at 12:31
  • Strg +z z is just wrong. It will send the vim process to background – hek2mgl Oct 1 '13 at 15:37
  • 2
    @MichaelKjörling: The Z Z just needs to be uppercase. – LarsH Oct 1 '13 at 16:40
  • 2
    This only closes one file though. I have lots of tabs open so it would take tons of Zs. – Philip Oct 2 '13 at 16:26

ZZ in normal mode saves the current file if modified and exits or closes the current window/tab (same as :x but not :wq which writes the file even if it hasn't been modified).

To exit unconditionally after having written all the modified files in all windows, tabs and hidden buffers, you need :xa (it still won't exit if some files can't be written for a reason or another)

To exit unconditionally without changing anything: ZQ (same as :q!).

  • 1
    I think you mean normal mode. "command mode" could easily be confused with command-line mode – David Brown Oct 1 '13 at 20:14
  • @DavidBrown, command mode is the traditional name, but I agree that normal mode is less ambiguous. I've amended the answer. – Stéphane Chazelas Oct 1 '13 at 20:35

:x is one key less than :wq

  • 10
    Not as good as Shift + Z Z though! – CodeMedic Oct 1 '13 at 17:04
  • @CodeMedic It's far and away the fastest if you've got nno : ; and nno ; :, though. (plus vno the same). I was dubious about the swap, but you'll never go back. :nno ,: :silent! unmap :<CR>:silent! unmap ;<CR> and :nno ,; :nno ; :<CR>:nno : ;<CR>:vno ; :<CR>:vno : ;<CR> support the odd yap@" and such. – jthill Oct 10 '15 at 19:47

Create a custom mapping for frequenly used tasks. If you quit vim often, create a mapping with few key strokes, e.g.

nnoremap <leader><leader> :xa<cr>

If the <leader> is set to comma using let mapleader = "," hitting comma twice is a quick way of quitting vim and saving your changes. If you want to save one more key stroke when you are in insert mode, also create a corresponding insert mode mapping:

inoremap <leader><leader> <esc>:xa<cr>

But beware, this might accidentally quite vim when you hit the <leader twice.

  • 1
    Thanks for a thorough answer, it encourages me to learn more about vim (which I tend to avoid)! – vemv Oct 1 '13 at 9:11
  • 4
    @vemv, you should work the other way round - any time you have to do the same thing more than once, see if there's a way to stop that! – Wayne Werner Oct 1 '13 at 18:49

How about just Q for 'Quit'?

That'll only require one shifted keystroke (i.e. Shift and Q). It's also a key combination that I rarely hit by accident.

By default, vim maps Q to switch to "Ex" mode. If you find "Ex" mode as useless (or annoying) as I do, then you won't miss its default function at all.

Here is what I have in my .vimrc file:

" allow quit via single keypress (Q)
map Q :qa<CR>

If you have unsaved buffers, it'll prompt you before exiting.

  • somehow in my VIM Q was already mapped to :q by default. Thanks for the tip. – user13107 Jul 26 '14 at 13:03

I use this three combination as my magic shortcuts.

Ctrl + s to save,

ctrl + d to save and exit,

ctrl + q to exit discarding changes.

1st. Put this to your ~/.bashrc

bind -r '\C-s'
stty -ixon

2nd. Place this to your ~/.vimrc

inoremap <C-s> <esc>:w<cr>                 " save files
nnoremap <C-s> :w<cr>
inoremap <C-d> <esc>:wq!<cr>               " save and exit
nnoremap <C-d> :wq!<cr>
inoremap <C-q> <esc>:qa!<cr>               " quit discarding changes
nnoremap <C-q> :qa!<cr>

3rd. Restart vim and get more productive days !

  • Why use CTRL+D/CTRL+Q to save+exit, and not just ZZ, and ZQ? – Evan Carroll Feb 29 '16 at 20:39
  • Hi Evan, ctrl + d / ctrl + q just need two keystroke, while ZZ and ZQ need three keystroke. shift + z + z / shift + q + q. One saving keys are significant if we do this frequently. Moreover we raised with ctrl + s as saving shortcut, i think it more intuitive rather than ZZ or ZQ. – Brain90 Mar 2 '16 at 3:09
  • I've always considered ZQ to be dangerous. You can intend to hit ZQ to not save, but if you accidentally hit Z twice you've gone and saved your file without meaning to. – Keith Devens May 29 at 21:56
  • if you're on a mac then use ~/.bash_profile. And remember to source it. – Snowcrash Jul 16 at 11:24

Here is a cheat sheet for VIM

Cheat-sheet-for-VIM

To quit the vi editor without saving any changes you've made:

If you are currently in insert or append mode, press Esc. 

Press  :  (colon). The cursor should reappear at the lower left corner of the screen beside a colon prompt. 

Enter the following:
  q!
This will quit the editor, and all changes you have made to the document will be lost.

Some more ::

Closing and Saving Files

When you edit a file in vi, you are actually editing a copy of the file rather than the original. The following sections describe methods you might use when closing a file, quitting vi, or both.
Quitting and Saving a File

The command ZZ (notice that it is in uppercase) will allow you to quit vi and save the edits made to a file. You will then return to a Unix prompt. Note that you can also use the following commands:

:w  to save your file but not quit vi (this is good to do periodically in
    case of machine crash!).
:q  to quit if you haven't made any edits.
:wq to quit and save edits (basically the same as ZZ).
Quitting without Saving Edits

Sometimes, when you create a mess (when you first start using vi this is easy to do!) you may wish to erase all edits made to the file and either start over or quit. To do this, you can choose from the following two commands:

:e! reads the original file back in so that you can start over.
:q! wipes out all edits and allows you to exit from vi.
  • The last 2 should add " since last time the file was saved" ... it's "obvious" but a beginner could understand "great, it's since I opened vi", and discover that no, if they saved during the session, ":q!" won't recover the original file at all, and neither will ":e!". It will just discard modifications that occured since the last time the file was opened or saved (whichever is the latest). – Olivier Dulac Oct 2 '13 at 15:03

My simple and convenient solution for quit and save from vim - is:

Ctrl + X shortcut.

Jut add this code to .vimrc file:

"Fast quit and save from normal and insert mode
:map <C-X> <ESC>:x<CR>
:imap <C-X> <ESC>:x<CR>
  • Nice, but why is <C-Q> not mappable? – friederbluemle May 1 '17 at 18:18

Try

nno : ;
nno ; :
vno : ;
vno ; :

If you use fFtT this will take some getting used to, but now quitting's just ;x to save if needed, ;q to not save. Plus, everything else gets quicker too.

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.