skip to Main Content

Is there a way in Microsoft VS Code to create two files for settings.json (different names or different subfolder) and switch between them?

Background: We use the Java Extension in VS Code. Our source code repository contains the settings for a Java project in a Unix environment. In specific cases we want to run the code in Windows environment and need a different settings.json.

2

Answers


  1. I have a nice workaround for this.

    Make two files, settings.json and _settings.json and keep them in the same folder, for example, C:/Users/USER/Project/.vscode.

    Now make a script or program that alternates their name.

    I’ll give an example Java Program.

    package com.example.program;
    
    import java.io.File;
    
    public class SettingsJSON{
    
      public static void main(String... args){
        final String dir = "C:/Users/USER/Project/.vscode/";  // Example path to directory.
    
        File s1 = new File(dir + "settings.json"), s2 = new File(dir + "_settings.json");
    
        s1.renameTo(dir + "temp.json");  // Temporarily renaming the file.
    
        s2.renameTo(dir + "settings.json");
        s1.renameTo(dir + "_settings.json");
      }
    
    }
    

    Now just call the main method and you have the settings.json file switched.
    Since you’re using vscode, you’ll have an option above the main method to Run|Debug it. Click on that to run the program.

    You can even put this class in the same project or a different one if you wish.

    Login or Signup to reply.
  2. Is there a way in Microsoft VS Code to create two files for settings.json (different names or different subfolder) and switch between them?

    Not by default- no.

    You can wait for this feature-request gets implemented (though you might be waiting a while): Allow to scope settings by platform
    #5595
    . You can show your support for the issue ticket by giving a thumbs up reaction to the issue. But please don’t make a "me too" comment. "me too" comments generally come off as annoying to repo maintainers because they clutter up discussion and don’t contribute anything of significant value.

    In the meantime, you could create a script to do so with your technology of choice and write a VS Code task to run the script, but that sounds like a footgun for accidental thrashy git changes. I’d instead suggest writing two template files named something like .vscode/settings.unix.json and .vscode/settings.windows.json and committing those to version control, but putting .vscode/settings.json in the gitignore.

    Certain other settings already have mechanisms to allow you have different values for different platforms: terminal profile definitions, and keybindings. But those are more user settings and not workspace settings.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search