使用适用于 Linux 的 Windows 子系统的 Visual Studio Code 入门Get started using Visual Studio Code with Windows Subsystem for Linux

Visual Studio Code 与远程 WSL 扩展一起使用,你可以直接从 VS Code 使用 WSL 作为你的全时开发环境。Visual Studio Code, along with the Remote - WSL extension, enables you to use WSL as your full-time development environment directly from VS Code. 您可以:You can:

  • 在基于 Linux 的环境中进行开发develop in a Linux-based environment
  • 使用特定于 Linux 的工具链和实用工具use Linux-specific toolchains and utilities
  • 从 Windows 轻松运行和调试基于 Linux 的应用程序,同时保持对高效率工具(如 Outlook 和 Office)的访问run and debug your Linux-based applications from the comfort of Windows while maintaining access to productivity tools like Outlook and Office
  • 使用 VS Code 内置终端运行所选的 Linux 分发版use the VS Code built-in terminal to run your Linux distribution of choice
  • 利用Intellisense 代码完成linting调试支持代码片段单元测试等 VS Code 功能take advantage of VS Code features like Intellisense code completion, linting, debug support, code snippets, and unit testing
  • 通过 VS Code 的内置Git 支持轻松管理版本控制easily manage your version control with VS Code's built-in Git support
  • 直接在 WSL 项目上运行命令和 VS Code 扩展run commands and VS Code extensions directly on your WSL projects
  • 编辑 Linux 中的文件或已装载的 Windows filesystem (例如/mnt/c) ,无需担心路径问题、二进制兼容性或其他跨操作系统挑战edit files in your Linux or mounted Windows filesystem (for example /mnt/c) without worrying about pathing issues, binary compatibility, or other cross-OS challenges

安装 VS Code 和远程 WSL 扩展Install VS Code and the Remote WSL extension

  • 访问 VS Code 安装 "页 ,然后选择32或64位安装程序。Visit the VS Code install page and select the 32 or 64 bit installer. 不在 WSL 文件系统) 上安装 Windows (上的 Visual Studio Code。Install Visual Studio Code on Windows (not in your WSL file system).

  • 当系统提示您在安装过程中 选择其他任务 时,请务必选中 " 添加到路径 " 选项,以便您可以使用代码命令轻松打开 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.

  • 安装 远程开发扩展包Install the Remote Development extension pack. 除了远程 SSH 和远程容器扩展外,此扩展包还包括远程 WSL 扩展,使你能够打开容器、远程计算机或 WSL 中的任何文件夹。This extension pack includes the Remote - WSL extension, in addition to the Remote - SSH, and Remote - Containers extensions, enabling you to open any folder in a container, on a remote machine, or in WSL.

重要

若要安装 WSL 扩展,将需要 1.35 的发行 版本或更高版本 VS Code。In order to install the Remote-WSL extension, you will need the 1.35 May release version or later of VS Code. 建议不要在不使用 WSL 扩展的 VS Code 中使用 WSL,因为将失去对自动完成、调试、linting 等的支持。有趣的事实:此 WSL 扩展安装在 $HOME/.vscode/extensions 中 (在 ls $HOME\.vscode\extensions\ PowerShell) 中输入命令。We do not recommend using WSL in VS Code without the Remote-WSL extension as you will lose support for auto-complete, debugging, linting, etc. Fun fact: this WSL extension is installed in $HOME/.vscode/extensions (enter the command ls $HOME\.vscode\extensions\ in PowerShell).

更新 Linux 分发版Update your Linux distribution

某些 WSL Linux 发行版缺少 VS Code 服务器启动所需的库。Some WSL Linux distributions are lacking libraries that are required by the VS Code server to start up. 可以通过使用包管理器将其他库添加到 Linux 分发中。You can add additional libraries into your Linux distribution by using its package manager.

例如,若要更新 Debian 或 Ubuntu,请使用:For example, to update Debian or Ubuntu, use:

sudo apt-get update

若要添加 wget (从 web 服务器检索内容) 和 ca 证书 (允许基于 SSL 的应用程序检查 SSL 连接的真实性) ,请输入: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:

sudo apt-get install wget ca-certificates

在 Visual Studio Code 中打开 WSL 项目Open a WSL project in Visual Studio Code

从命令行From the command-line

若要从 WSL 分发打开项目,请打开分发的命令行并输入以下命令: code .To open a project from your WSL distribution, open the distribution's command line and enter: code .

VS Code 远程服务器打开 WSL 项目

From VS CodeFrom VS Code

你还可以使用快捷方式访问更多 VS Code 远程选项: CTRL+SHIFT+P 在 VS Code 中打开命令面板。You can also access more VS Code Remote options by using the shortcut: CTRL+SHIFT+P in VS Code to bring up the command palette. 如果随后键入,您 VSCODE-REMOTE 将看到所有可用的 VS Code 远程选项,允许您在远程会话中重新打开该文件夹,指定要在其中打开的分发,等等。If you then type VSCODE-REMOTE you will see all of the VS Code Remote options available, allowing you to reopen the folder in a remote session, specify which distribution you want to open in, and more.

VS Code 的命令面板

VS Code 远程中的扩展Extensions inside of VS Code Remote

远程 WSL 扩展将 VS Code 拆分为 "客户端-服务器" 体系结构,客户端 (在 Windows 计算机上运行的用户界面,以及在远程运行 (代码、Git、插件等) 的服务器上运行的用户) 界面。The Remote-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.

VS Code 远程运行时,选择 "扩展" 选项卡将显示本地计算机与 WSL 分发之间剥离的扩展的列表。When running VS Code Remote, selecting the 'Extensions' tab will display a list of extensions split between your local machine and your WSL distribution.

安装本地扩展(如 主题)只需安装一次。Installing a local extension, like a theme, only needs to be installed once.

某些扩展(如 Python 扩展 )或处理 linting 或调试等内容的任何内容必须在每个远程 WSL 分发上单独安装。Some extensions, like the Python extension or anything that handles things like linting or debugging, must be installed separately on each remote WSL distributions. ⚠如果本地安装的扩展未安装在 WSL Remote 上,VS Code 将显示一个警告图标以及绿色的 "安装在 WSL" 按钮。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 Remote.

VS Code 与 WSL extension vs local extension

有关详细信息,请参阅 VS Code 文档:For further information, see the VS Code docs:

  • 在 WSL 中启动 VS Code Remote 时,将不运行任何 shell 启动脚本。When VS Code Remote is started in WSL, no shell startup scripts are run. 有关如何运行其他命令或修改环境的详细信息,请参阅此 高级环境安装脚本一文See this advanced environment setup script article for more info on how to run additional commands or modify the environment.

  • 从 WSL 命令行启动 VS Code 时遇到问题?Having problems launching VS Code from your WSL command line? 故障排除指南 包含有关更改路径变量、解决有关缺失依赖项的扩展错误、解决 Git 行结束问题、在远程计算机上安装本地 VSIX、启动浏览器窗口、阻止 localhost 端口、web socket 不工作、存储扩展数据错误等的提示。This troubleshooting guide includes tips on changing path variables, resolving extension errors about missing dependencies, resolving Git line ending issues, installing a local VSIX on a remote machine, launching a browser window, blocker localhost port, web sockets not working, errors storing extension data, and more.

安装 Git(可选)Install Git (optional)

如果计划与其他人协作,或是在开放源代码站点(如 GitHub)上托管项目,则 VS Code 支持使用 Git 进行版本控制If you plan to collaborate with others, or host your project on an open-source site (like GitHub), VS Code supports version control with Git. VS Code 中的“源代码管理”选项卡可跟踪所有更改,并直接在 UI 中内置了常见 Git 命令(add、commit、push、pull)。The Source Control tab in VS Code tracks all of your changes and has common Git commands (add, commit, push, pull) built right into the UI.

若要安装 Git,请参阅 设置 git 以使用适用于 Linux 的 Windows 子系统To install Git, see set up Git to work with Windows Subsystem for Linux.

安装 Windows 终端(可选)Install Windows Terminal (optional)

新的 Windows 终端启用多个选项卡 (在命令提示符、PowerShell 或多个 Linux 分发) 之间快速切换,自定义键绑定 (为打开或关闭选项卡、复制 + 粘贴等 ) 、表情符号☺和自定义主题 (颜色方案、字体样式和大小、背景图像/模糊/透明度) 。The new Windows Terminal enables multiple tabs (quickly switch between Command Prompt, PowerShell, or multiple Linux distributions), custom key bindings (create your own shortcut keys for opening or closing tabs, copy+paste, etc.), emojis ☺, and custom themes (color schemes, font styles and sizes, background image/blur/transparency). Windows 终端文档中了解详细信息。Learn more in the Windows Terminal docs.

  1. 在 Microsoft Store 中获取 Windows 终端:通过 Microsoft Store 进行安装时,将自动处理更新。Get Windows Terminal in the Microsoft Store: By installing via the store, updates are handled automatically.

  2. 安装完成后,打开 Windows 终端并选择“设置”以使用 profile.json 文件自定义终端。Once installed, open Windows Terminal and select Settings to customize your terminal using the profile.json file.

其他资源Additional Resources

可能需要考虑的几个附加扩展包括:A few additional extensions you may want to consider include:

  • 来自其他编辑器的键映射:如果是从另一个文本编辑器(如 Atom、Sublime、Vim、eMacs、Notepad++ 等)进行转换,则这些扩展可帮助你的环境对此进行适应。Keymaps from other editors: These extensions can help your environment feel right at home if you're transitioning from another text editor (like Atom, Sublime, Vim, eMacs, Notepad++, etc).
  • 设置同步:可以使用 GitHub 在不同安装之间同步 VS Code 设置。Settings Sync: Enables you to synchronize your VS Code settings across different installations using GitHub. 如果在不同的计算机上工作,这有助于在它们之间保持一致的环境。If you work on different machines, this helps keep your environment consistent across them.
  • 适用于 Chrome 的调试器:在服务器端通过 Linux 进行开发后,需要开发并测试客户端。Debugger for Chrome: Once you finish developing on the server side with Linux, you'll need to develop and test the client side. 此扩展将 VS Code 编辑器与 Chrome 浏览器调试服务进行集成,以提高工作效率。This extension integrates your VS Code editor with your Chrome browser debugging service, making things a bit more efficient.