docs 提到了一个名为 code
的可执行文件,但我不确定在哪里可以找到它,所以我可以把它放在我的路径上。我从 VSCode 站点下载的 zip 不包含任何此类可执行文件。 (我能够很好地运行 .app
。)
这是仅限 Windows 的东西吗?
cmd+shift+P
):键入 shell 命令以查找 Shell 命令:在 PATH 命令中安装“代码”命令
从 Visual Studio Code Setup page:
提示:如果你想通过简单地输入“code”从终端运行 VS Code,VS Code 有一个命令,Shell 命令:在 PATH 中安装“code”命令,将“code”添加到你的 $PATH 变量列表中。安装后,启动 VS Code。现在打开命令面板(F1 或 Mac 上的 ⇧+⌘+P)并输入 shell 命令以找到 Shell 命令:在 PATH 命令中安装“代码”命令。执行命令后,重启终端使新的 $PATH 值生效。您将能够简单地输入“代码”。在任何文件夹中开始编辑该文件夹中的文件。
⚡️ 简单的解决方案。
下载、安装并打开 Visual Studio Code。打开命令面板(Mac 上为⌘ + ⇧ + P)或查看❯ 命令面板
🌟 3. 输入shell command 找到Shell Command: Install 'code' command in PATH command
安装它,你就完成了
📟 这是一个免费的 GIF。
https://i.stack.imgur.com/CZJGA.gif
之后,您可以在终端中使用 code
或 code .
。
code
和平! ✌️
如果您想进一步了解一些使用 VSCode CLI 的重要提示/技巧,我在我的工作流程中创建了 a YouTube video。
如果您想从 terminal
、iTerm
等打开 Visual Studio Code 上的文件或文件夹,以下是安装 Visual Studio Code 时默认提供的命令
从命令行打开 Visual Studio Code
code --
打开整个文件夹/目录
code .
打开特定文件
code file_name
eg:- code index.html
此后,我们将脚本更新为以下语法,以支持多个文件和文件夹作为参数,并修复了无法正确检测当前工作目录的问题:
代码 () { VSCODE_CWD="$PWD" open -n -b "com.microsoft.VSCode" --args $* }
我们的 VS Code 1.0 版本更新:
请使用命令面板 (View | Command Palette
) 中的命令 Install 'Code' command in path
或 Install 'code-insiders' command in path
使代码可用于命令行。
sudo code
。我正在使用 VSCode 处理一些 NPM 模块,需要 sudo 覆盖。
对我来说,在 Macbook Book Pro 2019 MacOS 版本 10.15.6 上,在 VSCode 中打开命令面板的快捷方式是 Shift
+ Command
+ P
。
打开它时,只需写 install code
并按 enter
。
https://i.stack.imgur.com/YGJWj.png
之后,只需打开终端并输入 code
,您的 vscode 就会开始打开。
这是我在这个线程中寻找的教程。它展示了通过编写代码在 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
我有一个与@BengaminPasero 编写的命令匹配的 ~/bin/code
shell 脚本。
#!/bin/bash
VSCODE_CWD="$PWD" open -n -b "com.microsoft.VSCode" --args $*
我将 ~/bin:
添加到我的 $PATH
前,这样我就可以添加一堆一次性脚本,而不会污染我的 ~/.bash_profile
脚本。
function code { VSCODE_CWD="$PWD" open -n -b "com.microsoft.VSCode" --args $* }
注意:仅适用于 Windows 用户。
许多人已经建议了使用 code .
命令从命令提示符打开代码的方法。这只会打开 Visual Studio Code Stable 构建。但是,如果您已经下载了 Visual Studio Code Insider 构建/版本(具有所有最新的构建/功能,但版本不稳定),那么您需要在 windows 中按照以下说明进行操作:
转到控制面板\系统和安全\系统。点击高级系统设置
点击环境变量
在系统变量选项卡下,单击路径变量的编辑
根据您在机器。打开一个新的命令提示符并输入 code-insiders 。打开 vscode-insider 构建/版本
打开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
在 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>
现在按预期工作。
https://i.stack.imgur.com/Vumsf.png
如果您使用的是 VS Code Insiders:
code-insiders .
如果您使用的是 VS 代码:
code .
试试这个
打开 Visual Studio Code 并按 Command + Shift + P 然后在命令面板中键入 Shell 现在您可以找到此选项,例如 Shell Command : Install code in PATH fromSuggested list in command palette。选择该选项。
通过终端/命令提示符打开 VSCode
而已。
现在打开您的终端类型。
$代码。
我为 mingw32 发现了一个简洁的解决方法(即对于那些使用 git-scm.com 在 Windows 上安装的 bash 版本的人):
code () { VSCODE_CWD="$PWD" cmd //c code $* ;}
cd /c/some/path
然后 code .
如果您希望每次都自动设置它,那么您运行 git bash 然后将其添加到您的 ~/.bash_profile
您可以使用 Visual Studio Code defines 的 vscode:
协议:
open vscode://file/full/path/to/project/or/file
你也可以使用
/Applications/Visual\ Studio\ Code.app/Contents/Resources/app/bin/code
如果你不喜欢modifying your path
它非常简单:
从命令行启动
您还可以在将 VS Code 添加到路径后键入“code”,从终端运行 VS Code:
启动 VS 代码。打开命令面板(⇧⌘P)并输入“shell命令”以找到Shell命令:在PATH命令中安装“代码”命令。
资源
https://code.visualstudio.com/docs/setup/mac
我运行:open -a "Visual Studio Code" [folder-name]
以使用我的 Visual Studio Code 应用程序打开一个文件夹。如果您只想打开应用程序,文件夹名称是可选的。不确定这是否正是您的用例,但希望这会有所帮助!
这就是在 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 中打开您当前的文件夹!
就我而言,我不得不使用别名:
alias code="/<PATH TO VSCODE>/Visual\ Studio\ Code.app/Contents/Resources/app/bin/code"
您可以在 ~/.bash_profile
中添加此别名。
我将此添加到我的 ~/.profile
alias vscode='/Applications/Visual\ Studio\ Code.app/Contents/MacOS/Electron'
然后
. ~/.profile
之后我可以做
vscode
从终端
将此添加到 /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 的引用,它的行为符合预期。
由于 VS Code Insiders,我遇到了这个问题。路径变量在那里,但我需要将里面的 code-insiders.cmd 重命名为 code.cmd 。
也许这对某人有用。
如果您正在使用 visual code insiders
,并且想从终端或任何其他命令行工具在 Visual Studio Code insider
中打开文件或文件夹,那么您可以参考以下命令,这些命令默认包含在 visual studio code insider
中。
从命令行打开 Visual Studio Code
code-insiders --
打开整个文件夹/目录
code-insiders .
打开特定文件
code-insiders file_name
例如:- 代码 index.html
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
$> open -a "Visual Studio Code" [文件名]
将您当前的文件夹链接到 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 ."
如果您使用 snap
安装 VS CODE。您需要在 PATH 环境变量中添加 /snap/bin
。所以 - 打开您的 .bashrc
或 .zshrc
并在您的 PATH 环境变量中添加 /snap/bin
重新加载终端,然后 code
命令将启动它
对于 Windows,您可以使用命令:
start Code filename.extension
上面的行对我有用。
不定期副业成功案例分享
code -r <filename>
。通常,这就是你想要的。code
命令无法启动 Visual Studio 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
中,这将在重新启动后继续存在。