使用 Visual Studio Code [VS Code] 访问 Windows Subsystem for Linux [WSL]
Visual Studio Code, along with the WSL extension, enables you to use WSL as your full-time development environment directly from VS Code.
1. Install VS Code and the WSL extension
-
Install Visual Studio Code on Windows (not in your WSL file system).
在 Windows 中安装 Visual Studio Code,而不是 WSL 文件系统。 -
When prompted to Select Additional Tasks during installation, be sure to check the Add to PATH option so you can easily open a folder in WSL using the code command.
当系统提示您在安装过程中 Select Additional Tasks 时,请务必选中 Add to PATH 选项,以便您可以使用代码命令轻松打开 WSL 中的文件夹。 -
Install the Remote Development extension pack. This extension pack includes the WSL extension, in addition to the Remote - SSH, and Dev Containers extensions, enabling you to open any folder in a container, on a remote machine, or in WSL.
安装远程开发扩展包。
1.1. Remote Development
Visual Studio Code Remote Development Extension Pack
The Remote Development extension pack allows you to open any folder in a container, on a remote machine, or in the Windows Subsystem for Linux (WSL) and take advantage of VS Code’s full feature set.
This Remote Development extension pack includes four extensions:
-
Remote - SSH - Work with source code in any location by opening folders on a remote machine/VM using SSH. Supports x86_64, ARMv7l (AArch32), and ARMv8l (AArch64) glibc-based Linux, Windows 10/Server (1803+), and macOS 10.14+ (Mojave) SSH hosts.
-
Remote - Tunnels - Work with source code in any location by opening folders on a remote machine/VM using a VS Code Tunnel (rather than SSH).
-
Dev Containers - Work with a separate toolchain or container based application by opening any folder mounted into or inside a container.
-
WSL - Get a Linux-powered development experience from the comfort of Windows by opening any folder in the Windows Subsystem for Linux.
Important
This WSL extension is installed in $HOME/.vscode/extensions
(enter the command ls $HOME\.vscode\extensions\
in PowerShell).
2. Update your Linux distribution
Some WSL Linux distributions are lacking libraries that are required by the VS Code server to start up. You can add additional libraries into your Linux distribution by using its package manager.
某些 WSL Linux 发行版缺少启动 VS Code 服务器所需的库。可以使用其他库的包管理器将其他库添加到 Linux 发行版中。
For example, to update Debian or Ubuntu, use:
sudo apt-get update
To add wget (to retrieve content from web servers) and ca-certificates (to allow SSL-based applications to check for the authenticity of SSL connections), enter:
若要添加 wget (从 Web 服务器检索内容) 和 ca 证书 (允许基于 SSL 的应用程序检查 SSL 连接的真实性),请输入:
sudo apt-get install wget ca-certificates
3. Open a WSL project in Visual Studio Code
3.1. From the command-line
To open a project from your WSL distribution, open the distribution’s command line and enter: code .
若要从 WSL 发行版打开项目,请打开发行版的命令行并输入:code .
3.2. From VS Code
You can also access more VS Code WSL options by using the shortcut: CTRL+SHIFT+P
in VS Code to bring up the command palette. If you then type WSL
you will see a list of the options available, allowing you to reopen the folder in a WSL session, specify which distribution you want to open in, and more.
还可以使用快捷方式访问更多 VS Code WSL 选项:CTRL+SHIFT+P
,在 VS Code 中打开命令面板。如果随后键入 WSL,你将看到可用的选项列表,允许你在 WSL 会话中重新打开文件夹,指定要在其中打开的分发版,等等。
palette ['pælət]:n. 调色板,主要色彩
View -> Command Palette…
WSL: Open Folder in WSL…
WSL: Open Folder in WSL…
4. Extensions inside of VS Code WSL (VS Code WSL 内部的扩展)
The WSL extension splits VS Code into a “client-server” architecture, with the client (the user interface) running on your Windows machine and the server (your code, Git, plugins, etc) running “remotely” in your WSL distribution.
WSL 扩展将 VS Code 拆分为客户端-服务器 (client-server) 体系结构,客户端 (用户界面) 在 Windows 计算机上运行,服务器 (代码、Git、插件等) 在 WSL 发行版中远程运行。
When running the WSL extension, selecting the ‘Extensions’ tab will display a list of extensions split between your local machine and your WSL distribution.
运行 WSL 扩展时,选择 Extensions 选项卡将显示在本地计算机和 WSL 分发版之间拆分的 extensions 列表。
Installing a local extension, like a theme, only needs to be installed once.
安装本地扩展 (如主题) 只需安装一次。
Some extensions, like the Python extension or anything that handles things like linting or debugging, must be installed separately on each WSL distribution. VS Code will display a warning icon ⚠, along with a green “Install in WSL” button, if you have an extension locally installed that is not installed on your WSL distribution.
某些扩展 (like the Python extension or anything that handles things like linting or debugging) 必须单独安装在每个 WSL 分发版上。如果本地安装了未安装在 WSL 分发版上的扩展,VS Code 将显示警告图标 ⚠以及绿色的 Install in WSL 按钮。
References
[1] Yongqiang Cheng, https://yongqiang.blog.csdn.net/
[2] Developing in WSL, https://code.visualstudio.com/docs/remote/wsl
[3] Get started using Visual Studio Code with Windows Subsystem for Linux, https://docs.microsoft.com/en-us/windows/wsl/tutorials/wsl-vscode
[4] Remote Development using SSH, https://code.visualstudio.com/docs/remote/ssh