Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

:w doesn't save unchanged file #1282

Open
rhys-vdw opened this issue Feb 7, 2017 · 21 comments
Open

:w doesn't save unchanged file #1282

rhys-vdw opened this issue Feb 7, 2017 · 21 comments

Comments

@rhys-vdw
Copy link
Contributor

rhys-vdw commented Feb 7, 2017

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
@jpoon
Copy link
Member

jpoon commented Feb 8, 2017

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.

@rhys-vdw
Copy link
Contributor Author

rhys-vdw commented Feb 8, 2017

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.

@xconverge
Copy link
Member

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

Does this work for you now by any chance?

@rhys-vdw
Copy link
Contributor Author

rhys-vdw commented Mar 3, 2017

Manually trigger save actions

You can now save an editor via ⌘S 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 ⌘S to be a problem, just dirty checking in :w.

@xconverge
Copy link
Member

ah ok yea I forgot we still had an isDirty check

@xconverge
Copy link
Member

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>;

@xconverge
Copy link
Member

microsoft/vscode#22040

@evenfrost
Copy link

evenfrost commented Mar 12, 2018

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.

@evenfrost
Copy link

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": []
    }]
  }
]

@jhwheeler
Copy link

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

@evenfrost
Copy link

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

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

@jhwheeler
Copy link

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

@jmrichardson
Copy link

jmrichardson commented Mar 23, 2019

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

@vegerot
Copy link
Contributor

vegerot commented Nov 7, 2019

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

@jaoxford
Copy link

jaoxford commented Feb 16, 2020

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.

@enkeyz
Copy link

enkeyz commented Feb 2, 2021

Same issue with version 1.52.

@jpoon
Copy link
Member

jpoon commented Feb 5, 2021

I would suggest up-voting the VS Code issue: microsoft/vscode#22040. The extension passes thru the :w command straight to the VS Code save() api.

@rhys-vdw
Copy link
Contributor Author

rhys-vdw commented Feb 7, 2021

I would suggest up-voting the VS Code issue: microsoft/vscode#22040. The extension passes thru the :w command straight to the VS Code save() api.

Looks like this is the solution:

The ITextFileService.save() method accepts a ISaveOptions that can carry a force flag. The currently only guy that sets force to true is our Save File action.

microsoft/vscode#22040 (comment)

@jpoon
Copy link
Member

jpoon commented Feb 8, 2021

I believe Ben's comment in the linked comment is offering proposals. AFAIK, this is still an open issue against VS Code to enable extensions a force save -- I haven't taken a look at the VS Code extension API lately.

@patricklw
Copy link

Any update on this issue? :w used to save files for me and just stopped working, probably with the latest update of vscode (my neovim hasn't chagned lately). I've got vscode 1.60.1.

@samredway
Copy link

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

In case people are not finding this. This solution works well in 2024 on Cursor and I guess will still work on VS Code

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests