将 Visual Studio Code 设置为 Mac 的默认编辑器(包括编辑 Git 提交)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33315856/
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-19 11:28:23  来源:igfitidea点击:

Setting Visual Studio Code as my Mac's default editor (including editing Git commits)

gitmacosvisual-studio-code

提问by dumbledad

I am trying to configure my Mac Book Pro (OSX El Capitan 10.11.1) to use Visual Studio Code as its default editor. I have created a ~/.bash_profilefile with the following two lines

我正在尝试将我的 Mac Book Pro(OSX El Capitan 10.11.1)配置为使用 Visual Studio Code 作为其默认编辑器。我~/.bash_profile用以下两行创建了一个文件

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

This works for some things: I can type vscode test.txtat the bash terminal and up pops test.txtin Visual Studio Code, and if I run the command envI see VISUAL=open -n -b com.microsoft.VSCodein the list. I can even just type $VISUALand Visual Studio Code opens on a new empty file.

这适用于某些事情:我可以vscode test.txt在 bash 终端输入并test.txt在 Visual Studio Code 中弹出,如果我运行命令,env我会VISUAL=open -n -b com.microsoft.VSCode在列表中看到。我什$VISUAL至可以直接输入,Visual Studio Code 在一个新的空文件上打开。

But if I type git commitI get the following error

但是如果我输入git commit我会收到以下错误

error: cannot run com.microsoft.vscode: No such file or directory
error: unable to start editor 'com.microsoft.vscode' Please supply the
message using either -m or -F option.

错误:无法运行 com.microsoft.vscode:没有此类文件或目录
错误:无法启动编辑器“com.microsoft.vscode” 请
使用 -m 或 -F 选项提供消息。

So I have succeeded inasmuch as git is trying to open Visual Studio Code for me to edit my commit message but it is then failing.

所以我已经成功了,因为 git 试图打开 Visual Studio Code 让我编辑我的提交消息,但它失败了。

What Xshould I use in the line export VISUAL=Xin my ~/.bash_profilefile to enable git to open Visual Studio Code for commit messages?

X应该export VISUAL=X~/.bash_profile文件中的行中使用什么来启用 git 以打开 Visual Studio Code 以获取提交消息?

(N.B. How to use Visual Studio Code as Default Editor for Gitis not a duplicate since Gary is on a Windows PC.)

(注意如何使用 Visual Studio Code 作为 Git 的默认编辑器不是重复的,因为 Gary 在 Windows PC 上。)

采纳答案by Martin Andersen

It's working with the latest version 0.10.9 of VS Code

它适用于最新版本 0.10.9 的 VS Code

[core]
editor = '/Applications/Visual Studio Code.app/Contents/MacOS/Electron' -w

Test it with: git config --global --edit. remember to refresh the terminal after you have changed the config file.

使用以下命令进行测试:git config --global --edit。记住在更改配置文件后刷新终端。

回答by Govind Rai

Add export EDITOR="code -w"to your bash profile

添加export EDITOR="code -w"到您的 bash 个人资料

(Your bash profile is accessible via open ~/.bash_profile)

(您的 bash 个人资料可通过 访问open ~/.bash_profile

This requires you to have the codebinary already in your path. If you don't have that, or don't know if you do, simply go into vscode, enter CMD + SHIFT + P, type codeand click Shell Command: Install 'code' command in PATH. Then do the first step.

这要求您code的路径中已经有二进制文件。如果你没有,或者不知道你是否有,只需进入 vscode,输入CMD + SHIFT + P,输入代码,然后单击Shell 命令:在 PATH 中安装“代码”命令。然后做第一步。

Also, very helpful comment by pompalinibelow,

此外,下面pompalini 的非常有用的评论,

remember to "refresh" terminal by closing and opening it again or resourcing you bash profile by running source ~/.bash_profile, only then it will the new changes in your bash_profile apply in your terminal.

请记住通过关闭并再次打开来“刷新”终端或通过运行为您的 bash 配置文件提供资源source ~/.bash_profile,只有这样您的 bash_profile 中的新更改才会应用到您的终端中。

回答by Benjamin Pasero

Currently VSCode can not be used as git editor, sorry. We have this as a story on our backlog.

目前 VSCode 不能用作 git 编辑器,抱歉。我们在积压工作中有这个故事。

Update for our VS Code 1.0 release:

我们 VS Code 1.0 版本的更新:

This is now possible! All you need to do is to configure Code as the git editor using the newly introduced --waitoption from the command line.

这是可能的!您需要做的就是使用--wait命令行中新引入的选项将 Code 配置为 git 编辑器。

回答by Den

  1. In terminal

    • Type: open ~/.bash_profile

    • Insert: export EDITOR="code -w"

  2. In visual studio code

    • Press: CMD + SHIFT + P
    • Insert: install codeand select from autocomplete menu shell command: Install 'code' in command PATH
  1. 在终端

    • 类型: open ~/.bash_profile

    • 插入: export EDITOR="code -w"

  2. 在 Visual Studio 代码中

    • 按: CMD + SHIFT + P
    • 插入:install code并从自动完成菜单中选择shell command: Install 'code' in command PATH

回答by Americo Savinon

1) Just add this to your ~/.bash_profile or ~/.zshrc:

1) 只需将此添加到您的~/.bash_profile 或 ~/.zshrc 中:

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

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

2) Then either open a new terminal or run: source ~/.bash_profileor source ~/.zshrc

2)然后打开一个新终端或运行:source ~/.bash_profilesource ~/.zshrc

After this steps, you will be able to do code .to open VS Code on any path

完成此步骤后,您将能够code .在任何路径上打开 VS Code