3

The auto-save feature of vscode is quite handy, but can be a bit of a trouble some times.

And it will be very usefull if there's a way to see previous versions of the file auto-saved

does vs code have such a feature?

Xsmael
  • 3,624
  • 7
  • 44
  • 60

2 Answers2

12

No need for an extension with VSCode 1.66 (March 2022).
It now have:

Local history

Local history of files is now available from the Timeline view. Depending on the configured settings, every time you save an editor, a new entry is added to the list:

Local History

Each local history entry contains the full contents of the file at the time the entry was created and in certain cases can provide more semantic information (for example, indicate a refactoring).

From an entry you can:

  • Compare the changes to the local file or previous entry.
  • Restore the contents.
  • Delete or rename the entry.

There are new global commands to work with local history:

  • workbench.action.localHistory.create - Create a new history entry for the active file with a custom name.
  • workbench.action.localHistory.deleteAll - Delete all history entries across all files.
  • workbench.action.localHistory.restoreViaPicker - Find a history entry to restore across all files.

There are also new settings to work with local history:

  • workbench.localHistory.enabled - Enable or disable local history (default: true).
  • workbench.localHistory.maxFileSize - File size limit when creating a local history entry (default: 256 KB).
  • workbench.localHistory.maxFileEntries - Local history entries limit per file (default: 50).
  • workbench.localHistory.exclude - Glob patterns for excluding certain files from local history.
  • workbench.localHistory.mergeWindow - Interval in seconds during which the last entry in local file history is replaced with the entry that is being added (default 10s).

A new filter action in the Timeline view toolbar allows you to enable or disable individual providers:

Timeline Filter

Note: Local history entries are stored in different locations depending on your use of VS Code.

  • When opening local files, the entries are persisted in the local user data folder and
  • when opening remote files, they will be stored on the remote user data folder.
  • When no file system is available (for example, in certain cases when using VS Code for Web), entries are stored into IndexedDB.

Note: Mina confirms in the comments that the file history does not only update when one saves the file manually (Ctrl + S), but can save it regularly, once you edit the "Merge Window" value (default 10s).

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • Not sure if commenting is the best way of asking this, but does somebody know how could I rename the file I'm working on (a simple script) and not lose all the TImeline history for it? There should be a way, isn't? Well, thanks in advance for any help! – Rai Oct 22 '22 at 02:36
  • @Ramon0 That reminds me of https://github.com/microsoft/vscode/issues/152415 or https://github.com/microsoft/vscode/issues/26339#issuecomment-1070985925: possibly implemented since Apr 2022: https://github.com/microsoft/vscode/issues/145461#issue-1174310726. Note, if you recreate the file (empty) with the old name, you should get back its former timeline. But it is a poor workaround. What version of VSCode do you have? – VonC Oct 22 '22 at 08:59
  • Thanks for answering! I've not found yet a solution by following those links, but in the last one it can be read: Test: Timeline ● "ensure history entries are migrated and preserved when you rename a file or the parent folder of a file when entries exist for the file (this only works when renaming via explorer in VSCode)". Not sure if that's really implemented or not, but VS Code doesn't let me to rename my file via its Explorer (maybe cause I don't have it opened in a "Folder" or "Workspace" fashion but just as one more opened file for editing?). Last version (1.72.2), BTW – Rai Oct 22 '22 at 12:25
  • 1
    @Ramon0 For renaming file, try https://marketplace.visualstudio.com/items?itemName=sleistner.vscode-fileutils – VonC Oct 22 '22 at 12:32
  • Anyway, I have tried to simply save it with a different name by using "Save As..." in VS Code and the local history get lost... I've also tried to change the name inside the "entries.json" file inside the corresponding History folder's folder without success. Maybe because there is something else that should be changed? But still wondering why that shouldn't work And, well, I don't know what more I could try... so any other idea/suggestion (besides the creation of an empty file with the old name) would be welcomed. Thanks again! – Rai Oct 22 '22 at 12:37
  • 1
    [**File Utils**](https://marketplace.visualstudio.com/items?itemName=sleistner.vscode-fileutils) does it! I don't know why I missed your last comment (maybe due to crossed commenting or something), but I just came again out of curiosity and realized about it. Thank goodness! Because I installed the extension as suggested, renamed the file through it and _voilà_! Of course, I'd prefer something so basic were native... but, well, I guess that's the way it is for now and it's definitely better than nothing. Thanks! – Rai Oct 24 '22 at 23:15
  • @Ramon0 Great, well done! – VonC Oct 25 '22 at 05:50
  • @VonC, thanks for the great answer, but I have an issue, I enable auto-save, but the file history only updates when I save the file manually ( ctrl + S ), do you have an idea how I can make it auto-save the file history same as Webstorm? – Mina Apr 27 '23 at 10:20
  • **UPDATE** It worked fine with auto-save, once I edit the "Merge Window" value ( default 10s ) – Mina Apr 27 '23 at 10:34
  • 1
    @Mina Well done and good point. I have included your comment in the answer for more visibility. – VonC Apr 27 '23 at 12:11
1

Use the Local History extention.

A visual source code plugin for maintaining local history of files.

Every time you modify a file, a copy of the old contents is kept in the local history. At any time, you can compare a file with any older version from the history. It can help you out when you change or delete a file by accident. The history can also help you out when your workspace has a catastrophic problem. Each file revision is stored in a separate file inside the .history folder of your workspace directory (you can also configure another location, see local-history.path). e.g., .history/foo/bar/myFile_20151212205930.ts

Igal Serban
  • 10,558
  • 3
  • 35
  • 40