我想通过运行这个命令代码从Mac OSX终端运行/打开Visual Studio代码。我在这里找到了说明:

https://code.visualstudio.com/Docs/setup

显然,我需要在.bashrc文件中包含它,所以我这样做了,但无济于事。

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

我在这里编辑了.bashrc文件:

~ /。bashrc指向/Users/username/.bashrc

我应该编辑哪个.bashrc ?


当前回答

在.bash_profile中插入一个简单的Bash别名如何?

别名代码=“open -a /Applications/Visual\ Studio\ Code.app”

打开当前目录。

代码。

其他回答

我只是想把Benjamin Pasero的答案从他的评论中拉出来,因为这似乎是最好的解决方案。这是在设置Visual Studio代码页面上给出的提示,它说…

如果你想从终端运行VS Code,把下面的代码附加到你的~/。Bash_profile文件(~/.)ZSHRC,以防您使用zsh)。

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

现在,您可以简单地键入代码。在任何文件夹中开始编辑该文件夹中的文件。[或编写test.txt文件来处理test.txt文件]

我只是从Visual Studio code中提供的“code”程序中创建了一个符号链接。app bundle到/usr/local/bin(一个我更喜欢放东西的地方,它已经在我的机器上)。

你可以像这样用ln -s来做一个符号链接:

ln -s / application /Visual\ Studio\ Code。应用程序/内容/资源/ app / bin / /usr/local/bin/code代码

开放Visual Studio代码 用command + Shift + P打开命令面板 在命令面板中输入Shell 从建议列表中选择Shell命令:Install code in PATH 输入代码。MAC终端

不知何故,用拉贾的方法只对我起过一次作用,重启后,它似乎消失了。 为了使它在Mac OS重启时持久,我在~/中添加了这一行。zshrc,因为我使用zsh:

export PATH=/Applications/Visual\ Studio\ Code.app/Contents/资源/app/bin:$PATH 然后

源~ / . zshrc 现在,我可以做

代码。

即使重启了我的Mac电脑。

在Mac OS上永久设置VS代码路径;

只需在终端上使用以下命令打开.bash_profile

open -t .bash_profile

然后将以下路径添加到.bash_profile中

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

保存.bash_profile文件并退出终端。然后重新打开终端,输入代码.打开VS code。