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

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

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

  • 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.

  • 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.

  • This is the first time my life has changed from just reading a Hacker News headline.

  • 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)

  • Keybinds (in normal mode):

    Shift + ZZ -> :x (save only if needed and quit)

    Shift + ZQ -> :q! (quit without saving)

  • 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.)

  • `:cq` exits vim with exit code 1.

    This is useful if you're in vim to write a git commit message and you realize don't actually want to commit. Or you're in vim from `fc` to edit a shell command and you realise you don't actually want to run it.

  • Front page and more than a hundred comments for a basic Vim command. Reddit is leaking.

  • :update is like :w but only writes when changes are made.

    While we're at it :earlier and :later are undo/redo but instead of using an undo stack they use change points in time, so your undo history is never overwritten :)

  • You can also type ZZ (no colon) while in normal mode to exit and save.

    This also appears to only write when changes have been made (according to some testing in neovim on my local machine).

  • I became quite an advanced vim user in 1999, during my military service in Italy (I was stationed at the Ministry of Aerial Defense, and working on IT stuff).

    It's quite incredible and a bit weird that today, in 2023, I can say "Yes, I knew this", despite non having used vim much since ~2012.

    I would have never guessed I'd be able to recall things like this.

  • Never liked ":wq" construct since I started to use vi, almost three decades ago. Always used ":x". Now realizing, it had a hidden benefit. Good for me. <pats himself on the back>

  • Maybe it's just me, but I find :wq a lot easier to type... For :x (or worse, ZZ), I either have to move my hand or bend my ring finger/pinky to reach the keys. On the other hand, my fingers reach both w and q at the same time with very little effort.

  • I've been using `:x` successfully for years after finding it. I had no idea that `:X` actually encrypts the file and can cause havoc lol.

  • ... and you can append `a` (`:xa`) to write out all modified buffers and quit.

  • I had a CS prof years ago tell us all the only thing we needed to know about vi(m) was the dammit key

    As in "escape-q-dammit"

  • Another pro tip. You can escape insert mode with alt https://news.ycombinator.com/item?id=34168031

  • I know maybe 4 Vim commands I use to write git commit messages as I was too lazy to change the default editor. :x is one of them. It's funny how different people learn tools differently. I've learnt by googling "Vim basic commands for git commit messages" and never spent any more time on it but apparently this is a revelation for many long time Vim users here.

  • ZZ for me as well and I mapped :WQ to :w !sudo tee % >/dev/null

  • Hold shift and press Z twice will also close and save. It's only three key presses instead of 4 for :x (shift + ; + x + enter)

  • TIL that :wq is like :x

    I only knew about :x. So thus far I’ve only edited with :x, and then used :q! to quit without writing the changes.

  • I have a question about this. If I type ":w" and then immediately do Ctrl+Z to suspend the session, it seems that Vim is always able to save the file before the editor suspends. Is this by design? Or was I just lucky so far?

    Anyway, it would be great to have a default shortcut for this combination of keys too.

  • TIL that VIM has built-in encryption.

  • Why is it useful? I would do ZZ to quit with saving, or :q! to quit without saving.

    Incidentally, I find :w to be very inefficient to type (any : command really, but there are not normally the most common operations), for such a common operation. I wonder if people remap it.

  • I also have a funny related thing to Tell HN:

    `git --amend` -> `:wq` is shorter and faster to type than

    `git --amend --no-edit`

  • Is there a good way to manage saved-but-not-exited vim windows so I don't open the same file twice? I keep running into a problem where I'm editing a dozen files, and then I try to re-open a file I already have opened. Vim gives me a warning about this but my choices are a) tab through all my open windows until I find the right one, or b) open a new editing window and hope that I didn't have unsaved changes in the first one.

    Is there a way to have vim just go to the open window? Or is there a better way to manage my windows?

    (I'm using MacVim if it makes a difference.)

  • WHAT

    My life just began again. Thank you

  • I almost exclusively exit vi using CTRL_W-CTRL-Q.

    It's a 3 finger chord that I use all the time to close vi windows. Makes sense that I'd use it to close the last one too.

  • See also

      ZZ
      :silent! wa

  • I am surprised it is not already mentioned in the comments, but I personally cannot use an editor that doesn't automatically write changes when I am done typing. I just forget to save and wonder why the app I am working on did not change. So my (neo)vim setup uses a plugin like `Pocco81/auto-save.nvim`.

    This solves the problem mentioned by OP because I can just `:q` anytime.

  • I map 'S' to this in my .vimrc:

        nmap S :wall<cr>
    
    which saves all changed buffers into their respective files.

    It is probably the single most productive customization in my .vimrc file. I save my files very often, and I usually have multiple files and buffers open. 'cc' is an alias for the old 'S', so I don't lose much.

  • You can also use ZZ. It is equivalent to :x

  • 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?

  • Seriously. Just type Shift-double-z in normal mode. No colon, no fuss, no mistakes, no confusion with anything else. No need to remap in case you press shift, because you press shift on purpose. Works everywhere vim is installed, even if your dotfiles aren’t.

  • " https://stackoverflow.com/questions/7883803

    silent !stty -ixon > /dev/null 2>/dev/null

    nnoremap <C-q> :x<CR>

    inoremap <C-q> <ESC>:x<CR>

  • I learned early on that the easiest way to quit Vim without saving was ctrl-Z and then kill -9 %1

  • Maybe well-known, but: Ctrl-C exits interactive mode and goes back to normal mode. Easier to reach than Esc and I usually find rebinding/aliasing keys to be too much of a hassle

  • Cumulatively, you've just saved quite a lot of my time - thanks!

  • A lot of the pain could've been avoided if they just wrote somewhere that you need to type ':' or something else to access help/commands when inside of vi/vim

  • You could also just use a modern text editor with autosave

  • for me, I don't want it to write only when there are changes. I want it to error out if I try to quit without saving changes so I go and figure out if I changed something by accident before I save it and I want it to write specifically when I tell it to write. This cuts down on so many problems, at least in my use cases.

  • You can also just press ZZ in normal mode.

  • I'm rarely in a situation where I'd want to quit vim. One off config files on a remote machine perhaps?

  • I knew about `:x`, but I had no idea there was an actual distinction beyond just being a shortcut for `:wq`.

  • Also, `ZZ` is the same as `:x`.

  • Which is the same as ZZ in normal mode. I don’t think I’ve hit “:wq” in years.

  • Try "ZZ" instead. No ":" required.

  • :x gang checking in

  • Shift Z Z will instantly save and exit, same as wq!

  • undefined

  • By the way, why does visudo sometimes give me another editor (like nano) on a new system. Isn't "vi" supposed to mean "vi"?

  • I've been using ZZ all these years

  • i have to admit, i heavily prefer

    exit & save

    ZZ

    exit without save

    ZQ

    these are vi(m) commands, the :x & :wq crap is there for ed-compatibility

    but: whatever floats your boat ;))

  • I just use it because it’s shorter.

  • How is it that this is news?

  • Wait, people exit vim?! ;)

  • ZZ gang

  • Why would it write if no changes have been made?

  • undefined