5 ms·
I always feel that how to exit and quit vim should be at the very top of the first page. With a quick explanation for the various ways to do it. ZQ - in norm
by asciimov 5y ago
I always feel that how to exit and quit vim should be at the very top of the first page. With a quick explanation for the various ways to do it.
ZQ - in normal mode, quits (notice no colon and capital)
ZZ - in normal mode, saves only if file was modified and quits
:x - same as ZZ
:w - write the file
:wq - writes the file regardless of changes and quits
:q - quit, if no changes
:q! - quit, regardless of changes
:w !sudo tee % - For when you need to save a file you don't have permission to and you forgot to start vim with sudo.
Edit: Also note the difference between 'ZZ' and ':wq'. While 'ZZ' doesn't make any changes to the file system if the file hasn't been modified, ':wq' will at minimum change the date of last modification to the time that save was ran even if the file was not modified.
- JNRowe 5y agoMight also want to note :cq[uit] to quit with an error code. This can often be useful when you're using vim as your $EDITOR and wish to tell the calling process that you want to abort.
- asciimov 5y agoGood call, I think I've only used :cq once(?).
- JNRowe 5y agoOops, I probably could've offered examples. So, when writing a mail I can choose to simply give up instead of dropping me in to a dialog for "save draft/quit?", or to drop out of an edit session in `git add --interactive`, etc. The reason it can be useful in these specific instances is that you don't need to unwind your changes if you've run filters to modify the text for example.
- DerSaidin 5y ago:cq is very handy for aborting merges
- sundarurfriend 5y ago> :w - write the file > :wq - writes the file regardless of changes and quits The first one is also "write the file regardless of changes", to be clear. The command version of "saves only if file was modified" is :up (for update). I have that mapped to a normal mode key and rarely ever use `:w`.
- asciimov 5y agoJust learned about update, after 18 years of vimming, thanks!
- dmurray 5y agoThere's also :qa for "quit all files" - I guess this is just another modifier, but I never use "a" in any other context.
- artificialLimbs 5y ago>> :w !sudo tee % A king is you. Thanks for this one... I can stop hating myself a little bit more now.
- datagram 5y agoGiven that Vim's biggest barrier to entry is its learning curve, I don't think starting out by teaching people 8 ways to do 2 things is the right approach. Agreed that exiting should be the first thing taught to new Vim users though.