VS Code自动保存文件历史/撤消

VS Code auto-save file history / undo

vscode的自动保存功能非常方便,但有时会有点麻烦。

如果有办法查看自动保存的文件的以前版本,那将非常有用

vs code有这个功能吗?

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

无需使用 VSCode 1.66(2022 年 3 月)进行扩展。
它现在有:

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:

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.

还有一些新设置可用于本地历史记录:

  • 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:

注意:本地历史条目存储在不同的位置,具体取决于您对 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.