149

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.

CC BY-SA 3.0
7

9 Answers 9

Reset to default
191

Shiftzz in command mode saves the file and exits.

CC BY-SA 3.0
5
58

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

CC BY-SA 3.0
2
36

:x is one key less than :wq

CC BY-SA 3.0
3
  • 20
    Not as good as Shift + Z Z though!Commented 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
    Commented Oct 10, 2015 at 19:47
  • 2
    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.Commented Oct 24, 2020 at 18:43
25

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 !

CC BY-SA 4.0
5
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.

CC BY-SA 3.0
2
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.

CC BY-SA 3.0
1
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.
CC BY-SA 3.0
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).Commented 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>
CC BY-SA 3.0
2
  • 1
    Nice, but why is <C-Q> not mappable?Commented 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
    Commented 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.

CC BY-SA 3.0

    You must log in to answer this question.

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