How do I change the default directory for Windows Terminal for WSL?

2 min read 06-10-2024
How do I change the default directory for Windows Terminal for WSL?


Navigating WSL with Ease: Changing the Default Directory in Windows Terminal

Windows Terminal is a powerful tool for interacting with the Windows Subsystem for Linux (WSL). However, the default starting directory for WSL distributions might not always be the most convenient. This article explains how to customize this setting and make your WSL experience even smoother.

The Problem:

You might be tired of navigating to your desired working directory every time you open a new WSL terminal. It's a repetitive task that can slow down your workflow. Wouldn't it be great to automatically start in a specific location within your WSL environment?

Solution:

The good news is that you can easily customize the default starting directory for your WSL distributions within Windows Terminal. Here's how:

  1. Open Windows Terminal Settings:

    • Right-click the Windows Terminal icon in your taskbar and select "Settings".
    • Alternatively, you can open the "Settings" app, search for "Windows Terminal" and click "Open".
  2. Modify the "profiles" Section:

    • Within the settings file, locate the profiles section. This section defines different terminal profiles for various shells and WSL distributions.
    • Find the specific profile corresponding to your WSL distribution (e.g., Ubuntu-20.04).
  3. Set the "startingDirectory" property:

    • Inside the profile definition, add a new property named "startingDirectory".
    • Set the value to the desired starting directory within your WSL distribution. For example, if you want to start in your home directory, you would use:
    "startingDirectory": "home/"
    
    • If you want to start in a specific project folder, provide the complete path relative to your home directory. For instance:
      "startingDirectory": "home/project-name/"
      
  4. Save Changes and Restart:

    • Save the settings file. You can either click the "Save" icon or use the "Ctrl + S" keyboard shortcut.
    • Close and reopen Windows Terminal for the changes to take effect.

Example:

Here's an example of a profile configuration for Ubuntu-20.04, setting the starting directory to "home/my-projects":

{
  "profiles": {
    "Ubuntu-20.04": {
      "guid": "{3784078e-b839-4c79-8317-44d75e044029}",
      "name": "Ubuntu-20.04",
      "commandline": "wsl -d Ubuntu-20.04",
      "icon": "ms-appx:///Microsoft.WSL.exe",
      "startingDirectory": "home/my-projects/" 
    }
  }
}

Additional Tips:

  • Multiple WSL Distributions: If you use multiple WSL distributions, repeat steps 2 and 3 for each distribution profile.
  • Relative Paths: Use relative paths for "startingDirectory" to avoid issues with different file system structures between Windows and Linux.
  • Explore "profiles" options: Explore other properties within the "profiles" section to customize your terminal experience further, such as font size, colors, and background transparency.

By adjusting the default starting directory in Windows Terminal, you can streamline your WSL workflow and save valuable time. This simple customization empowers you to focus on the tasks that matter most.