213

I have used WebStorm from JetBrains for almost four years now. It's a fantastic IDE for many reasons, but one of the best features is that it saves versions of files outside of version control. So if you accidentally delete files or lose files before they are saved by your version control system, WebStorm has a copy of them and there are visual diff tools to use. This feature has saved me on more than one occasion.

For Visual Studio Code, is there some feature/plugin that will auto-save copies of files as they change? Will Visual Studio Code save the files to some central location, or perhaps in the .vscode folder in the local workspace?

The feature in WebStorm is available from Local HistoryShow History from a folder or file. Here is an article about it: Using Local History for code changes tracking

The view looks like:

Enter image description here

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Alexander Mills
  • 90,741
  • 139
  • 482
  • 817

6 Answers6

306

You can go to menu File and choose Auto Save.

Enter image description here

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
SinaMN75
  • 6,742
  • 5
  • 28
  • 56
  • 4
    All this does is save the _current_ file though - it doesn't give you a backup copy? – MikeB Jul 08 '20 at 10:31
  • @MikeBrockington You should use git for such matter – Daneal Oct 12 '21 at 22:12
  • 4
    @Daneal How does GIT help me with my local workflow - I need local copies. – MikeB Oct 13 '21 at 13:46
  • @MikeBrockington create a local git repo, then. You'll be able to jump to any commit and there's no need to have a remote origin – Daneal Oct 14 '21 at 18:03
  • @Daneal Still doesn't give me local COPIES of each FILE, and what it does give is at the expense of a massive change to workflow for me and my colleagues, and requires a new tool that we don't currently use. – MikeB Oct 15 '21 at 09:16
  • 2
    This may not have answered the OP's question, but it answered mine. After getting used to IntelliJ automatically saving after each change, when I switched to VSCode I keep finding it a pain to make sure I have saved every file I changed before compiling. – Andrew Allaire Jan 22 '22 at 00:02
  • 2
    Local history is super useful feature, autosave is not a replacement. Question should've been titled as 'local history in VSC' not 'auto save in VSC' – Alleo Jun 02 '22 at 18:18
118

You can enable auto save with one of this methods:

Method 1. Check the Auto Save item in the File menu

Visual Studio Code auto save activation

Method 2. Go to Settings, search for auto save, and select the auto save option (afterDelay)

Visual Studio Code auto save activation

Auto save description in Visual Studio Code documentation

Hamed Naeemaei
  • 8,052
  • 3
  • 37
  • 46
  • Thanks, but I don't think the second step is necessary anymore. After doing the first step, the setting was already as you described in your second step and I didn't need to change anything. Maybe VSCode developers ready your post and set "Auto Save" to "afterDelay" by default for the newer versions :) Please correct me if I'm wrong – Positive Navid Mar 02 '23 at 16:47
41

Yes, Visual Studio Code can auto-save changes on files as you make changes. It also allows you set a delay for how long to wait before saving the file.

Here's a link that should help you with that.

Or a shortcut you can simply navigate to your Visual Studio Code settings, and add the following to your settings.json file.

{
    ...
    "files.autoSave": "afterDelay",
    "files.autoSaveDelay": 1000,
    ...
}

This will instruct your editor to autosave after a 1000 ms delay. You can set the autosave option to onFocusChange to autosave whenever you move your cursor away from the current text area.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
proton
  • 1,639
  • 6
  • 18
  • 30
16

There's a package called Local History that can be used to save a backup of your files outside version control.

You should check that out.

proton
  • 1,639
  • 6
  • 18
  • 30
  • 3
    Very cool, but it does come with issues. For one, make sure to add the `.history` folder to your `.gitignore` file (_because that .history folder is created within your main working folder_). – Chris22 Dec 09 '20 at 23:50
  • When does it store exactly? After closing VScode? After saving? After a n sec timestamp? – LuckyLuke Skywalker Jun 19 '22 at 21:40
5

As of March 2022 (version 1.66), Visual Studio Code has a built-in feature called Local History that automatically creates commit-like versions of your files on every save.

It is enabled by default, but you can double check the setting workbench.localHistory.enabled to make sure it is enabled in your project.

From the reference:

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.

The saved local versions are accessible from the Timeline section in the File Explorer tab.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
thehale
  • 913
  • 10
  • 18
1

Also, ensure you add .history to your .gitignore file, so Visual Studio Code doesn't track every change made to a single file as changes made to multiple files. This can be a problem and make your files changed so large even though it's a single file or a few files you made edits to.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
  • 1
    Please add further details to expand on your answer, such as working code or documentation citations. – Community Sep 01 '21 at 05:57