155

Working with Visual Studio Code I have noticed if a file you are working with change, whenever that file get focused in a code panel it will be reloaded from the disk (if you don't have changes in the file through VSCode).

However, if you are on that file there is no alert to warning you about file changes.

I've been reviewing the settings and I cannot find anything like the visual studio option:

Detect when file is changed outside the environment

So my question: Is there any hidden setting or some hack to make that warning happen.

Update

Solved in version 0.3.0 of Visual Studio Code.

  1. The file will be updated from disk if there is no changes through the editor. (very useful to read log files during a process execution)
  2. If there are changes on both sides (from disk and through the editor) when ever you try to save the file using VSCode, the editor will warn you about that situation (i.e. "dirty writes") and a file comparison will allow you to decide what to do.
Nate Anderson
  • 18,334
  • 18
  • 100
  • 135
Juan M. Elosegui
  • 6,471
  • 5
  • 35
  • 48
  • 1
    if you have changes, why would you NOT want them? – John Papa May 07 '15 at 01:23
  • 17
    @JohnPapa I'm trying get the same behavior I find in visual studio or sublime text. If I have no changes through the IDE the file on the IDE should be updated to the latest version from the HDD. If I have changes through the IDE and also outside the IDE a warning should be popped up alerting of that situation. Something like "The file xxx has changed on disk. Do you want to reload it? " – Juan M. Elosegui May 07 '15 at 01:48
  • See https://stackoverflow.com/questions/54995008/how-to-temporarily-disable-preventing-dirty-writes-feature-in-visual-studio-co/59941475#59941475 and https://github.com/microsoft/vscode-docs/blob/vnext/release-notes/v1_42.md#ignore-save-conflicts for some changes to this behavior, including the ability to prevent the notification if you wish. – Mark Jan 28 '20 at 02:43
  • 4
    @johnPapa easy. Let's say you've been editing a file thinking it was a different file. You suddenly realise that and want to lose your changes. Or more relevant, you checkout a different git branch. Almost any other editor such as notepad++ will immediately say "file has changed on disk. do you want to reload?. Vscode will not detect it, and you can make changes and save and end up overwriting the wrong branch which is terrible. Can't believe I'm still having this problem in 2020 – Gostega Apr 02 '20 at 00:49

5 Answers5

88

VSCode will never refresh the file if you have changes in that file that are not saved to disk. However, if the file is open and does not have changes, it will replace with the changes on disk, that is true.

There is currently no way to disable this behaviour.

Benjamin Pasero
  • 113,622
  • 14
  • 75
  • 54
  • You are rigth. I Should be more specific about having changes in the file on VSCode. I will update the question. – Juan M. Elosegui May 06 '15 at 17:41
  • 80
    Other editors all have this. Some files may be accidentally modified by the user but what the user really wants is to be told this file has changed on disk and asked if they want to reload, at least via some notification pane on the editor. – Warren P Jan 27 '16 at 15:23
  • @MrDuk I know it's too late now but if you encounter this again, `git reflog` can be super useful in situations like that – Erik Inkapööl Dec 29 '16 at 12:30
  • 5
    Other editors may have this but this behavior persists even when closing and reopening VSCode which is very dangerous if you don't expect that to happen. You might have 10 files open, go back to VS2017 and then come back to VSCode 2 days later and do 'save all'..... – Simon_Weaver Jan 03 '18 at 05:29
  • 1
    @Simon_Weaver, your scenario will happen regardless of if there is a dialog to confirm reloading a file that has changed on disk. In your scenario Code will never reload the file (since you have editor changes). Obviously people want to work in different ways. I, for sure, belong to the group that want to be able to select if the file shall be automatically reloaded or not. The way I work (in some specific environments) it's essential a file is _never_ reloaded automatically. – Ingemar Jan 16 '18 at 12:34
  • 6
    @Ingemar I really wanted to give VSCode a shot (not least for Angular intellisense) and perhaps switching between VS2017 and VSCode. I'd often make a change in VSCode but not save it, then end up changing the same file in VS2017. I'd switch back to VSCode and accidentally resave the file over my change (even after closing/reopening). In just a couple days my changed vanished at least twice and once I finally figured out why it was pretty much the end of VSCode for me. At the very least VSCode should warn when you saving a file that had been externally modified since it was opened in VSCode. – Simon_Weaver Jan 16 '18 at 20:08
  • @Ingemar There were other things like double click vs. single click that bugged me and having to wade through pages of text options instead of a menu - I love the sound of the configurability but I just didn't have time for it on top of a steep Angular learning curve. – Simon_Weaver Jan 16 '18 at 20:11
  • @Simon_Weaver, agree completely about that VSCode should warn about overwriting modified files. And I also find it a bit harder to go through settings in text format instead of a well designed menu. Seems like MS has adopted some "good old" *nix standards :) – Ingemar Jan 17 '18 at 07:55
  • My use case is this: I have a webapp log file open is vscode. I want it to refresh it, and from time to time I select all + delete it (on localhost dev machine) to get rid of the clutter. It will not refresh afterwrds. – igorludi Mar 16 '18 at 09:25
  • 1
    @BenjaminPasero there should be a warning as soon as the editor is focusing on the file (again) or some input is made. the alert that something changed only as you try to save is just too late, which might be all this fuzz is about. – sjas May 12 '18 at 20:30
  • also a link to the bugtracker might help in getting further feedback – sjas May 12 '18 at 20:32
  • 1
    Not being able to configure if I want or not a notification on auto-refresh is a complete deal breaker for me. – arod May 25 '18 at 17:21
  • 4
    This answer is now 3 years old, has anybody opened an issue about this? Can you please post the link here? – Fabio says Reinstate Monica Aug 31 '18 at 14:36
  • One way to go around this is enabling autosave. Because of autosave, there will never be unsaved changes that would prevent reloading the file after an external operation like `git reset`. Works well for me, though I'm only using VS Code for files in git repositories anyway. – borellini Nov 20 '18 at 13:12
  • Is there a way to force reload? Whether you have changes or not? Command + R in the browser reloads a page. Can the same be added for VSC? If the file is newer can something be shown in the editor `mypage.html (hey dude newer changes on disk)`? – 1.21 gigawatts Jan 25 '19 at 01:11
  • 52
    `File: Revert File` can be used to get fresh contents from disk. – Benjamin Pasero Jan 25 '19 at 13:59
  • 2
    @BenjaminPasero you should post that as an answer. Hit Ctrl + Shift + P to open the command pallette, type "revert", and hit enter and it will refresh the file. I'm generating json files as part my application and am trying to use VS Code just to prettify and view them but it was super annoying without this command. – Jason Masters Feb 04 '19 at 20:18
  • @JasonMasters how is that the answer to the question here? It is an answer to a comment that was made. – Benjamin Pasero Feb 06 '19 at 18:20
  • Close the tab and then double click on the file in the Solution Explorer - that will reload from the disk - tried on VS 2019. – user674669 Nov 27 '19 at 03:07
  • "VSCode will never refresh the file if you have changes in that file that are not saved to disk." -- I would remove "that are not saved to disk" because even if you save your changes to disk, the file will not auto-refresh. – thdoan Nov 05 '20 at 18:09
  • > VSCode will never refresh the file if you have changes in that file that are not saved to disk. lol, of course not, why would you be productive when you could be worrying about the state of your hard drive. – gnkdl_gansklgna Jul 13 '21 at 21:07
  • Don't ever say never, because it isn't true any more. – windmaomao Sep 27 '21 at 13:48
  • version 1.67.2 windows 10 still broken! I lost my changes many times because of this crappy editor. :-(. Every other decent IDE will show a confirmation dialog. – Petr Jun 12 '22 at 21:31
39
{
    "files.useExperimentalFileWatcher" : true
}

in Code -> Preferences -> Settings

Tested with Visual Studio Code Version 1.26.1 on mac and win

Won Jun Bae
  • 5,140
  • 6
  • 43
  • 49
  • 1
    given an upvote as it works, but you missed quotes in the key. plese fix that in answer – Shivendra Agarwal Aug 30 '18 at 19:04
  • 3
    It doesn't work for me (VSCode 1.26.1 on Windows 10): when a file is modified externally, if in Code there are no changes it is silently reloaded, otherwise it won't do anything and then when I try to save it it will warn me about the conflict, offering to solve it by comparing the 2 versions. All of this happens regardless of `files.useExperimentalFileWatcher`, as I've tried both true and false, and I haven't seen any difference. – Fabio says Reinstate Monica Aug 31 '18 at 14:17
  • 7
    Version 1.28.2 (1.28.2) `Code -> Preferences -> Settings` and search for "watcher" Check the box. Works for me. – Shanimal Nov 07 '18 at 22:37
  • 2
    Damn, that eats CPU like crazy on Linux. – abhi1010 Jan 22 '19 at 02:57
  • 16
    @WonJunBae What does this do? Please give a description for this feature you are recommending. – 1.21 gigawatts Jan 25 '19 at 00:50
  • 1
    @1.21 gigawatts, Enabling this feature made my VSCode recognize new files that were added externally. – Damion Gomez Apr 29 '19 at 16:53
  • 2
    This still only works as long as you will not restart VSCode. If you leave a file unsaved and save it later, after a VSCode restart, any changes made within another editor will be lost, without a warning. – Cosmin Rus May 10 '19 at 11:57
  • 8
    Unknown configuration setting on VSCode 1.41.1 – Josh Correia Feb 06 '20 at 19:01
  • does not exit in 1.67.2 :-( probably remover forever then – Petr Jun 12 '22 at 21:41
  • Yeah, @JoshCorreia, https://stackoverflow.com/questions/54122049/how-to-make-vscode-to-forcefully-reload-a-changed-file#comment135075500_54122419 – rokejulianlockhart Jul 04 '23 at 11:11
28

SUPER-SHIFT-p > File: Revert File is the only way

(where SUPER is Command on Mac and Ctrl on PC)

David 天宇 Wong
  • 3,724
  • 4
  • 35
  • 47
2

In version 1.57.1 (June 2021) there is still no setting like Detect when file is changed outside the environment. But if the file was accidentally changed outside, you can easily revert the changes with just the Undo (Ctrl+Z) command

elshev
  • 1,164
  • 3
  • 16
  • 30
2

DEPRECATED:

On Ubuntu, after creating a new file using vscode, it only shows up once I refresh the explorer manually. It's frustrating. There was a key I changed in settings.json, and everything worked fine after that:

"files.legacyWatcher": "on"

Save, restart the vscode, and be safe :)

Ali Askari
  • 341
  • 3
  • 7