67

How do we create a workspace in Visual Studio Code?

I see File menu commands to open, add a folder to and save a workspace, but nothing to create one.

The questions How can I create a workspace in Visual Studio Code? and How can I create a Visual Studio Code Python workspace? are specific to Python. I am asking how to create a generic workspace.

The question What is a 'workspace' in Visual Studio Code? provides some explanation of what they are, but it is not clear how to create them.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Sam Hobbs
  • 2,594
  • 3
  • 21
  • 32
  • 6
    Just close the workspaces and `File -> Add Folder to Workspace...` This will create a new workspace called *UNTITLED (WORKSPACE)* with chosen folder. `File -> Save Workspace as...` if you want to save it or `File -> Close Workspace` to delete and close. For all this I recommend extension `vscode-workspace-switcher`, though. – Traxidus Wolf Jun 24 '19 at 07:32
  • Again, you don't have to close. Just use Open Folder or Open Workspace or Open Recent (if you've opened it recently). Boom, you've switched. Open recent to go back. It doesn't get any easier than that. Maybe that extension keeps track of your workspaces making it easy to find ones you haven't used in awhile, but you certainly don't need it for merely switching workspaces. – aamarks Jun 25 '19 at 23:23
  • Re vscode-workspace-switcher - in my case, the "W shaped icon" doesn't appear, and no 'subsection" in the explorer exists. And "Set the extension's configuration option ... to an array of directory globs, representing the directories where your .code-workspace files are stored" - that would be each separate project directory - more work than dealing with the missing-functionality built-in. – JosephK Apr 28 '20 at 10:00

6 Answers6

30

I had your same question, but the answer is simple, you don't. There is no need to start a blank workspace. To start on a new project use Open Folder from the File menu: navigate to the project, create a folder if needed, and then select the folder. You are now working on any files within that folder as if it were a workspace. If you leave and open another folder or workspace with files still open in editors, those files will be open when you open that folder again. Visual Studio Code making things work effortlessly! Until you do something that specifically requires a workspace you don't have to save it. If you save a workspace with no settings changed to the root folder here's what it looks like: {"folders": [{"path": "."}],"settings": {}}. That's it. The open editors are saved internally regardless of whether you saved as workspace. Starting by saving a blank workspace is like having an empty file cabinet in your office. VSCode will inform you if you ever need that and you can save a workspace then.

I finally had a chance to use VSC on a Mac. The difference on a Mac is that Open File and Open Folder are under the same general Open command (apparently because Macs use the same browser to select files and folders). Just use that to Open a folder, and open files in that folder workspace using the VSC explorer. All this still applies. The folder is the workspace.

You only need to save that workspace if you want to Add Folder to Workspace (in which case VSC will prompt you to save your multiroot workspace if you exit) or if you want to save settings specific to your project folder (though you can do that without a workspace using a settings json in a .vscode folder within). Even then VSC will auto save workspace settings for the folder internally when you haven't explicitly made the project folder a workspace.

Do you need a workspace and the extra .code-workspace file involved? If you have multiple root folders it's certainly nice to open the workspace and have those all there in your next session. If it's just for settings for a project with one root it's not needed (maybe if you want a file you can share with others on the project), and if the reason you are changing settings per project is language related then it may make far more sense to add language settings to your main settings file and have them in effect every time you work on that kind of project.

In short every time you use Open Folder workbench.action.files.openFolder you are essentially switching to another workspace.

enter image description here

aamarks
  • 942
  • 10
  • 17
  • The reason I said to close any folder you have open is to ensure that the **new** workspace **only** has the intended contents. – Sam Hobbs Jun 25 '19 at 14:58
  • 4
    As soon as you use **Open** Folder you have a clean workspace with only that folder. Trying to make some pristine blank workspace (which the OP requested) by Closing, Adding, Saving before you've opened files or changed settings, has no significance and saves an insignificant file. All that and you can just Open (even if you already had a complicated multi-root workspace open) and never even save it even if you change workspace settings. It will all be there when you open it again. – aamarks Jun 25 '19 at 23:08
  • Can you update this please, as Code no longer has an 'Open Folder' command in the 'File' menu? (unless I disabled the command somehow on my machine) – dcorking Jul 30 '19 at 11:48
  • @dcorking, it does. I have the current version. It's the fourth item in the file menu along with Open File, Open Workspace and Open Recent. It's not different on Linux or Mac is it? – aamarks Aug 02 '19 at 03:21
  • Macs may call the first main menu item something else besides 'file'. You can get to the command typing 'open folder' into the command window as well. On Windows the shortcut to open the command drop down is `ctrl+shift+P`, and `ctrl+K` `ctrl+O` for Open Folder. – aamarks Aug 02 '19 at 03:32
  • @aamarks None of those methods work for me: CMD-P opens the command widget but 'open folder' has no matches there. Code has a File menu on macOS, but no Open Folder command on it. The only place I can find 'Open Folder' is on the Welcome tab that opens after you open a new window. This works but requires the additional step of opening a new window (similar to my answer https://stackoverflow.com/a/57270907/1899424 ). I think the underlying confusion (for users like me and the OP user34660) is that Code seems to have a one to one relationship between Window and Workspace, which I did not expect. – dcorking Aug 02 '19 at 09:31
  • @dcorking, You need `shift` in there: `{ "key": "ctrl+shift+p", "command": "workbench.action.showCommands"}` Without shift you're doing workbench.action.quickOpen. You can check Keyboard Shortcuts for that and also Open Folder. – aamarks Aug 03 '19 at 16:03
  • There is very little distinction between the two versions of the command widget, so thanks for the tip about `shift`. All the same, there is still no Open Folder command. The closest I can find in Keyboard Shortcuts are `workbench.action.files.openFileFolderInNewWindow workbench.action.files.openFolderInNewWindow workbench.action.openFolderSettings workbench.action.files.openFileFolder workbench.action.files.openLocalFileFolder `. Do any of those create a new workspace? – dcorking Aug 04 '19 at 14:03
  • You're going to have to do research for mac. This may answer it: https://stackoverflow.com/questions/42180953/is-there-an-open-folder-keyboard-shortcut-in-vs-code – aamarks Aug 04 '19 at 23:46
  • 3
    VS Code does not have Open Folder on Mac. Instead, comments on the Q&A you linked confirm that to open a folder you simply use the Open command that opens both files and folders https://stackoverflow.com/questions/42180953/is-there-an-open-folder-keyboard-shortcut-in-vs-code . Please edit your answer to mention that it doesn't work on Mac or link my answer https://stackoverflow.com/a/57270907/1899424 – dcorking Aug 05 '19 at 07:44
  • The accepted answer on that page says nothing about a Mac specific problem. I'm finding it hard to believe that a basic command like this doesn't exist on Mac. You're using the current version of VSCode, 1.36?? And not the context menu? The menu here doesn't note anything specific about Mac and they seem careful to point out differences where they exist: https://code.visualstudio.com/docs/editor/multi-root-workspaces#_adding-folders. (Note, the info on that page is for multi-root and Add Folder where you will have to save the workspace, unlike Open Folder where you don't). – aamarks Aug 05 '19 at 13:44
  • The answer was edited to show that you open a folder on a Mac using the Open command. All else in the answer still applies to a Mac. When you open a folder you have a workspace. You don't normally need to explicitly save the workspace. When you reopen that folder, any files you had open previously will be there just like you'd expect from a workspace. – aamarks Mar 28 '21 at 19:44
22

In the folder you want to add to your workspace, create a file {foldername}.code-workspace and in the file put in the following code.

{
    "folders": [
        {
            "path": "."
        }
    ]
}

Save and close the file. Back in Visual Studio Code, click menu FileOpen Workspace... and select the .code-workspace file you created and it will open it as a new workspace without having to add a folder to an existing workspace.

See : Official reference for more details

KNU
  • 2,560
  • 5
  • 26
  • 39
Chicostix89
  • 367
  • 2
  • 5
  • Where would you place this file? – Isaac Lubow Nov 18 '19 at 20:36
  • You wouldn't, there is no reason to write an empty file like that. You're just making unnecessary complications. – aamarks Dec 10 '19 at 08:52
  • 1
    This works. @aamarks, it's not an unnecessary complications if it works. – Ercross May 06 '20 at 15:38
  • @Ireti, it is completely superfluous to have that empty file. I've written a complicated VSC extension in addition to various other little projects without setting a workspace. The folder is the workspace. Only certain projects and team groups may need to explicitly set a workspace as I've stated in my answer here. – aamarks Jun 14 '20 at 18:02
  • This method has some sense (though it's not cool), because if you want to use workspace this file must exist anyway – kanlukasz Jun 16 '20 at 10:32
  • I would suggest that the answer from @User34660, which does not involve the manual creation of a project file, is to be preferred an the grounds of simplicity. – holdenweb Oct 20 '20 at 13:41
12

In the File menu choose "Open Folder...". Select a folder. You can add folders to the workspace using "File" | "Add Folder to Workspace...".

Optionally, also go to "File" | "Preferences" | "Settings". The "User Settings" tab will be open by default. Look for the "Workspace Settings" tab and select it. Modify a setting, such as change the theme.

Then you can save the workspace using "Save" in the File menu.

When you close the workspace using "Close Workspace" in the File menu then if you changed the theme for the workspace then the theme should revert to the global theme. When you re-open the workspace VS Code will open the folder(s) that were added to the workspace and apply whatever settings that were specified for the workspace. There are a few settings that are ignored for workspaces for security reasons but all other settings can be overridden in workspaces.

It is possible to add multiple projects to a workspace; see Multi-root Workspaces.

Sam Hobbs
  • 2,594
  • 3
  • 21
  • 32
7

If you already have a window open with project files or folders, and you want a new workspace to work on a different project:

  1. From the 'File' menu, choose 'New Window'
  2. From the 'File' menu, choose 'Add Folder to Workspace' and choose the root folder of your project

The new window is the workspace for your new project. You can save it to a named file with 'File', 'Save Workspace As...'

(The above instructions refer to Visual Studio Code 1.36.1 on macOS.)

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
dcorking
  • 1,146
  • 1
  • 14
  • 24
  • This is the only answer that makes sense to me. It's exactly what has been asked for in the question. I don't really know what the other answers are talking about. I just wanted to know how to create a new workspace, and THIS is the answer. – AllBlooming Jul 07 '22 at 04:48
6
  1. Close all open files in VS Code.
  2. Select File menu >> Open Folder... and open the folder containing your source files.
  3. Select File Menu >> Save Workspace as... it should default to the folder you previously opened. If not browse to it and save the workspace file.
Geordie
  • 1,920
  • 2
  • 24
  • 34
0

To create a new workspace in Visual Studio Code press F1 (or Crtl+Shift+P) and choose 'AL: GO!'

Ortwin
  • 1
  • 1