5 votes

How frequently does vscode save dirty files?

trying to settle on a program where I can jot down stuff and have some degree of confidence that it will persist and I can't seem to find any actual documentation from vs code as to:

  1. How often unsaved files are saved so that they can be restored when a session is reopened
  2. if that frequency of how often they are saved can be configured?

Sidenote: I was going to ask this question on SO but apparently I am at risk of a question ban for not asking "good quality questions". Half my questions on there are from when I was a fresh developer and didn't really know how to articulate my questions well. Really changes my opinion of the openness of SO......

29 comments

  1. [6]
    zoroa
    Link
    vscode has documentation that answers both of your questions: https://code.visualstudio.com/docs/editor/codebasics#_save-auto-save If you're specfically asking about vscode's behavior of...

    vscode has documentation that answers both of your questions: https://code.visualstudio.com/docs/editor/codebasics#_save-auto-save

    However, it's easy to turn on Auto Save, which will save your changes after a configured delay or when focus leaves the editor. With this option turned on, there is no need to explicitly save the file. The easiest way to turn on Auto Save is with the File > Auto Save toggle that turns on and off save after a delay.

    For more control over Auto Save, open User or Workspace settings and find the associated settings:

    • files.autoSave: Can have the values:
      • off - to disable auto save.
      • afterDelay - to save files after a configured delay (default 1000 ms).
      • onFocusChange - to save files when focus moves out of the editor of the dirty file.
      • onWindowChange - to save files when the focus moves out of the VS Code window.
    • files.autoSaveDelay: Configures the delay in milliseconds when files.autoSave is configured to afterDelay. The default is 1000 ms.

    If you're specfically asking about vscode's behavior of preserving dirty files that haven't been written to disk, I can't find any documentation either. But can offer the anecdote that I can't remember an instance of vscode failing to persist a dirty file across sessions in the 7 or so years I've used it.

    19 votes
    1. [3]
      overbyte
      Link Parent
      A quick poke at VSCode calls it internally as an "untitled working copy", and new files get flagged as such:...

      A quick poke at VSCode calls it internally as an "untitled working copy", and new files get flagged as such: https://github.com/microsoft/vscode/blob/b4b2590456bd2e6326814ad3f1cdc8ab8f2357ca/src/vs/workbench/services/workingCopy/common/workingCopy.ts#L19-L24

      The flag does a few things like not using the regular auto save logic: https://github.com/microsoft/vscode/blob/b4b2590456bd2e6326814ad3f1cdc8ab8f2357ca/src/vs/workbench/services/workingCopy/common/workingCopyBackupTracker.ts#L232

      This looks to be part of the logic that handles untitled files and it fires off a save whenever the file is modified, and there's no tweakables among the files that handle untitled copies:
      https://github.com/microsoft/vscode/blob/b4b2590456bd2e6326814ad3f1cdc8ab8f2357ca/src/vs/workbench/services/workingCopy/common/untitledFileWorkingCopy.ts#L200

      The untitled files are saved by default in %APPDATA%\Code\Backups\<random number>\untitled, which gets changed immediately if you watch the directory whenever you open/close the editor and add/remove unsaved files to it:
      https://github.com/microsoft/vscode/blob/48ba01b5896376718aa66a11c60305cb105815c0/src/vs/workbench/services/workspaces/browser/workspacesService.ts#L167

      6 votes
      1. [2]
        b3_k1nd_rw1nd
        Link Parent
        thanks! i dont suppose if you know if this has actually been documented anywhere though?

        thanks! i dont suppose if you know if this has actually been documented anywhere though?

        1. overbyte
          Link Parent
          Searching "untitled editor" yields the 1.42 release notes about Untitled editor improvements which is the closest you'll get aside from directly looking at the code itself, or ones scattered...

          Searching "untitled editor" yields the 1.42 release notes about Untitled editor improvements which is the closest you'll get aside from directly looking at the code itself, or ones scattered across other release notes that get displayed by default whenever VS Code updates.

          A related code change is saving UI state like the currently opened files in #87158.

          Given the nature of things like wikis and open source projects, things don't just appear out of thin air so feel free to contribute documentation changes to the project.

          1 vote
    2. [2]
      b3_k1nd_rw1nd
      Link Parent
      I am, which is why that link you provided is not related to my question.

      If you're specfically asking about vscode's behavior of preserving dirty files that haven't been written to disk

      I am, which is why that link you provided is not related to my question.

      4 votes
      1. davek804
        Link Parent
        This is about one step away from the same negative comment you made about SO above about poorly worded questions, from my POV. A little more pleasantry ("hey, thanks for taking a moment to put...

        This is about one step away from the same negative comment you made about SO above about poorly worded questions, from my POV.

        A little more pleasantry ("hey, thanks for taking a moment to put together an answer, but...") and a little less matter-of-fact tone can really improve textual conversation online.

        28 votes
  2. [2]
    Notcoffeetable
    Link
    Do you need an IDE? Is this for code drafts or just notes? I used Sublime for my quick notes. Over 2 years I've never had it lose stuff. I don't even bother saving most of the time it just...

    Do you need an IDE? Is this for code drafts or just notes?

    I used Sublime for my quick notes. Over 2 years I've never had it lose stuff. I don't even bother saving most of the time it just maintains each tab I have as a buffer between sessions.

    6 votes
    1. DynamoSunshirt
      Link Parent
      Second sublime. And if you're allergic to save, there's no need to pay for it -- the notification that asks you to buy pops up every 5 saves. Folks severely underestimate the power of Sublime's...

      Second sublime. And if you're allergic to save, there's no need to pay for it -- the notification that asks you to buy pops up every 5 saves.

      Folks severely underestimate the power of Sublime's theming and color scheme engines. There are a lot of nice options out there.

      3 votes
  3. elguero
    Link
    Nothing gets lost? I can write in a file, not save, restart the computer and continue editing from where I was. Using it in my day job and while I’m not a personal fan, it’s really reliable.

    Nothing gets lost? I can write in a file, not save, restart the computer and continue editing from where I was.

    Using it in my day job and while I’m not a personal fan, it’s really reliable.

    3 votes
  4. [6]
    Comment deleted by author
    Link
    1. [5]
      RheingoldRiver
      Link Parent
      it doesn't prompt you to save your unsaved files? (that's been my experience)

      If I make an edit and immediately close the program, the changes are preserved

      it doesn't prompt you to save your unsaved files? (that's been my experience)

      1. [4]
        Notcoffeetable
        Link Parent
        I just tested in my VSCode: Opened VS Code, waits for it to actually open typed some text closed w/o saving, received no prompt. Opened and my text is there. If I close the tab holding this text...

        I just tested in my VSCode:

        • Opened VS Code, waits for it to actually open typed some text
        • closed w/o saving, received no prompt.
        • Opened and my text is there.

        If I close the tab holding this text it asks if I want to save the file.

        1 vote
        1. [3]
          RheingoldRiver
          Link Parent
          ahhh ok I see. normally when I am closing something, it's a "window" or a "project" and not the entirety of vscode (X button vs File -> Exit) If you have multiple windows open and close 1 of them,...

          ahhh ok I see.

          normally when I am closing something, it's a "window" or a "project" and not the entirety of vscode

          (X button vs File -> Exit)

          If you have multiple windows open and close 1 of them, I don't believe you get this autosave functionality

          1 vote
          1. [2]
            b3_k1nd_rw1nd
            (edited )
            Link Parent
            FTFY And correct. which is why all my unsaved files are in the same windows which I close last when I am done with my computer.

            If you have multiple windows open and more than 1 of them has unsaved files and close 1 of them, I don't believe you get this autosave functionality

            FTFY

            And correct. which is why all my unsaved files are in the same windows which I close last when I am done with my computer.

            1 vote
  5. [4]
    stu2b50
    Link
    I'm not sure what you mean. Unsaved files, as in files you did not explicitly save in, are just not saved at all. There is no backup. It's gone. Files that ARE saved are written to disk...

    I'm not sure what you mean. Unsaved files, as in files you did not explicitly save in, are just not saved at all. There is no backup. It's gone.

    Files that ARE saved are written to disk immediately.

    It's a code editor, in the end, not Microsoft Word, so it's necessary that something like "is the file written to disk" be as transparent as possible.

    I'm sure there's a setting or extension that will just write the file to disk in the background periodically, but I'm not sure that's what you're after.

    1. [3]
      b3_k1nd_rw1nd
      Link Parent
      Open up VS Code, open a new file, type in random words and then close your VS code and then open it back up, that unsaved file with those random words will be persisted.

      I'm not sure what you mean. Unsaved files, as in files you did not explicitly save in, are just not saved at all. There is no backup. It's gone.

      Open up VS Code, open a new file, type in random words and then close your VS code and then open it back up, that unsaved file with those random words will be persisted.

      4 votes
      1. [2]
        RheingoldRiver
        Link Parent
        well there's a difference between open, write something, close and open, write something, computer crashes in the first case, it will probably prompt you "do you want to save your files" and if...

        well there's a difference between open, write something, close and open, write something, computer crashes

        in the first case, it will probably prompt you "do you want to save your files" and if you say no it will (understandably) delete your unsaved files

        in the second case, it will retain the locally-saved copies

        If you use Discord already, maybe jotting down notes in a server that you create and never invite anyone else to might be a good strategy for making sure you never lose anything; you can make a bunch of channels organized into categories and then write notes there which will be persisted to their servers.

        although if you require open source, I don't know of anything that will let you create a file, not save it, manually close the program, and expect it to still be there when you re-open

        1. b3_k1nd_rw1nd
          Link Parent
          VS Code does not prompt you if you have only 1 windows open with unsaved files.

          in the first case, it will probably prompt you "do you want to save your files" and if you say no it will (understandably) delete your unsaved files

          VS Code does not prompt you if you have only 1 windows open with unsaved files.

  6. [11]
    RheingoldRiver
    Link
    I'd recommend Notepad++ for this use case because it starts up in under a second. I don't save anything in n++ I just rely on its autosave and I never lose anything. Sublime is also a good choice,...

    I'd recommend Notepad++ for this use case because it starts up in under a second. I don't save anything in n++ I just rely on its autosave and I never lose anything.

    Sublime is also a good choice, but for me Sublime is about 1 second to start up and has some learning curve whereas npp is dead simple and quick.

    VSC only if you need all its features (e.g. it's a good choice as a markdown editor) but not for simple text notes, it's overkill & too slow for that

    4 votes
    1. [10]
      b3_k1nd_rw1nd
      Link Parent
      I am on Linux it's not open-source

      I'd recommend Notepad++ for this use case because it starts up in under a second. I don't save anything in n++ I just rely on its autosave and I never lose anything.

      I am on Linux

      Sublime is also a good choice, but for me Sublime is about 1 second to start up and has some learning curve whereas npp is dead simple and quick.

      it's not open-source

      1 vote
      1. [8]
        Notcoffeetable
        Link Parent
        Is the open source thing about money or principles? I only ask because VSCode is open source but Microsoft is.... Microsoft. I used Sublime throughout grad school without a license (which they...

        Is the open source thing about money or principles?

        I only ask because VSCode is open source but Microsoft is.... Microsoft.

        I used Sublime throughout grad school without a license (which they permit). The cost is minimal and I bought a license when I got my first big boy paycheck because it is an excellent product.

        6 votes
        1. [7]
          b3_k1nd_rw1nd
          Link Parent
          Are you basically arguing that that an open-source license on a codebase can't be trusted depending on who works on it?

          I only ask because VSCode is open source but Microsoft is.... Microsoft.

          Are you basically arguing that that an open-source license on a codebase can't be trusted depending on who works on it?

          1 vote
          1. [2]
            parsley
            Link Parent
            VSCode is "kind of" Open source https://github.com/microsoft/vscode/wiki/Differences-between-the-repository-and-Visual-Studio-Code. There are plenty of "Open source" projects you cannot really...

            VSCode is "kind of" Open source https://github.com/microsoft/vscode/wiki/Differences-between-the-repository-and-Visual-Studio-Code.

            There are plenty of "Open source" projects you cannot really build from the source. Many services only open source the client, or parts of the server, or things like that. Its often called Open Core business model.

            5 votes
          2. [2]
            dysthymia
            Link Parent
            It is open-source, but the open-source version of the program is only if you bother to compile it yourself. Otherwise, it contains some Microsoft binaries – this is mainly why...

            It is open-source, but the open-source version of the program is only if you bother to compile it yourself. Otherwise, it contains some Microsoft binaries – this is mainly why https://github.com/VSCodium/vscodium exists.

            2 votes
            1. b3_k1nd_rw1nd
              Link Parent
              yea, that's why I use vscodium now.

              yea, that's why I use vscodium now.

              2 votes
          3. [2]
            Notcoffeetable
            Link Parent
            Just saying Microsoft isn't exactly the GNU Foundation. But if you want access to the code base, then VSCode checks the box.

            Just saying Microsoft isn't exactly the GNU Foundation. But if you want access to the code base, then VSCode checks the box.

            2 votes
            1. b3_k1nd_rw1nd
              Link Parent
              What does that have to do with anything? the CIA could release a really useful tool and if it has the open-source license, I see no reason not to trust it.

              Just saying Microsoft isn't exactly the GNU Foundation.

              What does that have to do with anything? the CIA could release a really useful tool and if it has the open-source license, I see no reason not to trust it.

      2. tomf
        Link Parent
        you should mention these requirements when you post a question.

        you should mention these requirements when you post a question.

        5 votes