Lurker News
`:x` leaves the modification time of files untouched if nothing was changed.

    :help :x
        Like ":wq", but write only when changes have been made.

Comments (270) 59 threads

waisbrot14 replies#
I learned to do `:wq` after I learned that `:X` encrypts your file. When typing without really paying attention to the screen, I've twice encrypted my file with password like `cd tmp`, then saved the config file, breaking my system.

After that, I switched to `:wq` (and sometimes `:w` `:q`) which is much safer against over-typing.

tomxor2 replies#
Thanks for this warning.

Even worse for me, because my laptop keyboard has some kind of horrible encoding causing latency on the shift key, so I'm constantly doing :Wq when I type fast enough... Yeah I had the suspicion it was human error - but I've tested using a single finger for shift and w, so it's definitely the keyboard.
[edit]
Looks like that feature is not necessarily built in by default though.

DwnVoteHoneyPot#
i did an alias mapping :Wq to :wq
dunham#
Yeah I do that a lot. And on macos, vi decides that :Wq is an error and returns a non-zero status code on my subsequent :wq, cancelling my git commit. So I get to type the commit message again.
jjallen1 reply#
Yeah, thanks for this. `:wq` is pretty fast to write. I think I will stick to it.
sodapopcan2 replies#
Or, as other have pointed out, just use `ZZ` which is even easier and equivilant to `:x`.
sshine3 replies#
:q, if you didn’t make a change, is even shorter.

Continue this thread — 3 replies

shp0ngle2 replies#
:wq just makes more sense to me, it’s “command: write, quit”

ZZ is just.. random?

Continue this thread — 2 replies

manaskarekar2 replies#
Thanks for bringing this up.

I'm toying with either disabling it,

    cmap X
or, mapping it down to a lower x.

    cmap X x
1. Does anyone see anything this could interfere with?

2. Does anyone know a better way to turn off the `:X` encryption option?
Sadly, having to remap definitely dulls the shine of `:x`.

sodapopcan1 reply#
Well, in fairness, `:X` brings up a prompt, so an accidental `:X` won't seamlessly masquerade itself as `:x`.

Otherwise, I feel a remap is just a valid option as any to disable it. I'm not sure there's a way (a quick skim of the docs made me think `set key=` would do it but that didn't work for me, or at least I didn't understand what it does) but either way you'd still have to add config.
But again, as I say to everyone I see using commands to quit and keeps getting brought up here: `ZZ` is the same thing.

manaskarekar5 replies#
Yeah, but it's possible to quickly type the next command (that you would after exiting vim) as the encryption key.

That said, given that neovim doesn't have this feature at all makes it less of an issue to me.

Continue this thread — 5 replies

queuebert1 reply#
Mapping to 'x' is dangerous since on a system where you don't have your vimrc you'll get the original behavior of 'X'. Ideally you'd map 'X' to deliver a small electric shock. ;-)
westurner#

  cmap X x
  :help X
> Mapping to 'x' is dangerous since on a system where you don't have your vimrc you'll get the original behavior of 'X'.

Which is still to prompt? A person could take their chances.
Besides, wouldn't that form of intentionally aversive conditioning actually boost the learning rate and create hyperassociations to the behavior your're attempting to stimulus extinct or just learn over?

ablob#
This command is no longer available in neovim, so it should be fine to use there.
wayne2 replies#
I have computer-literate parents and grew up in the PC era, so as a child I typed my elementary school assignments in vi. Accidentally using :X, encrypting one of my school papers, and having to completely rewrite it, was one of the slightly-traumatizing moments in my childhood.

FWIW, I switched to :wq for a while but I'm back to using :x instead of :wq. I'm just very careful now. :)

chasd001 reply#
I did this same thing on a large programming assignment in college. I’m 46 and can still remember the way the lab smelled and what the people sitting next to me were wearing the moment it happened.
lucb1e1 reply#
I get annoyed by ":W is not a valid command" errors and aliased it, and that's not even dangerous to get wrong, just have to retype ":w". Why be so careful instead of just aliasing it?

vimrc excerpt:

    command Wq wq
    command WQ wq
    command W w
    command Q q
    command Bd bd
    cnoreabbrev q1 q!
I hate having to get the shift key timing correct so much that I also aliased : to ; for commands, but that's more radical and gets me into trouble on remote systems that are not my own, not sure if I recommend it. I'd recommend vim to change the default for everyone, though (fat chance, I know).

Another annoying thing in vim is whatever the heck Q does. Try to type :q, fail, retry, and now suddenly you have an extra window to close or something. Solution: nnoremap Q

hcs#
I used to use the W alias whenever I noticed myself doing it on a new setup, but I haven't had to do it recently. I suspect my "slow on the shift" issue was due in part to the lousy keyboards I used to use.
freedomben1 reply#
Thanks great link. tldr:

> vim encryption uses old, obsolete algorithms that are poorly implemented. Since insecure cryptography is worse than no cryptgraphy, the community voted in favor of removing all crypto.

jpavel2#
This was in 2014. Since then, Vim has added non-obsolete encryption algorithms (some provided by libsodium), used by default.
raydiatian2 replies#
This is different from :x though
isbjorn16#
I think their point is sometimes their left pinky is a bit lazy and won't get off the fucking shift key
the_fury9 replies#
It is, but accidentally hitting capslock instead of shift when hitting the colon is extremely easy to do.

Continue this thread — 9 replies

Continue this thread — 8 more replies

sshine6 replies#
I always :q when I didn’t change anything.

If I changed something intentionally, :wq and :x are equivalent. If I changed something accidentally, :x won’t catch that, and :q complaining will require me to decide if :q! or :wq is correct.
So :x does not fit my workflow of avoiding accidental writes. Just like ZZ does not.

hhjj1 reply#
Can't you use :q and then :x instead of :q then :wq ?
sshine1 reply#
Yes, but if I :q and it complains that the file was modified, and I decide that the change is worthwhile, :wq and :x are equivalent, since :x is only different when the file isn’t modified.

You may argue that :x is shorter, but one thing is character count and another is muscle memory distance because of mnemonic similarity between :q and :wq.

caust1c1 reply#
Could alias x to wq? :-) That's what I'm doing after discovering this!

Continue this thread — 1 reply

pkulak1 reply#
I use :x cus it's one less keystroke. And yes, I still use :q to make sure I don't save in case I changed something by mistake.
JelteF1 reply#
ZZ is only 3 keystrokes as opposed to 4 for :x (shift + z + z = 3 and shift + ; + x + enter = 4). It also doesn't require timing the release of shift before the press of x.
pkulak2 replies#
Wow! Never knew that existed. If I can undo 20 years of muscle memory, I'll use it. :D

EDIT: Just tried it, and Z being right next to Shift is a deal breaker.

Continue this thread — 2 replies

renewiltord#
It's funny but I use q when I don't intend to change things and wq in whatever state I'm seeing them and x when I'm working on clean git state.

Honestly it sounds complex, but I don't even think about it and it only struck me reading your comment.
The warning note on q is desirable.

julesnp#
I use the 'confirm' option in my vim config, that way I can always use :q and then get a prompt on what to do if a file's been changed.

I also map to :q to speed things up a bit.

silisili#
This is precisely why I use w and q instead of x. Not knowing if it wrote is definitely not a feature I'd want.
bigmadwolf#
ZQ will quit without writing anything.
joeatwork3 replies#
This is the first time my life has changed from just reading a Hacker News headline.
cassepipe4 replies#
I can't help but see that argument as post rationalization from people who have bee using :wq their whole life

\s
Been using :x from the start about 4 years ago and I never accidentally encrypted a file.

Continue this thread — 4 replies

Continue this thread — 2 more replies

j1elo20 replies#
See, that's why even though I'm a terminal user first and foremost, I really like the discoverability of GUIs, and that's where a good GUI is unbeatable by CLI.

This would have just been an entry "Save if needed" in the File menu, right below "Save", and users would probably find it by accident while looking at the menu, even while not actively looking for new ways to save a file.
(Not getting into the fact that a well programmed Save function would, IMO, not do anything if there are no changes, i.e. if I was the one writing the code, the Save button would be greyed out, or in the case of vim, :wq would not write anything if no changes had been made... but that's a different discussion)

Continue this thread — 20 replies

Load more comments