Vim: `:w` doesn't save unchanged file

Created on 8 Feb 2017  路  15Comments  路  Source: VSCodeVim/Vim

Please thumbs-up 馃憤 this issue if it personally affects you! You can do this by clicking on the emoji-face on the top right of this post. Issues with more thumbs-up will be prioritized.


So sometimes I trigger my tools to run by resaving files without having changed them (typically to run tests in guard). In Vim :w will cause this to happen, in VsCodeVim it presumably checks the file's dirty state before saving. Sounds logical on paper, but is a bit inconvenient for me in practise.

What did you do?

:w on unchanged file.

What did you expect to happen?

My file watcher tools to run.

What happened instead?

Nothing. (File was not updated)

Technical details:

  • VSCode Version: 0.9.0
  • VsCodeVim Version: 0.5.2
  • OS: OSX Sierra
kinenhancement upstreavscode

Most helpful comment

"vim.normalModeKeyBindingsNonRecursive": [
        {
            "before": [":", "w"],
            "commands": [
                "workbench.action.files.save",
            ]
        }
    ]

All 15 comments

Just tried this on vanilla vim and it saves the file regardless of it's 'dirtiness'. I recall there was a reason why we checked the dirty flag though...

In any case, PRs are welcome.

PRs are welcome.

Cool. I'm happy to submit a PR. I imagine it will be fairly trivial.

I recall there was a reason why we checked the dirty flag though...

I'll look at the blame first to see if there's any gotchas I should be aware of.

https://code.visualstudio.com/updates/v1_10#_manually-trigger-save-actions

Does this work for you now by any chance?

Manually trigger save actions

You can now save an editor via 鈱楽 even if the file is not dirty and extensions which perform actions on save will be triggered. This allows you to trigger Format on Save even if the file is not dirty.

Hm. Doesn't seem to affect :w. Previously I did not find 鈱楽 to be a problem, just dirty checking in :w.

ah ok yea I forgot we still had an isDirty check

hmm this looks to be on vscode's side, we call save, we don't have an isDirty check for write on our side...

/**
         * Save the underlying file.
         *
         * @return A promise that will resolve to true when the file
         * has been saved. If the file was not dirty or the save failed,
         * will return false.
         */
        save(): Thenable<boolean>;

What's the current status of it? TBH I cannot recall facing it before VS Code v1.21 update, but now :w definitely does not save unchanged file, but Ctrl+S does. Found this out while live-reloading a project via nodemon.

UPD: Sorry, didn't read carefully enough the upstream issue. It seems such API hasn't been implemented in VS Code yet.

Actually, it would be great temporary fix if we could use something like this in settings:

"vim.otherModesKeyBindingsNonRecursive": [
  {
    "before": [":", "w"],
    "after": [],
    "commands": [{
      "command": "workbench.action.files.save",
      "args": []
    }]
  }
]

@evenfrost Is there any way to do something like that, for example, to map :W to :w? It's not even funny how often I type :W when I mean to type :w and it slows down my workflow. In vanilla vim, I can simply remap the command, but I don't see how to map commands here. Normal mode and visual mode, yes, but :W or some such, no.

@jhwheeler I haven't tested it, but something like that could work:

  "vim.normalModeKeyBindingsNonRecursive": [
        {
            "before": [":", "W"],
            "commands": [
                ":w"
            ]
        }
    ]

@evenfrost Thank you. However, it doesn't work; I still get Not an editor command. W.

I'm wondering if :W qualifies as normal mode, since you are moving into command-line mode. I don't see in the README a separate setting for command-line mode keybindings; is there such a thing or should it work with normalModeKeyBindingsNonRecursive?

"vim.normalModeKeyBindingsNonRecursive": [
        {
            "before": [":", "w"],
            "commands": [
                "workbench.action.files.save",
            ]
        }
    ]

2019, same problem. I have Format On Save enabled, but doing :w does not format it while Command+S does

Still the same issue in 2020! I want to use :w to quickly format my files with Black. The workaround I'm using is inserting an empty line, and then saving. Which calls the formatOnSave.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

typeoneerror picture typeoneerror  路  3Comments

Jimmy-Z picture Jimmy-Z  路  3Comments

cckowin picture cckowin  路  3Comments

rajinder-yadav picture rajinder-yadav  路  3Comments

st-schneider picture st-schneider  路  3Comments