Exit vim more quickly
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.
@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.
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.
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.
If I wanted such a solution I'd use `xmodmap` instead of getting French characters I won't use at all.
Shiftzz in command mode saves the file and exits.
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`?
ZZin normal mode saves the current file if modified and exits or closes the current window/tab (same as
:wqwhich 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:
I think you mean normal mode. "command mode" could easily be confused with command-line mode
:x is one key less than :wq
@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 ::silent! unmap ;` and `:nno ,; :nno ; ::nno : ;:vno ; ::vno : ;` support the odd `[email protected]"` and such.
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>
<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
Thanks for a thorough answer, it encourages me to learn more about vim (which I tend to avoid)!
Use this to do the magic. 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 and get more productive days !
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.
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.
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
" allow quit via single keypress (Q) map Q :qa<CR>
If you have unsaved buffers, it'll prompt you before exiting.
Here is a 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).
My simple and convenient solution for quit and save from vim - is:
Ctrl + X shortcut.
Jut add this code to
"Fast quit and save from normal and insert mode :map <C-X> <ESC>:x<CR> :imap <C-X> <ESC>:x<CR>
@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 ``, then ``. If `^Q` is printed, `vim` sees the ``, else the terminal probably swallowed it.