148

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.

7
  • 3
    @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. Oct 1, 2013 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. Oct 2, 2013 at 2:01
  • Proper typing involves moving the hands/arms, not stretching the fingers. Esc and left Control are perfectly fine - have never developed Emacs pinky after all the years.
    – deprecated
    Oct 2, 2013 at 6:27
  • 3
    Get a French keyboard - you can eliminate the shift.
    – Rich
    Oct 2, 2013 at 11:16
  • 1
    void-pointer, doesn't Ctrl+C work instead of Ctrl+[ as a substitute for Esc? Mar 6, 2015 at 22:55

9 Answers 9

192

Shiftzz in command mode saves the file and exits.

5
  • 5
    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?
    – user
    Oct 1, 2013 at 11:26
  • 58
    Also, ESC Z Q to exit without saving Oct 1, 2013 at 12:31
  • Strg +z z is just wrong. It will send the vim process to background
    – hek2mgl
    Oct 1, 2013 at 15:37
  • 3
    @MichaelKjörling: The Z Z just needs to be uppercase.
    – LarsH
    Oct 1, 2013 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, 2013 at 16:26
57

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!).

2
  • 2
    I think you mean normal mode. "command mode" could easily be confused with command-line mode Oct 1, 2013 at 20:14
  • 1
    @DavidBrown, command mode is the traditional name, but I agree that normal mode is less ambiguous. I've amended the answer. Oct 1, 2013 at 20:35
36

:x is one key less than :wq

3
  • 20
    Not as good as Shift + Z Z though!
    – CodeMedic
    Oct 1, 2013 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, 2015 at 19:47
  • 1
    Just want to comment that :x does not the same as :wq. :x saves only if there is change and quits. :wq saves always (even if there is no change) and quits. Oct 24, 2020 at 18:43
26

Two keystrokes only!

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

Get more productive day !

5
  • 3
    Why use CTRL+D/CTRL+Q to save+exit, and not just ZZ, and ZQ? Feb 29, 2016 at 20:39
  • 1
    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, 2016 at 3:09
  • 5
    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. May 29, 2018 at 21:56
  • 2
    if you're on a mac then use ~/.bash_profile. And remember to source it.
    – Snowcrash
    Jul 16, 2018 at 11:24
  • 1
    The Ctrl buttons did not work for me, but I voting it up for the 2nd place, you nicely and briefly listed the one of the most required commands for this venture. Thank you.
    – Mugé
    Apr 19, 2020 at 14:01
21

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.

2
  • 1
    Thanks for a thorough answer, it encourages me to learn more about vim (which I tend to avoid)!
    – deprecated
    Oct 1, 2013 at 9:11
  • 5
    @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! Oct 1, 2013 at 18:49
18

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.

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

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.
1
  • 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). Oct 2, 2013 at 15:03
3

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>
2
  • 1
    Nice, but why is <C-Q> not mappable? May 1, 2017 at 18:18
  • @friederbluemle At my side Ctrl+S and Ctrl+Q (stop/continue output) are mapped to the terminal directly, so vim does not get those keystrokes. Find out yourself: Enter edit mode (i), then press <C-V>, then <C-Q>. If ^Q is printed, vim sees the <C-Q>, else the terminal probably swallowed it.
    – Tino
    Apr 27, 2020 at 20:19
2

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.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .