Live data from Hacker News

Tell HN: Vim users, `:x` is like `:wq` but writes only when changes are made

news.ycombinator.com

11–20 of 279 posts

Re: Tell HN: Vim users, `:x` is like `:wq` but writes only when changes are made

#12
The really fun part:

> This command is not supported in Vim9 script, because it is too easily confused with a variable name.

So you can’t just write “x”, but must instead go for something bland like “exit”, or something exciting like “exe'x'”. (I’m presuming “execute 'x'” will work, but my mental model of how you’d unsupport the command in Vim9 script could be wrong.)

Re: Tell HN: Vim users, `:x` is like `:wq` but writes only when changes are made

#14
I barely ever use vi(m), but occasionally use it when I have to or it's convenient for me to edit something at a CLI only, which in my work practice is only an occasional brief thing.

`:x` is one of only like 8 commands I know!

I'm surprised that it's apparently not one of the basic commands everyone knows?

Re: Tell HN: Vim users, `:x` is like `:wq` but writes only when changes are made

#16

Why would it write if no changes have been made?

I can imagine any number of situations where it might be wanted. Keep the file open in vim, changes are made from somewhere else, but you'd like to keep the original version, so you :w. It makes sense to me the :write actually writes regardless of context (assuming sufficient permissions) and with no hidden conditions like "something must have changed". That kind of thing leads to weird user behavior like editing whitespace just to be able to re-save a file.

Re: Tell HN: Vim users, `:x` is like `:wq` but writes only when changes are made

#17

Keybinds (in normal mode): Shift + ZZ -> :x (save only if needed and quit) Shift + ZQ -> :q! (quit without saving)

I always preferred these over the :wq! And :q!, but for some reason seem way less popular. Most tutorials don’t even mention ZZ and ZQ.

Re: Tell HN: Vim users, `:x` is like `:wq` but writes only when changes are made

#20

Why would it write if no changes have been made?

There are strange cases which may make this a logical choice. Since vi doesn't traditionally lock the file it's editing, another process could change the file on-disk while you have it in the editing buffer. I think you'll get a message in these situations, because it's usually not what you want. Then you'd have the choice of writing out the buffer, whether it's changed or not, to restore the contents of the file from when you began your vi session.
Post reply on HN