如何安装和使用 Visual Studio Code (VS Code) 命令行界面

介绍

Visual Studio Code是一个免费的,开源和跨平台的文本编辑器,主要由微软开发,它使用Web技术,如JavaScript和CSS,这有助于促进一个大型生态系统的社区创建的插件,以扩展其功能到许多不同的编程语言和功能。

在本教程中,您将安装Visual Studio Code命令行界面,并学习如何使用它来打开文件和目录,比较文件之间的更改,并安装扩展。

前提条件

要完成本教程,您需要安装 Visual Studio Code. 请参阅 官方 Setting up Visual Studio Code 文档,以了解如何为您的平台安装 Code。

安装 Visual Studio 代码命令行接口

您可能需要在使用它之前安装 Visual Studio Code 命令行界面。 要做到这一点,首先启动正常的 Visual Studio Code 图形界面。 如果这是您第一次打开应用程序,默认屏幕将在左侧有一个图标栏,以及默认的欢迎卡:

A screenshot of the default "Get Started" screen in the Visual Studio Code interface, including an icon toolbar along the left-hand side, a few suggested activities to get started such as "New File..." and "Open...", and some highlighted instructional walkthroughs

Visual Studio Code 提供了一个内置的命令来安装它的命令行界面。 在 Mac 上键入Command+Shift+P,在 Windows 和 Linux 上键入Control+Shift+P:

A screenshot of the Visual Studio Code interface with the Command Palette activated, waiting for input to be entered after its '>' prompt

这将打开一个提示在你的代码窗口的顶部。键入shell 命令到提示.它应该自动完成到正确的命令,该命令将读到Shell 命令:在 PATH 中安装代码命令:

A screenshot of the Visual Studio Code interface, with the Command Palette activated and the "Install 'code' command in PATH" command highlighted

按「ENTER」來執行突出的命令. 您可能會被要求輸入您的管理員認證來完成安裝過程。

您现在已经安装了代码命令行命令。

通过运行代码--版本旗帜来验证安装是否成功:

1code --version
1[secondary_label Output]
21.62.1
3f4af3cbf5a99787542e2a30fe1fd37cd644cc31f
4x64

如果您的输出包含版本字符串,则您已成功安装了 Visual Studio Code 命令行接口。

使用代码命令打开文件

运行一个或多个文件名中的代码命令将打开这些文件在Visual Studio Code GUI中:

1code file1

这将打开代码中的file1文件。

1code *.md

这将打开代码中的当前目录中的所有标记文件(.md)。

默认情况下,文件将在现有代码窗口中打开,如果有一个是可用的,则使用新窗口旗帜强迫Visual Studio Code为指定的文件打开新窗口。

使用代码命令打开目录

使用代码命令,然后是一个或多个目录名称,在新的 Visual Studio 代码窗口中打开目录:

1code directory1 directory2

代码将为目录打开一个新的窗口. 使用--重复使用窗口旗帜告诉代码重新使用现有的前端窗口。

使用代码命令打开一个.code-workspace工作空间文件

使用代码命令打开工作区文件类似于打开目录:

1code example.code-workspace

这将在新窗口中打开示例工作区,除非您通过添加--重复使用窗口标志来重新使用现有窗口。

使用代码命令安装扩展

您也可以使用代码命令行工具安装 Visual Studio Code 扩展程序. 要做到这一点,您首先需要知道扩展程序的 unique identifier

例如,这里是Jupyter笔记本扩展的页面:

https://marketplace.visualstudio.com/items?itemName=ms-toolsai.jupyter

注意地址中的 itemName 参数. 该参数的值, `ms-toolsai.jupyter,是该扩展的唯一标识符。

您也可以在 Marketplace 页面上找到这些信息,在更多信息部分右侧列的底部:

A screenshot of the Jupyter extension's page on the Visual Studio Marketplace, highlighting the 'Unique Identifier ms-toosai.jupyter' unique id information in the page's right-hand column

一旦你有这个独特的ID,你可以用它与代码 --install-extension来安装扩展:

1code --install-extension ms-toolsai.jupyter
1[secondary_label Output]
2Installing extension 'ms-toolsai.jupyter'...
3Extension 'ms-toolsai.jupyter' v2021.11.1001489384 was successfully installed.

使用相同的ID与--uninstall-extension旗帜来卸载该扩展。

使用代码命令显示两个文件之间的差异

要显示一个标准的分屏 diff 显示两个文件之间的添加、删除和更改,请使用 `--diff' 旗帜:

1code --diff file1 file2

A screenshot of the Visual Studio Code diff interface, with two files side by side, and the second line highlighted, showing a few words have changed between the two versions

类似于打开文件,这将默认地重复使用最前面的窗口,如果有一个,要强迫新窗口打开,请使用新窗口旗帜。

使用代码命令将stdin导入 Visual Studio 代码

大多数命令行壳的一个重要特征是能够将一个命令的输出 pipe (或 send) 输入到下一个命令行。

1ls ~ | code -

这将执行ls命令在~目录上,这是当前用户的家庭目录的捷径。来自ls的输出将是您的家庭目录中的文件和目录的列表。这将发送到代码命令,其中单个-表示它应该读取文本中的管道而不是文件。

代码将输出有关它创建的临时文件的一些信息,以保持输入:

1[secondary_label Output]
2Reading from stdin via: /var/folders/dw/ncv0fr3x0xg7tg0c_cvfynvh0000gn/T/code-stdin-jfa

然后,此文件将在 Code GUI 界面中打开:

A screenshot of Visual Studio Code with a text file open, displaying the text piped in from the ls command. The text is standard directories such as Desktop and Documents, along with file1 and file2 used in the previous section

此命令将继续无限期地等待更多输入。 按CTRL+C,让代码停止收听并返回您的壳。

添加--新窗口旗帜,强迫代码为输入打开新窗口。

结论

在本教程中,您安装了Visual Studio Code的代码命令行工具,并使用它来打开文件和目录,比较文件和安装扩展。

要了解有关代码命令的更多信息,您可以运行其帮助函数:

1code --help

您还可以参阅 官方 Visual Studio 代码命令行文档或查看我们的 VS 代码标签页以获取更多 Visual Studio 代码教程、技术讨论和问答。

Published At
Categories with 技术
Tagged with
comments powered by Disqus