Ora

How to Add Configuration in VS Code

Published in VS Code Configuration 6 mins read

Adding configuration in VS Code is fundamental to tailoring your development environment and projects to your specific needs. It allows you to customize editor behavior, define how to run and debug applications, and automate common tasks.

VS Code manages configurations primarily through JSON files, making them human-readable and easy to share across teams. You can configure global settings for your user profile, or project-specific settings that live within your workspace.

Understanding Configuration Types

Before diving into how, let's briefly understand the main types of configurations you'll encounter:

Configuration File Purpose Scope
settings.json Customize editor behavior, themes, language-specific rules. User, Workspace, Folder
launch.json Define how to run or debug your application. Workspace/Folder (.vscode folder)
tasks.json Automate repetitive tasks like building, testing, deploying. Workspace/Folder (.vscode folder)

1. General VS Code Settings (settings.json)

settings.json files control almost every aspect of VS Code's user interface and behavior. There are different scopes for settings:

  • User Settings: Applied globally to all VS Code instances you open. These are stored in a location specific to your operating system.
  • Workspace Settings: Applied only to the currently open workspace (folder). These are stored in a .vscode subfolder at the root of your workspace. They override user settings.
  • Folder Settings (Multi-root Workspaces): If you're using a multi-root workspace, each folder can have its own settings.json in its .vscode subfolder. These override workspace and user settings for that specific folder.

How to Add/Modify Settings:

  1. Via the Settings UI:

    • Go to File > Preferences > Settings (Windows/Linux) or Code > Settings > Settings (macOS), or use the keyboard shortcut Ctrl+, (Windows/Linux) / Cmd+, (macOS).
    • Search for the setting you want to change.
    • Modify the value. Changes are saved automatically.
  2. Directly in settings.json (Recommended for advanced users):

    • Open User Settings:
      • From the Command Palette (Ctrl+Shift+P or Cmd+Shift+P), type and select Preferences: Open User Settings (JSON).
    • Open Workspace Settings:
      • From the Command Palette, type and select Preferences: Open Workspace Settings (JSON). This will create a .vscode folder and a settings.json file if they don't already exist.
    • Add your configurations:
      {
          "editor.fontSize": 14,
          "workbench.colorTheme": "Default Dark+",
          "files.autoSave": "afterDelay",
          "[python]": {
              "editor.tabSize": 4
          }
      }
      • Tip: VS Code provides IntelliSense and validation as you type in settings.json, making it easier to discover and correctly apply settings.

2. Run and Debug Configurations (launch.json)

launch.json defines how VS Code should launch your application for debugging or running without debugging. These configurations are specific to your project and are stored in a launch.json file inside the .vscode folder at your project's root.

How to Add/Create Run Configurations:

  1. Using the Run and Debug View:

    • Open the Run and Debug view by clicking the "Run and Debug" icon in the Activity Bar on the side of VS Code (or Ctrl+Shift+D / Cmd+Shift+D).
    • If no launch.json exists, VS Code will prompt you to create one. Click the "create a launch.json file" link or the gear icon.
    • You'll then be asked to select an environment (e.g., Node.js, Python, C++). VS Code will generate a basic launch.json template for you.
  2. Directly from the Command Palette:

    • Open the Command Palette (Ctrl+Shift+P or Cmd+Shift+P).
    • Type and select Debug: Open launch.json. This will either open an existing launch.json or create a new one with a template.
  3. For Specific Project Types (e.g., Fiori):

    • Some VS Code extensions for specific development environments provide specialized commands to help set up run configurations.
    • For instance, if you're working on Fiori projects, you can streamline the process:
      • From the Command Palette (Ctrl+Shift+P or Cmd+Shift+P), execute the Fiori: Open Run Configurations command.
      • Select the project that you want to add a new configuration to from the list, and press Enter to confirm your input. This will help generate or manage appropriate launch.json entries for your Fiori application.

Example launch.json Entry:

{
    "version": "0.2.0",
    "configurations": [
        {
            "type": "node",
            "request": "launch",
            "name": "Launch Program",
            "skipFiles": [
                "<node_internals>/**"
            ],
            "program": "${workspaceFolder}/app.js",
            "runtimeExecutable": "node",
            "env": {
                "NODE_ENV": "development"
            }
        },
        {
            "type": "python",
            "request": "launch",
            "name": "Python: Current File",
            "program": "${file}",
            "console": "integratedTerminal"
        }
    ]
}
  • type: The debugger type (e.g., node, python, chrome).
  • request: Whether to launch the application or attach to an already running process.
  • name: A display name for your configuration in the Debug dropdown.
  • program: The entry point of your application.
  • env: Environment variables for the launched process.

3. Task Configurations (tasks.json)

tasks.json allows you to define and run custom tasks directly from VS Code. This is incredibly useful for automating build processes, running tests, deploying code, or executing any command-line tool. Like launch.json, it's stored in the .vscode folder of your project.

How to Add/Create Task Configurations:

  1. From the Terminal Menu:

    • Go to Terminal > Configure Tasks....
    • VS Code will prompt you to select a task runner (e.g., npm, gulp, grunt, or "Others" for custom commands).
    • Choose an option, and VS Code will generate a tasks.json file with a template.
  2. Directly from the Command Palette:

    • Open the Command Palette (Ctrl+Shift+P or Cmd+Shift+P).
    • Type and select Tasks: Configure Task. Follow the same prompts as above.

Example tasks.json Entry:

{
    "version": "2.0.0",
    "tasks": [
        {
            "label": "build",
            "type": "shell",
            "command": "npm run build",
            "group": {
                "kind": "build",
                "isDefault": true
            },
            "presentation": {
                "reveal": "always",
                "panel": "new"
            },
            "problemMatcher": "$tsc"
        },
        {
            "label": "lint",
            "type": "shell",
            "command": "eslint .",
            "group": "test",
            "problemMatcher": "$eslint-stylish"
        }
    ]
}
  • label: A display name for the task.
  • type: The task runner (e.g., shell for command-line, npm, gulp).
  • command: The actual command to execute.
  • group: Assigns the task to a group (e.g., build, test) making it easier to run default tasks.

Best Practices for Configuration

  • Scope Appropriately: Use user settings for personal preferences and workspace settings for project-specific configurations.
  • Version Control: Commit your .vscode/settings.json, .vscode/launch.json, and .vscode/tasks.json files to version control (e.g., Git) so your team shares the same development environment settings.
  • Use Variables: Leverage VS Code variables like ${workspaceFolder}, ${file}, or ${env:PATH} for more flexible configurations.
  • Comment Your JSON: Although JSON doesn't officially support comments, VS Code's schema validation for configuration files often allows for C-style // comments, making your configurations easier to understand.

By mastering these configuration methods, you can significantly enhance your productivity and streamline your workflow in VS Code.