Goodbye to the command prompt in WSL, VSCode and Windows Terminal

I came back to Windows from macOS and used PowerShell Core because it said, "Command prompt, at least PowerShell." By the way, I remembered that there was WSL (Windows Subsystem for Linux). I decided to use WSL entirely for the development environment, so make a note of the procedure.

The development environment here means developing the front end of the web application with Angular and the back end with node.js (and since I am not really familiar with Linux, WSL, distribution and shell I am confident that it is confused with.)

1. Introduction of WSL

Introduce WSL by referring to.

2. Introducing Windows Terminal

Windows Terminal is a terminal client for Windows, and although it is still in the Preview version, I love it because it is easy to use and highly customizable.

When you install WSL, you will also see an additional Profile called "Ubuntu" in Windows Terminal. To have Ubuntu Terminal open by default when you start Windows Terminal, press ctrl +, to open profiles.json and change defaultProfile.

Other,

Is changing. Windows Terminal is fun because you can customize it in various ways by editing this profiles.json. For reference, I will post my settings.

profiles.json

{
    "$schema": "https://aka.ms/terminal-profiles-schema",

    "defaultProfile": "{2c4de342-38b7-51cf-b940-2309a097f518}",

    "profiles":
    [
<Omitted>
        {
            "guid": "{2c4de342-38b7-51cf-b940-2309a097f518}",
            "hidden": false,
            "name": "Ubuntu",
            "colorScheme": "Campbell",
            "cursorShape": "emptyBox",
            "acrylicOpacity": 0.85,
            "useAcrylic": true,
            "cursorColor": "#FFFFFF",
            "fontFace": "Cascadia",
            "fontSize": 12,
            "startingDirectory": "C:\\dev",
            "source": "Windows.Terminal.Wsl"
        }
    ],
    "schemes": [
        {
            "name": "Campbell",
            "foreground": "#F2F2F2",
            "background": "#0C0C0C",
            "colors": [
                "#0C0C0C",
                "#C50F1F",
                "#13A10E",
                "#C19C00",
                "#0037DA",
                "#881798",
                "#3A96DD",
                "#CCCCCC",
                "#767676",
                "#E74856",
                "#16C60C",
                "#F9F1A5",
                "#3B78FF",
                "#B4009E",
                "#61D6D6",
                "#F2F2F2"
            ]
        }
<Omitted>
    ],

    // Add any keybinding overrides to this array.
    // To unbind a default keybinding, set the command to "unbound"
    "keybindings": [
        { "command": "copy", "keys": [ "ctrl+c" ] },
        { "command": "paste", "keys": [ "ctrl+v" ] }
    ]
}

3. Change VScode's Default Terminal to WSL

With the introduction of WSL, "wsl" has also increased in the Terminal of Visual Studio Code, so change it to "wsl" with ctrl + shift + p Terminal: Select default shell.

It seems that you can switch Terminal for each workspace by specifying Terminal in .code-workspace, but I don't recommend it because there are some environments where wsl is not included.

4. Perform VSCode tasks in WSL

Even if I change VScode's Default Terminal to WSL, VScode's tasks seem to be executed on the Windows side, and I want to run them on WSL as well. Unfortunately, to solve this, you need to write "WSL-dependent tasks".

./vscode/tasks.json

{
  "version": "2.0.0",
  "tasks": [
    {
      "label": "ng-serve",
      "type": "shell",
      "isBackground": true,
      "command": "ng serve",
      "problemMatcher": {
        "owner": "custom",
        "pattern": {
            "regexp": "^$"
        },
        "background": {
          "activeOnStart": true,
          "beginsPattern": ".*Angular Live Development Server.*",
          "endsPattern": ".*Compiled successfully.*"
        }
      }
    },
    {
      "label": "ng-serve-wsl",
      "type": "shell",
      "isBackground": true,
      "command": "\"ng serve\"",
      "options": {
        "shell": {
          "executable": "C:\\Windows\\System32\\wsl.exe",
          "args": [ "bash -ic" ]
        }
      },
      "problemMatcher": {
        "owner": "custom",
        "pattern": {
            "regexp": "^$"
        },
        "background": {
          "activeOnStart": true,
          "beginsPattern": ".*Angular Live Development Server.*",
          "endsPattern": ".*Compiled successfully.*"
        }
      }
    }
  ]
}

For example, the tasks.json example above defines a task called ng-serve for debugging Angular, but since it works on Windows, In addition, we also have a task called ng-serve-wsl with settings to run on WSL.

The difference between the WSL version is the information of wsl.exe set in `ʻoptions: {} ``, which is the command prompt.

wsl.exe bash -ic "ng serve"

Is equivalent to executing. The important thing is the bash execution argument -i, otherwise .bashrc will not be read (= PATH is not set) and ng will result in a command not found error. I will end up.

5. Build a development environment on the WSL side

Tools necessary for development such as git, node, npm, angular, python, aws-cli, firebase-cli will be put in WSL. For Ubuntu, it is usually installed with apt-get.

Summary and a little troublesome (redundant) place

I was wondering if I should wait until WSL2, but it was very convenient to use it with WSL1 as well.

Terminal and VSCode, which are often used during development, can now be spent almost without being aware that they are Windows. The scripts etc. are almost the same as macOS, and it is easier to rehearse CI running on Linux.

The interoperability with Windows is higher than I expected, and if I add .exe to the command, I can execute Windows commands even from WSL, so I felt that I could reach the itchy place (from WSL). You should be able to build .NET apps that only work on Windows by hitting msbuild.exe, and of course you can check the operation of .NET programs that run cross-platform by putting .NET Core on the WSL side in the first place. Is possible).

As a bit redundant,

git credentials need to be stored in both Win / WSL

Sometimes I hit the git command in bash, sometimes I use a Git client app (such as GitKraken or VSCode), and I know how to make the credential store common. I don't have it, so I'm memorizing it in both for now.

Converting a Windows path to a UNIX path

In order to "copy the pathname in Windows Explorer and cd in bash", you need a command similar to the following:

cd $(wslpath -u "C:\dev\hoge")

I can convert a Windows path string to a UNIX path with the wslpath function, so I'm biting it and moving the directory.

Most of the other members of the team are Windows

Therefore, if you need to write a script on the assumption that it will work in their environment, you will have to use PowerShell together to check the operation, and eventually you will have to install node on the Windows side as well. Well, this is a team situation. Still, Windows Terminal is easy to use because you can manage both WSL and PowerShell in tabs.

Another solution or favorite

Assuming VSCode, by using Remote --WSL Extension, all project development can be done on WSL. You will be able to do it. The Terminal described above and the task execution are all.

Currently, it is still a Preivew version, so stability is uncertain, WSL seems to be slow (subjective) because it is a remote premise, VS Code extension needs to be installed for remote depending on the thing, etc. I was a little worried, so I tried it a little and put it on hold.

Recommended Posts

Goodbye to the command prompt in WSL, VSCode and Windows Terminal
In the python command python points to python3.8
Bring files in Windows to WSL
Hit the echo command in the Mac terminal to output Hello World
I made a command to display a colorful calendar in the terminal
Twitter streaming client to enjoy in the terminal
How to add Anaconda Powershell Prompt to Windows Terminal?
How to get started with the 2020 Python project (windows wsl and mac standardization)
Use libsixel to output Sixel in Python and output a Matplotlib graph to the terminal.
Jedi-vim shortcut command that allows you to refer to the definition source and definition destination in Python
How to run a Python file at a Windows 10 command prompt
I tried to illustrate the time and time in C language
How to get all the keys and values in the dictionary
Solution to the problem that the display is corrupted when the .exe command is included in the while loop in wsl2
[sh] How to store the command execution result in a variable
Repeat with While. Scripts to Tweet and search from the terminal
Automatically acquire the operation log in the terminal when logging in to Linux
How to give and what the constraints option in scipy.optimize.minimize is
[EC2] How to install chrome and the contents of each command
[EC2] How to install and download chromedriver from the command line
About the procedure for linking Visual Studio Code for Windows and WSL
Until Windows Subsystem for Linux (WSL) is installed in Windows and fish is installed
I want to connect remotely to another computer, and the nautilus command
[Windows + anaconda] Automatically activate the environment when the command prompt is started
How to display bytes in the same way in Java and Python
Reasons and work notes for the tool / environment kitchen to switch the main PC from Linux to Windows + WSL2
Programming to fight in the world ~ 5-1
Programming to fight in the world ~ 5-5,5-6
Programming to fight in the world 5-3
Python 3.6 on Windows ... and to Xamarin.
Programming to fight in the world-Chapter 4
[linux] kill command to kill the process
Cython to try in the shortest
Programming to fight in the world ~ 5-2
Supplement to the explanation of vscode
Put Cabocha 0.68 on Windows and try to analyze the dependency with Python
Replace the directory name and the file name in the directory together with a Linux command.
Create a shortcut to run a Python file in VScode on your terminal
Get, test, and submit test cases on the command line in the AtCoder contest
[Linux] Command to get a list of commands executed in the past
The file name was bad in Python and I was addicted to import
I want to leave an arbitrary command in the command history of Shell
I implemented the VGG16 model in Keras and tried to identify CIFAR10
I tried to control the network bandwidth and delay with the tc command