472

文档提到了一个名为 的可执行文件,code但我不确定在哪里可以找到它,所以我可以把它放在我的路径上。我从 VSCode 站点下载的 zip 不包含任何此类可执行文件。(我可以正常运行.app。)

这是仅限 Windows 的东西吗?

4

30 回答 30

843

Visual Studio 代码设置页面

提示:如果你想通过简单地输入“code”从终端运行 VS Code,VS Code 有一个命令,Shell 命令:在 PATH 中安装“code”命令,将“code”添加到你的 $PATH 变量列表中。

安装后,启动 VS Code。现在打开命令面板(F1 或⇧</kbd>+⌘</kbd>+P on Mac) and type shell command to find the Shell Command: Install 'code' command in PATH command.

执行命令后,重启终端使新的 $PATH 值生效。您将能够简单地输入“代码”。在任何文件夹中开始编辑该文件夹中的文件。

于 2015-04-30T15:12:37.573 回答
350

⚡️ 简单的解决方案。

  1. 下载、安装并打开Visual Studio Code
  2. 打开命令面板 (⌘</kbd> + ⇧</kbd> + P on Mac) OR ViewCommand Palette

3.shell command输入查找 Shell Command: Install 'code' command in PATH command

  1. 安装它,你就完成了

这是一个免费的 GIF。

在命令行上安装代码

之后,您可以在终端中使用code或。code .

code

和平!✌️

如果您想进一步了解一些使用 VSCode CLI 的重要提示/技巧,我在我的工作流程上制作了一个 YouTube 视频。

于 2017-09-21T11:51:11.997 回答
48

terminal如果您想从您的,等打开 Visual Studio Code 上的文件或文件夹iTerm,以下是安装 Visual Studio Code 时默认提供的命令

从命令行打开 Visual Studio Code

code --

打开整个文件夹/目录

code .

打开特定文件

code file_name
eg:- code index.html
于 2017-01-24T06:16:33.420 回答
39

此后,我们将脚本更新为以下语法,以支持多个文件和文件夹作为参数,并修复了无法正确检测当前工作目录的问题:

code () {
    VSCODE_CWD="$PWD" open -n -b "com.microsoft.VSCode" --args $*
}

我们的 VS Code 1.0 版本更新:

请使用命令Install 'Code' command in pathInstall 'code-insiders' command in path从命令选项板 ( View | Command Palette) 使代码可用于命令行。

于 2015-06-03T18:50:43.413 回答
24

对我来说,在 Macbook Book Pro 2019 MacOS 版本10.15.6上,在 VSCode 中打开命令面板的快捷方式是Shift++ 。CommandP

打开它时,只需写下install code并按下enter

在此处输入图像描述

之后只需打开终端并输入code您的 vscode 即可开始打开。

于 2020-08-05T15:13:09.983 回答
17

这是我在这个线程中寻找的教程。它展示了通过编写代码在 Visual Studio Code 中打开文件的方法。

1.- 打开文件

重击

                open ~/.bash_profile 

终端操作系统

                    open ~/.zshrc

2.- 在您的文件中添加:

         code () { VSCODE_CWD="$PWD" open -n -b "com.microsoft.VSCode" --args $* ;}

3.-重新初始化终端并尝试在您要打开的文件夹中

         code .

4.-然后您可以按照此评论中所示使用它:https ://stackoverflow.com/a/41821250/10033560

于 2018-07-04T18:03:56.647 回答
12

我有一个~/bin/code与@BengaminPasero 编写的命令匹配的shell 脚本。

#!/bin/bash
VSCODE_CWD="$PWD" open -n -b "com.microsoft.VSCode" --args $*

~/bin:我给 my添加前缀,$PATH这允许我添加一堆单独的脚本而不会污染我的~/.bash_profile脚本。

于 2015-09-22T17:32:46.063 回答
10

注意:仅适用于 Windows 用户。

正如许多人已经提出了使用code .命令从命令提示符打开代码的方法。这只会打开Visual Studio Code Stable 构建。但是,如果您已经下载了Visual Studio Code Insider构建/版本(具有所有最新的构建/功能,但版本不稳定),那么您需要在 windows 中按照以下说明进行操作:

  • 转到控制面板\系统和安全\系统。点击高级系统设置 在此处输入图像描述
  • 点击环境变量 在此处输入图像描述
  • 在系统变量选项卡下,单击路径变量的编辑 在此处输入图像描述
  • 根据您在机器中安装 vscode Insider 的位置 添加路径C:\Users\tsabu\AppData\Local\Programs\Microsoft VS Code Insiders\bin (或) 。 打开一个的命令提示符并键入以打开 vscode-insider build/versionC:\Program Files\Microsoft VS Code Insiders\bin在此处输入图像描述

    code-insiders .
于 2018-08-31T05:03:30.710 回答
8

打开VSC并按(Command + Up + P)后,我尝试输入“shell命令”,但没有任何反应。为了让“Shell 命令:在 PATH 命令中安装 'code' 命令”出现,您必须执行以下操作:

  1. 按(命令、向上、P)

  2. 类型>(这将显示并运行命令)

  3. 然后键入Shell Command: Install 'code' command in PATH command。然后它应该会出现。

    单击它后,它将更新,您应该一切顺利!

从命令行文档启动 MacOS X

于 2018-07-19T17:14:50.023 回答
7

在 OSX Mavericks 上,我创建了一个名为vscode(改编自.bashrcVSCode Setup中的)的 bash 脚本~/bin

#!/bin/bash

if [[ $# = 0 ]]
then
    open -a "Visual Studio Code"
else
    [[ $1 = /* ]] && F="$1" || F="$PWD/${1#./}"
    open -a "Visual Studio Code" --args "$F"
fi

vscode <file or directory>现在按预期工作。

于 2015-05-02T10:45:48.603 回答
7

如果您在下载文件夹中安装您的 vs 代码,您需要将 VS 代码移动到应用程序文件夹然后打开 vs 代码,然后按shift + command + p您将看到在此处输入图像描述下图。然后你需要输入code .现在你很高兴。

于 2019-12-03T03:16:38.147 回答
6

试试这个

打开 Visual Studio Code 并按 Command + Shift + P 然后在命令面板中键入 Shell 现在您可以找到此选项,例如 Shell Command : Install code in PATH fromSuggested list in command palette。选择该选项。

通过终端/命令提示符打开 VSCode

而已。

现在打开您的终端类型。

$代码。

于 2020-05-16T09:22:20.910 回答
6

如果您使用的是VS Code Insiders

code-insiders .

如果您使用的是VS 代码

code .
于 2020-07-30T15:43:42.547 回答
4

我为 mingw32 发现了一个简洁的解决方法(即对于那些使用 git-scm.com 在 Windows 上安装的 bash 版本的人):

code () { VSCODE_CWD="$PWD" cmd //c code $* ;}
于 2015-08-16T22:18:08.770 回答
4

您可以使用Visual Studio Code 定义vscode:的协议:

open vscode://file/full/path/to/project/or/file

你也可以使用

/Applications/Visual\ Studio\ Code.app/Contents/Resources/app/bin/code

如果你不想修改你的路径

于 2017-11-24T12:23:13.977 回答
4

它非常简单:

从命令行启动

您还可以在将 VS Code 添加到路径后键入“code”,从终端运行 VS Code:

启动 VS 代码。打开命令面板 (⇧⌘P) 并输入“ shell command ”以找到 Shell 命令:在 PATH 命令中安装“code”命令。

资源

https://code.visualstudio.com/docs/setup/mac

于 2018-09-27T07:59:13.270 回答
3

就我而言,我不得不使用别名:

alias code="/<PATH TO VSCODE>/Visual\ Studio\ Code.app/Contents/Resources/app/bin/code"

您可以将此别名添加到您的~/.bash_profile.

于 2021-05-28T22:55:21.837 回答
3

我将此添加到我的~/.profile

alias vscode='/Applications/Visual\ Studio\ Code.app/Contents/MacOS/Electron'

然后

. ~/.profile

之后我可以做

 vscode

从终端

于 2021-10-29T09:52:30.080 回答
2

将此添加到/usr/local/bin/code,如果它们不同,您可能必须修改路径。

#!/usr/bin/env bash

CONTENTS="/Applications/Visual Studio Code.app/Contents"
ELECTRON="$CONTENTS/MacOS/Electron"
CLI="$CONTENTS/Resources/app/out/cli.js"
ATOM_SHELL_INTERNAL_RUN_AS_NODE=1 "$ELECTRON" "$CLI" "$@"
exit $?

之后使可执行文件

sudo chmod +x /usr/local/bin/code
于 2016-07-29T12:45:18.640 回答
2

由于 VS Code Insiders,我遇到了这个问题。路径变量在那里,但我需要将里面的 code-insiders.cmd 重命名为 code.cmd 。

也许这对某人有用。

于 2016-07-31T08:43:02.803 回答
2

我跑了:open -a "Visual Studio Code" [folder-name]用我的 Visual Studio Code 应用程序打开一个文件夹。如果您只想打开应用程序,文件夹名称是可选的。不确定这是否正是您的用例,但希望这会有所帮助!

于 2017-03-22T16:09:11.073 回答
2

对于 windows 用户只需输入

>code .

更多命令在这里 https://code.visualstudio.com/docs/editor/command-line

于 2017-10-18T02:19:13.997 回答
2

这就是在 Mac OS Catalina 上对我有用的方法——在此处找到 (感谢 Josiah!)

如果您使用的是 Mac OS Catalina,则需要编辑 .zprofile 而不是 .bash_profile。

  1. 编辑您的 ~/.zprofile 文件:vim ~/.zprofile
  2. 在其中添加以下代码,在它自己的行上:code () { VSCODE_CWD="$PWD" open -n -b "com.microsoft.VSCode" --args $* ;}
  3. 保存文件::wq
  4. 重新运行更新的文件:source ~/.zprofile.
  5. 测试运行会code .在 VS Code 中打开您当前的文件夹!
于 2020-06-08T17:33:11.603 回答
2

如果您正在使用visual code insiders并且想Visual Studio Code insider从终端或任何其他命令行工具打开 s 中的文件或文件夹,那么您可以参考下面默认提供的命令visual studio code insider

从命令行打开 Visual Studio Code

code-insiders --

打开整个文件夹/目录

code-insiders .

打开特定文件

code-insiders file_name

例如:- 代码 index.html

于 2021-02-01T12:01:02.633 回答
1

VS Code Command Line给出的启动路径指令不正确;示例中显示的前导冒号不起作用。但是,使用反斜杠终止的目录名称启动会按预期打开指定的目录。

所以,例如,

代码 C:\Users\DAVE\Documents\Programming\Angular\StringCalculator\src\

在目录中打开 Visual Studio Code 编辑器C:\Users\DAVE\Documents\Programming\Angular\StringCalculator\src

重要提示:终端反斜杠虽然是可选的,但很有用,因为它清楚地表明打算打开一个目录,而不是文件。请记住,文件扩展名是并且一直是可选的。

注意:附加到 PATH 列表的目录就是该\bin目录,shell 命令code启动一个Windows NT 命令脚本

因此,当合并到另一个 shell 脚本中时,如果您希望脚本的其余部分运行,则code必须调用启动。幸运的是,我在第一次测试新的 shell 脚本之前发现了这一点,我正在创建该脚本以在本地 Web 服务器、我的默认 Web 浏览器和 Visual Studio Code 中同时启动一个 Angular 2 项目。

以下是我的 Angular 启动脚本,用于消除对我在其他地方发布但并非严格要求的系统实用程序之一的依赖。


@echo off

转到跳过

=========================================================================

Name:               StartAngularApp.CMD

Synopsis:           Start the Angular 2 application installed in a specified
                     directory.

Arguments:          %1 = OPTIONAL: Name of directory in which to application
                          is installed

Remarks:            If no argument is specified, the application must be in
                    the current working directory.

                    This is a completely generalized Windows NT command
                    script (shell script) that uses the NPM Angular CLI to
                    load an Angular 2 application into a Node development
                    Web server, the default Web browser, and the Visual
                    Studio Code text editor.

Dependencies:       Unless otherwise specified in the command line, the
                    application is created in the current working directory.

                    All of the following shell scripts and programs must be
                    installed in a directory that is on the Windows PATH
                    directory list.

                    1)  ShowTime.CMD

                    2)  WWPause.exe

                    3)  WWSleep.exe

                    4)  npm (the Node Package Manager) and its startup 
                        script, npm.cmd, must be accessible via the Windows
                        PATH environment string. By default, this goes into
                        directory C:\Program Files\nodejs.

                    5)  The Angular 2 startup script, ng.cmd, and the Node
                        Modules library must be installed for global access.
                        By default, these go into directory %AppData%\npm.

Author:             David A. Gray

Created:            Monday, 23 April 2017

-----------------------------------------------------------------------
Revision History
-----------------------------------------------------------------------

Date       By  Synopsis
---------- --- --------------------------------------------------------
2017/04/23 DAG Script created, tested, and deployed.
=======================================================================

:跳过

echo BOJ %~0, version %~t0
echo.
echo -------------------------------------------------------
echo Displaying the current node.js version:
echo -------------------------------------------------------
echo.
node -v
echo.
echo -------------------------------------------------------
echo Displaying the current Node Package Manager version:
echo -------------------------------------------------------
echo.
call npm -v
echo.
echo -------------------------------------------------------
echo Loading Angular starter application %1
echo into a local Web server, the default Web browser, and
echo the Visual Studio Code text editor.
echo -------------------------------------------------------
echo.

if "%1" neq "" (
    echo.
    echo -------------------------------------------------------
    echo Starting the Angular application in directory %1
    echo -------------------------------------------------------
    echo.
    cd "%~1"
    call code %1\src\
) else (
    echo.
    echo -------------------------------------------------------
    echo Starting the Angular application in directory %CD%
    echo -------------------------------------------------------
    echo.
    call code %CD%\src\
)

call ng serve --open

echo.
echo -------------------------------------------------------
echo %~nx0 Done!
echo -------------------------------------------------------
echo.
Pause
于 2017-04-25T04:09:20.857 回答
1

$> open -a "Visual Studio Code" [文件名]

于 2021-06-04T08:54:05.987 回答
0

将您当前的文件夹链接到 vscode。

Windows Registry Editor Version 5.00

; Directory\Background\shell => on empty space

[HKEY_CLASSES_ROOT\Directory\Background\shell\vscode]
"Icon"="C:\\current-folder-vscode\\Code.exe,0"
@="VsCode"

[HKEY_CLASSES_ROOT\Directory\Background\shell\vscode\command]
@="C:\\current-folder-vscode\\Code.exe ."

; Directory\shell => on a folder

[HKEY_CLASSES_ROOT\Directory\shell\vscode]
@="VsCode"
"Icon"="C:\\current-folder-vscode\\Code.exe,0"

[HKEY_CLASSES_ROOT\Directory\shell\vscode\command]
@="C:\\current-folder-vscode\\Code.exe ."
于 2020-04-08T18:29:21.837 回答
0

如果您使用snap. 您需要添加/snap/binPATH 环境变量。所以 - 打开你的.bashrcor.zshrc 并添加/snap/bin你的 PATH 环境变量

重新加载终端,然后code命令将启动它

于 2020-07-01T21:27:09.603 回答
0

对于 Windows,您可以使用命令:

start Code filename.extension

上面的行对我有用。

于 2021-01-02T04:59:57.533 回答
-1

VSCode 现在支持开箱即用的版本 1.58。只需输入:

$ cd path/to/your/directory
$ code .
于 2021-07-19T09:09:37.477 回答