Make Visual Studio Code open in clean state - visual-studio-code

I work in multiple projects spread in multiple folders on macOS.
I usually start working on them by running:
cd ~/workspace/project-a
code .
That always causes a new window to be open with the last files I worked on that project. My next move is to close all tabs, if the editor was split I have to do that as many times as split editors I had.
Is there a setting that would allow me to always start on a clean state?
My settings that I believe are related to this issue are the following:
"window.restoreWindows": "none",
"files.hotExit": "onExitAndWindowClose",
I tried off for files.hotExit but the behaviour remained the same.
Also if possible, where is this information stored (open files for given folders)? Is that a dot file inside the folder or elsewhere inside Visual Studio Code installation?

Make sure you are on the latest VSCode (1.24.1 as on 18-Jun-18)
Then make sure you have below in your settings
"window.restoreWindows": "none",
Make sure there are no JSON errors in your custom settings file. This also could cause the settings to be not loaded at all.
Next try launching the folder using
code -n .
Also $HOME/Library/Application Support/Code/Backups/workspaces.json contains the information of open workspaces
Also refer to below thread
Visual Studio Code always reopens previous file or folder

You could try, in addition of the setting "window.restoreWindows": "none" to start with:
code -n
That would force a new VSCode Window to be opened.
Also if possible, where is this information stored (open files for given folders)?
See issue 3884
# Windows
%APPDATA%\Code\Workspaces
# Mac
$HOME/Library/Application Support/Code
$HOME/Library/Application Support/Code/Workspaces
$HOME/Library/Application Support/Code/Backups/workspace.json

Window > Open Folders In New Window
set it to off

Related

How to change folder that opened by default in VSCode?

I have small problem with VSCode folder, that opened by default.
Problem description: I start new instance of VSCode (trough File->New Window), and then if I choose File->Open Folder it opens dialog with my Windows user folder as starting point (C:\Users\MyUser)
Question: How can I change that folder in settings (if it possible)? So by default it will show as start point for example D:\development\ ?
At the time I write this answer, this is not possible. There are two problems on Windows, and one problem on Mac and Linux:
VS Code does not provide a default path to the file dialog 1. It does remember the last folder that you opened a file in, but that path cannot be used as a default because it is overwritten constantly.
On Windows only, Electron ignores the default path when creating a file dialog if the default path is a directory 2.
An extension also cannot solve this, because extensions are not allowed to modify the File menu 3.
I think the best option at this point is to pin a folder to the Quick Access area in Windows Explorer, as suggested in a comment, or to put an actual shortcut in the user profile folder.
Workspaces and File > Open Recent may also be helpful if you often open the same folders.
Your main problem is that you are unable to open your specific folder in VScode.
To solve that you can simply open the terminal/cmd in that specific window by just typing cmd in your search bar or just by pressing shift+right-click in that folder.
Now your cmd is open and you just have to type "code ." in the cmd and press enter to open the current folder in your VSCode.
In case that code . doesn't work for you then you have to add the Vscode in the environment variables of your windows.
Visual Studio doesn't provide a specific feature to open a specific path. But there is a solution to your problem. You are saying that you want D:\develpment as a default when you open VS Code. You can go to that specific directory or create shortcut to desktop then click right click on that folder and then click on open with code. If you didnot see open with code then reinstall your VS code and check on open with code when you are reinstalling VS Code.
make a shortcut on the desktop for vscode and then modify it and add the folder after the .exe command. This will default open that folder when you double click on it.
Visual Studio Code can be installed in two ways - User setup and System setup. I strongly believe you have User setup installed in your PC. Try re-installing it System-wide. That should probably fix your problem.
For more information: https://code.visualstudio.com/docs/setup/windows#_user-setup-versus-system-setup
PS: A lot more information is needed, you can share a screenshot of the window and elaborate more on it.

Visual Studio Code source control not showing changes

Visual Studio Code source control panel is empty when I click on it. Nothing to expand and nothing to click on.
Things I've tried:
Uninstalled/Reinstalled Git
Uninstall/Reinstalled VS Code
Removed extensions folder
Open your project with cmd.
> cd your-folder-location
> code . -n
It worked for me
Dude, just lost an hour because my SCM in VSCode randomly stopped showing anything today. I restarted everything, tried git init, everything on the forums. Made sure Git built in extension is enabled, mine was already enabled so I was totally lost.
All I had to do was disable and then reenable the built in Git extension. and it fixed it.
Go to Extensions.
Filter by "built in".
Click the gear icon by Git, and click disable.
Then click it again, and click enable.
Here is a screenshot reference
In my case, somehow, the Source Control Repositories option, available under the 3 dots ... on the SOURCE CONTROL tab, was no longer selected.
All I had to do was press the ... and select Source Control Repositories, then select the correct repo, and all the changes were again listed.
I had a similar issue. It seems vs code has two source control extensions. When I clicked View -> SCM it opened an extension with changes displaying.
source control extension 1
source control extension 2
I had this problem 2-3 times for the last 2 years (OS -> Linux Mint). The changes on any file didn't appear to the source control nor have they been marked on the line I've edited. When manually go to "Source control" and click on the refresh button they appear but the lines that I had change didn't light up (there were no visual marking on the files after editing them). This happened when I switched to a different branch while the workspace was open to the 2 monitors at the same time. Or when working on several projects (opened 2-3 or more VS Code instances). The scariest thing was that it didn't work not only for one repository(project) but for all of them. I've read alot on the subject and tried everything that I found and think of. There is some issue with git path mapping or something.
The thing that I tried:
reload VS Code
restarting VS Code
disable all extensions
enable/disable all git related options in (file -> preferences -> settings)
deleting (folders and files) and cloning the repository
updating git
removing and installing git
restarting PC (don't judge me I was desperate)
But the only solution that worked for me was:
open VS Code (if open, don't close it)
go to the directory where you keep your repositories (not from VS Code but from you file explorer).
go one folder above it (if you are in .../{{some folder}}/{{you repos}}, go to ../{{some folder}})
then open you repositories containing folder (/{{you repos}}) by right click -> open with VS Code
wait until everything loads. The Source Control will mark alot of changes, don't worry about it.
then close VS Code (all windows (instances), because it will open a new instance)
after that go to the directory where you keep your repositories again and right click and open with VS code the repository of you choice. Now at this point the Source control will start working properly.
What worked for me was going to my "code" folder where I keep all my repos, right-clicking on the folder containing the repo I want and opening that folder with VS Code.
The VS Code window for this specific repo was closed. I did have another window for a different repo open. As soon as the window opened, the changes showed up in source control and I was able to commit, push and everything else like normal.
I faced this problem when I opened a repo in a directory inside symlink.
My solution: just open this directory in original destination without any symlinks
I had this problem in a repository not as a problem from config but because I had a coverage folder with thousands of files not tracked and it seemed to slow the process of checking that out too much.
So I added that folder to .gitignore and it started working again.
Restating my Vscode And Giving time to load properly Solved my Problem
I had the same problem. What I did was:
Open another folder with File -> Open Folder...
Close the VSC
Open VSC
Open the original folder with File -> Open Folder...
After this I saw that the source control started loading and my changes came back.
Hope this works for you.
I couldn't see any changes in while trying git status. I opened changed files in text editor and they were not changed either. That lead me to conclusion that changes can't be seen by the system (and therefore by git).
The Autosave option was disabled, simply saving the changes helped.
That was my beginning with VSC, in Pycharm never had such problem.
I had this problem, because I was changing files one folder down from where I opened Visual Studio Code.
Solution- open Visual Studio Code without a location, File/Open Folder - open the folder I am directly working out of.
Unstaged changes now show in the direct folder I'm working in. Unstage changes previously auto-staged by Visual Studio Code when working on a nested project directory to see them (open a Terminal and run git reset).
I encountered the same issue, and I fixed that by removing the files.watcherExclude property in settings.json file.
Because the value of files.watcherExclude became { "**/**/*": true } somehow.
// settings.json
// remove or comment next line
// files.watcherExclude: { "**/**/*": true }
For me, the files were in WSL (Windows subsystem for linux) but I was not opening the folder as such.
in the bottom left, click the green >< symbol, then click "reopen folder in WSL"
VSC remote mode image
presto.
Go to View -> Terminal
cd to root folder, and run git status and see if you have any errors
I had a unsafe repository fatat error, as my repo was on a network drive. Did as suggested by git to add an exception and it fixed the issue.
close vscode
moved local Code config folder as backup (~/.config/Code/)
reopen vscode (this will still show problem)
close and reopen vscode (this will show db re-write issue but it will re-create config folder.)
this solved my problem. this reset many of my settings but It can be checked from config backup.
In setting check Git: Autorefresh
I experience this problem when I right click a folder and open it up with VS code. Instead now I start VS code from the start menu and after that I use File -> Open Folder option.
I experienced this issue with VSCode V1.70.1, all I did is just closing VSCode completely and open a new instance and I could find git changes appears simultaneously as expected.
If running into this issue on a Mac, make sure you are running your instance of Visual Studio Code from your Applications folder and not your Downloads folder. I managed to fire up an instance from the wrong folder and this prevented my Source Control from being able to properly load git info and also caused Visual Studio updates to fail. More on the issue can be found here.
Here is another possible solution for Linux users:
In my case, it was only not showing lines changed with the file open. Source control tab was showing fine.
I have a symlink from /var/www to /mnt/{hdd-uuid}/www. When I created my workspace, it was using the path /var/www/project-folder, instead of the full path, and this was giving me the error.
I opened my .code-workpsace file in another text editor and changed the references in the JSON from ../../../../var/www/project-folder to /mnt/{hdd-uuid}/www/project-folder, then reopen VS Code. Close all file tabs open and, when you open again a modified file it will show the lines changed.
It is possible that you need to trust the repo again.
Try opening project or folder which contains git files. Later try opening your wanted folder. This sorted the issue for me.
Double-check git is actually installed on your system. I just did a fresh install of Windows 11 the other day, and although one of the development tools I installed thereafter downloaded and supposedly installed git in Windows, actually, it never installed it. Fail!
I fixed this issue by toggled-on the AutoSave feature in VSCode via File > AutoSave. I noticed that the badge on the github does not show up until the file is actually saved first.
What worked for me was that I was forgetting to save, so just enabled autosave option.
File > Auto Save (check)
Open Visual Studio code -> View -> Appearance -> Show activity bar

Keep opened files between close and reopen

If I open some single file (without creating any project), and then close and reopen VS Code, the file will be restored. Okay, that's what I want.
But, if I open some file, then close VS Code, and then open another single file, the first file will not be reopened.
Is there way to fix it? Probably, some changes in "User Settings" (?).
I've already tried
"window.restoreWindows": "all"
but it doesn't help.
I think this is the same issue as described in the following feature request:
Keep previously opened files when opening a single file instead of clearing all files (#14675)
Which means that no, this is not possible at the moment.
A new setting preserve for window.restoreWindows has been introduced (#15949) in version 1.52 November 2020. This should do just what you want.
Go to
Settings > Window > In Restore Windows section set it to > all
That's it. There are other options you can find your best.
I am using visual studio code Version 1.27 on linux.

How to open multiples files in one Code instance from command line?

If I open a file in Visual Studio Code from the command line or from within File Explorer, each file will open in a separate Code instance. Is there a way to make them all open in the same instance? I.e. add them all to the 'Working Files' list?
Upcoming version will have a new option files.openInNewWindow which you can set to off to always open files into the last active instance. This is then identical to starting VS Code with the -r option but is much nicer to use (e.g. when you open a file from the desktop).
Update for our VS Code 1.0 release:
The setting is available as window.openFilesInNewWindow
You can use the command line option of -r or --reuse-window to assure it opens in the last active VS Code window
You can review the other options here
code Folder1/File1.txt Folder2/File2.java Folder3/File3.css
This command opens three files in one instance of the editor. All files are going to be added to the working files list.
This even works with much more files in the parameter list.

How to prevent Visual Studio Code from always reopening the previous files or folders?

Visual Studio Code always seems to remember my session and reopen the files and/or projects that were open the last time I used it. It obviously behaves correctly when running it from the command line with a file or folder supplied, but when opening from a taskbar shortcut, I'd like it to default to an empty environment.
Is there any way to change this behavior?
You can also go into your settings and use the following:
"window.reopenFolders": "none"
which will not reopen the folders you were working on when you closed the editor. The other options are one (the default) and all.
Edit 2017-11-09:
The option is now changed in latest versions.
"window.restoreWindows": "none"
See Mathieu DOMER's answer.
Edit 2018-09-12:
Another setting related to this is the hotExit setting. This has been discussed in this answer to a related question. To prevent reopening and remembering unsaved files, you can set this to:
"files.hotExit": "off"
But from the test I've made, when the window.restoreWindows setting is set to none, this is not needed. I haven't tested every possible combination, so YMMV.
And to answer a question in the comments, to edit the settings, you have to open the settings file. Some documentation can be found here (at least on the date I am writing this).
Edit 2022-03-16:
If you prefer using a GUI to change the settings, see D'Arcy Rittich's answer.
In VS Code:
for Windows/Linux Ctrl+, (or choose File/Preferences/Settings) to open the settings page.
for Mac ⌘+, (or choose Code -> Preferences -> Settings) to open the settings page.
then type restoreWindows in the Search settings input to filter for this setting. Set it to none and restart VS Code.
With latest update, it seems that the parameter has changed, now use:
"window.restoreWindows": "none"
You can add the -n option to the startup of VS Code and it will always start with an empty window, not restoring your previous session.
01 December 2018
This works for me. i.e. "C:\Users\Sampath\AppData\Local\Programs\Microsoft VS Code\Code.exe" -n
For me, none of above is working while I'm trying to close "dirty" unsaved files which I accidentally edited 1000 files and wanted to ignore saving all of them.
My fix was adding this line into settings.json:
"files.hotExit": "off"
Open up vscode, close vscode and just click the confirmation button to close all of the files without saving.
Then open back vscode and boom.. no more unsaved files being shown.
If "window.restoreWindows": "none" not solve the problem,then try to run code as root -> sudo code --user-data-dir code files and restart code normally without root.
Below worked for me
Right-click on Shortcut and add --disable-gpu to Target as per screen shot.
For me the only solution that worked was to go to the solution root and delete the .vs folder.
I reinstalled Visual Studio Code by downloading the latest update. I did not have to uninstall the previously installed Code. It work ok for me now.