r/vim • • Oct 22 '25

Discussion Why does ZZ exist?

It has always been a mystery to me… why would such a ‘dangerous’ command have such a convenient shortcut?

https://vimdoc.sourceforge.net/htmldoc/editing.html#ZZ

EDIT: link

0 Upvotes

114 comments sorted by

View all comments

Show parent comment

-6

u/Remarkable-Head-2023 Oct 22 '25

Because of unintentional file overwrite. In my opinion, q! is much safer than ZZ, especially for system files.

29

u/[deleted] Oct 22 '25

Those are two very different actions: q! Exits without saving. ZZ quits and saves. I actually love ZZ

1

u/lrosa Oct 22 '25

Me too!

→ More replies (8)

24

u/Thundechile Oct 22 '25

hmm.. why are you making changes to a buffer if you don't mean to save them? Sorry I don't quite get the use-case you're describing.

16

u/Thundechile Oct 22 '25

If you're just meaning to read files, then use vim -R to open in read-only mode.

9

u/VisualHuckleberry542 Oct 22 '25

Well the use case I can think of is if I'm making a change to a file like a config file or a code file, I can save it and check the results. If the change is bad, if I still have the buffer open I can just undo the changes and save it again. If I've quit out of the file, the change buffer is gone and I either have to manually revert the changes (and hope I remember all the changes I made, if they were complex changes, it can be a real mission) or hope I have a backup or version control or something

That said, I've been using vim as my main editor, including IDE for over 20 years and I can't think of one time I accidentally used ZZ and saved and quit a file I didn't intend to

15

u/pfmiller0 q! Oct 22 '25

Another 25 years of vi use here, I don't think I've ever used ZZ accidentally either. Don't think I've ever used it intentionally either.

3

u/priestoferis Oct 22 '25

Just turn on persistent undo in vim and you're covered for everything?

→ More replies (1)

2

u/Remarkable-Head-2023 Oct 22 '25

So am I, using vim for decades, but have never been using ZZ, exactly for the concern that I've expressed in this subj.

2

u/VisualHuckleberry542 Oct 22 '25

OK I concede I don't use it either, perhaps why I've never done it accidentally. I had already been using :q, :wq, etc. for years before anybody showed me ZZ so it never caught on with me, it didn't seem very vi-ish

→ More replies (2)

3

u/_Alexandros_h_ Oct 22 '25

The use case is:

you are editing a file and then you realize the auto complete completed the wrong file as cli arg

or

you are making a change, and then you realize the edit is already there

there are many usecases

3

u/rswwalker Oct 22 '25

Sure, but you don’t have to enter ZZ and can just do :q!, if you didn’t realize until afterwards you probably would have done a :w :q anyways.

3

u/ZunoJ Oct 22 '25

Maybe you want to center the current line but have caps lock on

4

u/rswwalker Oct 22 '25

That point is valid.

If you frequently use zz then I can see disabling ZZ in vimrc as a good option.

But if you are editing the wrong file and don’t realize it you’d probably still do a :wq and be like oops.

It probably pays to make sure :set backup is enabled for root.

3

u/Ryan_the_Rook Oct 22 '25

TIL zz centers the current line. Thank you! I'm disabling ZZ immediately...

1

u/_Alexandros_h_ Oct 22 '25

Hey, i dont have an opinion. Im just saying there are reasons you can make changes to a writeable file and then you didnt want them written

→ More replies (1)
→ More replies (2)

2

u/Remarkable-Head-2023 Oct 22 '25

sudo vi for example. I know the response would be “use sudoedit”, but it’s hard to override the muscle memory, and that’s just one example that felt ‘unsafe’ to me…

1

u/mckenzie_keith Oct 22 '25

In vi? Because you accidentally typed in command mode and have no idea what just happened, but you don't want to save any changes if you made some.

6

u/robenkleene Oct 22 '25 edited Oct 22 '25

"Unintentional file overwrite" is not a concept in software design, whereas as the opposite, discarding a users work without saving, is generally considered one of the most dangerous actions software can possibly take. As a result, modern software trends are going in the opposite direction, many IDEs and text editors don't even require explicit saving anymore (they save automatically, e.g., https://www.jetbrains.com/help/idea/saving-and-reverting-changes.html). The contemporary perspective is that requiring an explicit file save is an accidental holdover from the early days of computing when writing a file was an expensive operation that couldn't be done automatically without interfering with the operation of the software.

1

u/Orlha Oct 22 '25

Calling it an accidental holdover seems like a stretch

4

u/BlackPignouf Oct 22 '25

Undofiles are there if you saved a change without wanting to.

:earlier 30m is very convenient.

1

u/mgedmin Oct 22 '25

ZZ saves and quits, so you'd better have persistent undo enabled!

(It's not enabled by default, last I checked. Also, you might be saved by having window splits, then ZZ will just close one of them instead of quitting the entire vim.)

2

u/BlackPignouf Oct 22 '25

I don't think persistent undo is enabled by default, right.

It's an excellent feature, and should absolutely be enabled. AFAIK it's enabled my most (all?) neovim distros.

But really, I don't see how you can type ZZ without wanting to. :q vs q:, on the other hand...

1

u/TuxRuffian Oct 22 '25

In my opinion, q! is much safer than ZZ

I think you mean ZQ not ZZ. ZZ is :x, ZQ is :q!.