0

I have this function which sources my .zshrc only if it has been modified in vim but I recently reinstalled Windows and WSL and then used my old .zshrc and .vimrc to get my setup back but this specific function doesn't work like it did before:

function zshrc () {
    local original_ctime="$(zstat +ctime -- ~/.zshrc)" 2>/dev/null
    vim ~/.zshrc || return
    [[ -n $original_ctime ]] || return
    if [[ $original_ctime != "$(zstat +ctime -- ~/.zshrc 2>/dev/null)" ]]
    then
        source ~/.zshrc
    fi
}

This function is supposed to check if the file creation time has changed. See here.

Before I reinstalled WSL I could run zshrc and save the vim buffer using :wq (without making any changes) and then vim would not overwrite the original file so it WON'T source ~/.zshrc and if I DO make changes then only vim will overwrite the original file, so the function WILL source ~/.zshrc. I'm assuming some defaults were maybe changed in vim so how can I get that specific behaviour back?

I understand that I can just exit vim with :q! to get this but when I'm editing different files quickly then I'll use :wq without thinking about it and then it sources .zshrc again without any changes.

4
  • 2
    That's strange: I'd expect :wq to always save the file. If it didn't, I'd consider it a bug. Are you sure this wasn't a bug in WSL? Aug 18 '20 at 9:18
  • 1
    fyi, ctime is not creation time, it's inode change status time. It's probably mtime you want here (for the age of the "contents" of the file). See also the -H/-A options of stat to avoid the fork. Aug 18 '20 at 13:42
  • 1
    Another option is to compare a checksum before and after. Aug 18 '20 at 13:43
  • @Gilles'SO-stopbeingevil' At least that's what I thought was happening 'cause I always exit with :wq and if I didn't modify the buffer then zstat +ctime would return the same time as before so I assumed vim didn't write the buffer to the file.
    – ntruter42
    Aug 18 '20 at 14:28
3

Get in the habit of doing ZZ (i.e., Shift+Z, Shift+Z).  If the buffer has been modified, this will do :wq.  If the buffer hasn't been modified, it will simply exit without saving.

1
  • 3
    Also :x instead of :wq.
    – Quasímodo
    Aug 18 '20 at 11:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.