如何从OSX上的命令行打开Visual Studio代码?

文档提到一个叫做code的可执行文件,但是我不确定在哪里可以find,所以我可以把它放在我的路上。 我从VSCode网站下载的zip文件没有包含任何这样的可执行文件。 (我可以运行.app就好了。)

这是一个Windows的东西?

从Visual Studio代码设置页面 :

提示 :如果你想通过input'code'从terminal运行VS Code,VS Code有一个命令,Shell命令:在PATH中安装'code'命令,将'code'添加到$ PATHvariables列表中。

安装完成后,启动VS Code。 现在打开命令面板(在Mac上是F1或⇧⌘P),然后inputshell commandShell Command: Install 'code' command in PATH命令中findShell Command: Install 'code' command in PATH Install'code Shell Command: Install 'code' command in PATH命令。

执行该命令后,重新启动terminal,使新的$ PATH值生效。 您只需键入“代码”即可 在任何文件夹中开始编辑该文件夹中的文件。

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

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

更新我们的VS 1.0版本:

Install 'Code' command in path View | Command Palette板( View | Command Palette )的Install 'Code' command in path使用命令Install 'Code' command in path使命令行可以使用Code。

如果您想在terminal上打开Visual Studio代码中的文件或文件夹,则下面的iTerm等是默认安装Visual Studio代码时的命令

从命令行打开Visual Studio代码

 code -- 

打开整个文件夹/目录

 code . 

打开一个特定的文件

 code file_name eg:- code index.html 

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

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

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

Easy简单的解决scheme。

  1. 下载,安装并打开Visual Studio Code for Mac。
  2. 打开命令面板(在Mac上为 + + PF1

Type 3.inputshell command查找Shell Command: Install 'code' command in PATH command

  1. 安装它,你就完成了

🎯这是一个免费的GIF。

在命令行上安装代码

干杯!

在OSX Mavericks上,我在~/bin创build了一个名为vscode (从VSCode设置中的.bashrc改编而来)的bash脚本:

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

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

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

把它添加到/ usr / local / bin / code中 ,如果它们不同,你可能不得不修改path。

 #!/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 

我有这个问题,因为VS Code Insiders。 pathvariables是在那里,但我需要重新命名code.cmd里面的code-insiders.cmd。

也许这对某人有用。

我跑: open -a "Visual Studio Code" [folder-name]与我的Visual Studio代码应用程序打开一个文件夹。 如果您只想打开应用程序,文件夹名称是可选的。 不知道这是否正是您的使用情况,但希望这有助于!

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

所以,例如,

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

打开目录C:\Users\DAVE\Documents\Programming\Angular\StringCalculator\src的Visual Studio代码编辑器。

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

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

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

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


 @echo off 

转到SKIPREM

 ========================================================================= 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. ======================================================================= 

:SKIPREM

 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 

对于用户只需键入

 >code . 

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