bash 如何设置链接以从 OSX 上的终端打开 Visual Studio Code?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30159158/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 12:58:24  来源:igfitidea点击:

How do I set up a link to open up Visual Studio Code from terminal on OSX?

macosbashterminalvisual-studio-code

提问by Matthew Harwood

I've recently installed Visual Studio Codeand I love it! In the past, I've used sublime text 2/3 and at some point I copied code to allow subl .command to open the current directory with sublime.

我最近安装了Visual Studio Code,我喜欢它!过去,我使用了 sublime text 2/3,并且在某些时候我复制了代码以允许subl .命令使用 sublime 打开当前目录。

Question:

题:

  • How can I write a similar code to allow visual studio code to open up the current directory like I've done in the past with sublime.
  • Where should I put paste this code?
  • 如何编写类似的代码以允许 Visual Studio 代码打开当前目录,就像我过去使用 sublime 所做的那样。
  • 我应该在哪里粘贴这段代码?

Desired alias/link/command would be code .

所需的别名/链接/命令是 code .

Does the command on this video

这个视频上的命令

回答by Benjamin Pasero

From version 1.0 use the command Install 'Code' command in pathfrom the command palette (View | Command Palette) to make Code available to the command line.

从 1.0 版开始,使用Install 'Code' command in path命令面板 ( View | Command Palette) 中的命令使代码可用于命令行。

Historical answer:

历史答案:

With VSCode 0.3.0 the startup script should now be configured to be this:

使用 VSCode 0.3.0,启动脚本现在应该配置为:

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

UPDATE:If this doesnt work for you uninstall VSC and reinstall it; for this will only work with 0.3.0+

更新:如果这对您不起作用,请卸载 VSC 并重新安装;因为这只适用于 0.3.0+

回答by suryakrupa

Visual Code has a self-service means to do the same!

Visual Code 有一个自助服务方式来做同样的事情!

Followed https://code.visualstudio.com/docs/setup/mac#_installation

按照https://code.visualstudio.com/docs/setup/mac#_installation

Enable <code>code</code>to open visual-code on mac

启用<code>code</code>在mac上打开visual-code

回答by baf

Update

更新

As pointed out in comments by Tony, Atom Shellhas been renamed to Electron. My updated code should read:

正如托尼在评论中指出的那样,Atom Shell已重命名为Electron. 我更新的代码应该是:

code() { (/Applications/Visual\ Studio\ Code.app/Contents/MacOS/Electron "" &) }

Also, I recommend using official way, as described in Benjamin's answer:

另外,我建议使用官方方式,如本杰明的回答中所述

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


You should create a command to start the editor in ~/.bash_profilefile. This file is read each time you open terminal and start your bash session.

您应该创建一个命令来启动~/.bash_profile文件中的编辑器。每次打开终端并启动 bash 会话时都会读取此文件。

As the process should be started in the background and we want to pass a directory or a file as an argument I would add such single line function to .bash profile:

由于该进程应该在后台启动,并且我们希望将目录或文件作为参数传递,我会将这样的单行函数添加到.bash profile

code() { (/Applications/Visual\ Studio\ Code.app/Contents/MacOS/Atom "" &) }

Where /Applications/Visual\ Studio\ Code.appis a path to your Visual Studio Code app. You may need to adjust it if you installed it somewhere else.

/Applications/Visual\ Studio\ Code.appVisual Studio Code 应用程序的路径在哪里。如果您将其安装在其他地方,则可能需要对其进行调整。

This function opens Atom editor that is a base of Visual Studio Code and passes the first parameter to it with $1expansion. Ampersand &will make the process detach from terminal and run in the background. The whole function body is put in brackets ()to quiet messages about detaching and ending the process.

此函数打开作为 Visual Studio Code 基础的 Atom 编辑器,并通过$1扩展将第一个参数传递给它。& 符号&将使进程与终端分离并在后台运行。整个函数体都放在括号中,()以隐藏有关分离和结束进程的消息。

If the .bash_profilefile is missing on your system you will have to create it first.

如果.bash_profile您的系统上缺少该文件,您必须先创建它。

After editing the file you will have to restart your current bash session.

编辑文件后,您必须重新启动当前的 bash 会话。

回答by Michael Welch

I set it up following the docs here: https://code.visualstudio.com/Docs/setupand added a codedefinition to my .bash_profile.

我按照此处的文档进行设置:https: //code.visualstudio.com/Docs/setupcode在我的.bash_profile.

UPDATE (6/10/2015): This answer originally contained the code from the linked site. I have now removed the code from this answer as the linked site now recommends a different codeimplementation. (as Benjamin pointed out in the comments). Please see the link for the recommended code to add to your .bash_profile.

更新 (6/10/2015):这个答案最初包含来自链接站点的代码。我现在已经从这个答案中删除了代码,因为链接的站点现在推荐了一个不同的code实现。(正如本杰明在评论中指出的那样)。请查看推荐代码的链接以添加到您的.bash_profile.