ChatGPT解决这个技术问题 Extra ChatGPT

如何从 OSX 的命令行打开 Visual Studio Code?

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

这是仅限 Windows 的东西吗?

键入命令颗粒 (cmd+shift+P):键入 shell 命令以查找 Shell 命令:在 PATH 命令中安装“代码”命令
@CharlieParker 当我输入代码时,我找不到那个 shell 命令。

C
Community

Visual Studio Code Setup page

提示:如果你想通过简单地输入“code”从终端运行 VS Code,VS Code 有一个命令,Shell 命令:在 PATH 中安装“code”命令,将“code”添加到你的 $PATH 变量列表中。安装后,启动 VS Code。现在打开命令面板(F1 或 Mac 上的 ⇧+⌘+P)并输入 shell 命令以找到 Shell 命令:在 PATH 命令中安装“代码”命令。执行命令后,重启终端使新的 $PATH 值生效。您将能够简单地输入“代码”。在任何文件夹中开始编辑该文件夹中的文件。


附带说明一下,vscode 0.10.11 版在 win 10 上没有向我的 PATH 添加任何内容,就像该页面所说的那样。没什么大不了的。
您可以添加选项 -r 以在旧窗口的新选项卡中打开它:code -r <filename>。通常,这就是你想要的。
我正在使用 macOS Sierra 10.12.6。重新启动笔记本电脑后,code 命令无法启动 Visual Studio Code。
当我这样做时,VS Code 会创建一个名为 /usr/local/bin/code 的符号链接,它链接到 /private/var/folders 下的临时目录中的可执行文件。重新启动计算机后,该目录被删除,因此符号链接消失。我使用 sudo ln -s /Applications/Visual\ Studio\ Code.app/Contents/Resources/app/bin/code /usr/local/bin/code 创建了一个更持久的符号链接。假设 /usr/local/bin 在您的 PATH 中,这将在重新启动后继续存在。
code () { VSCODE_CWD="$PWD" open -n -b "com.microsoft.VSCode" --args $* ;} 如果它不起作用,您可以尝试将其添加到 /.zshrc。
b
bjb568

⚡️ 简单的解决方案。

下载、安装并打开 Visual Studio Code。打开命令面板(Mac 上为⌘ + ⇧ + P)或查看❯ 命令面板

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

安装它,你就完成了

📟 这是一个免费的 GIF。

https://i.stack.imgur.com/CZJGA.gif

之后,您可以在终端中使用 codecode .

code

和平! ✌️

如果您想进一步了解一些使用 VSCode CLI 的重要提示/技巧,我在我的工作流程中创建了 a YouTube video


无论出于何种原因 (⌘ + ⇧ + P ) 都不起作用,但 F1 做到了。谢谢!
在 (⇧⌘P) 之后,只是一个示例,使用 View:Toggle Integrated Terminal 命令。
^这不是这里要问的。
(⌘ + ⇧ + P ) 对我不起作用,我必须使用 View -> Command palette 让数据出现在命令调色板搜索中
^您必须在 windows 或 linux 上。使用 Ctrl 而不是 那里。
S
Sudharshan

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

从命令行打开 Visual Studio Code

code --

打开整个文件夹/目录

code .

打开特定文件

code file_name
eg:- code index.html

J
Josh Unger

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

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

我们的 VS Code 1.0 版本更新:

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


有没有办法用 sudo 来完成这项工作,例如:sudo code。我正在使用 VSCode 处理一些 NPM 模块,需要 sudo 覆盖。
@BenjaminPasero 请参阅我对适用于 mingw32 的版本的回答。如果可能,请添加到 VSCode 设置文档。
I
Imran Zahoor

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

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

https://i.stack.imgur.com/YGJWj.png

之后,只需打开终端并输入 code,您的 vscode 就会开始打开。


在版本 11 (2015) 中为我工作
这应该被选为正确答案
谢谢托马斯,这并不重要,因为它可以帮助人们。 Upvote 做得很好:)
D
Daniel Diner

这是我在这个线程中寻找的教程。它展示了通过编写代码在 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


这是唯一对我有用的解决方案。 VS Code 的 CMD+SHIFT+P 没有保持状态。
工作完美!
代码 ~/.zshrc 为我工作谢谢
T
Tracker1

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

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

我将 ~/bin: 添加到我的 $PATH 前,这样我就可以添加一堆一次性脚本,而不会污染我的 ~/.bash_profile 脚本。


在 bash_profile 中可以添加一个函数。 <br> function code { VSCODE_CWD="$PWD" open -n -b "com.microsoft.VSCode" --args $* }
@Py_minion 我特别声明我这样做是为了避免我的 bash_profile 臃肿......此外,这个答案和原始问题来自 4 年前,在应用程序中添加到路径的选项之前。
S
Sabunkar Tejas Sahailesh

注意:仅适用于 Windows 用户。

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

转到控制面板\系统和安全\系统。点击高级系统设置

点击环境变量

在系统变量选项卡下,单击路径变量的编辑

根据您在机器。打开一个新的命令提示符并输入 code-insiders 。打开 vscode-insider 构建/版本


问题是关于 OSX 而不是 windows,我认为这不是一个相关的答案。但是当你有 6+ 时,这意味着它仍然在帮助一些人。
C
Community

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

按 (Command, Up, P) Type > (这将显示并运行命令) 然后输入 Shell Command: Install 'code' command in PATH 命令。然后它应该会出现。单击它后,它将更新,您应该一切顺利!

MacOS X Launch from Command Line docs


p
peterdn

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

#!/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> 现在按预期工作。


如果 Code 已关闭,此脚本将起作用,但如果它已经打开,它只会将其向前推进,不会切换到新目录。有没有办法纠正它?
您可以使用 open -n -a ... 打开一个新实例。
F
Forhad

https://i.stack.imgur.com/Vumsf.png


V
Vương Hữu Thiện

如果您使用的是 VS Code Insiders:

code-insiders .

如果您使用的是 VS 代码:

code .

a
anand bharti

试试这个

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

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

而已。

现在打开您的终端类型。

$代码。


I
Ilan

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

code () { VSCODE_CWD="$PWD" cmd //c code $* ;}

仅供参考:如果还没有,您可能应该为此专门针对 Windows 提出/回答您自己的单独问题。
启动“git bash”(即 mingw32),只需在 bash 命令提示符下键入它并按回车键。现在使用它 cd /c/some/path 然后 code . 如果您希望每次都自动设置它,那么您运行 git bash 然后将其添加到您的 ~/.bash_profile
x
xverges

您可以使用 Visual Studio Code definesvscode: 协议:

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

你也可以使用

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

如果你不喜欢modifying your path


T
Thiago

它非常简单:

从命令行启动

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

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

资源

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


m
mmilutinovic1313

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


K
Kendra

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

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

编辑你的 ~/.zprofile 文件: vim ~/.zprofile 在它自己的行上添加以下代码: code () { VSCODE_CWD="$PWD" open -n -b "com.microsoft.VSCode" --args $* ;} 保存文件: :wq 重新运行更新的文件: source ~/.zprofile。测试运行代码。在 VS Code 中打开您当前的文件夹!


在 Mac OS Monterey 上,还有它的 .zprofile 而不是 .bash_profile。
R
Rafael Rozon

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

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

您可以在 ~/.bash_profile 中添加此别名。


S
Shereef Marzouk

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

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

然后

. ~/.profile

之后我可以做

 vscode

从终端


感谢您的评论,这对我有用。不过,请更正错字。第二个反斜杠后需要有一个空格。
为我工作,我刚刚修改它以获得空间谢谢@matrik
u
user1116928

将此添加到 /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

非常感谢这是它在我的机器上工作的唯一方法。我注意到编辑器会自行打开文件 $CONTENTS/Resources/app/out/cli.js。我删除了对 CLI 的引用,它的行为符合预期。
p
pungggi

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

也许这对某人有用。


B
Bhargav Rao

对于 windows 用户只需输入

>code .

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


B
Blessing

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

从命令行打开 Visual Studio Code

code-insiders --

打开整个文件夹/目录

code-insiders .

打开特定文件

code-insiders file_name

例如:- 代码 index.html


K
Krismu

VSCode 现在支持开箱即用 with version 1.58。只需输入:

$ cd path/to/your/directory
$ code .

D
David A. Gray

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

所以,例如,

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

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

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

注意:附加到 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

M
Mohamed El hadjaoui

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


A
Antonio Henrique

将您当前的文件夹链接到 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 ."

OP 想要一个 OSX 解决方案。
e
edikgat

如果您使用 snap 安装 VS CODE。您需要在 PATH 环境变量中添加 /snap/bin。所以 - 打开您的 .bashrc.zshrc 并在您的 PATH 环境变量中添加 /snap/bin

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


D
Dharman

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

start Code filename.extension

上面的行对我有用。