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

标签 git macos visual-studio-code

我正在尝试将我的 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"

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

但是如果我输入 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.

所以我已经成功了,因为 git 正在尝试打开 Visual Studio Code 以供我编辑我的提交消息,但它随后失败了。

我应该在 ~/.bash_profile 文件中的 export VISUAL=X 行中使用什么 X 以使 git 能够打开 Visual Studio提交消息的代码?

(注意 How to use Visual Studio Code as Default Editor for Git 不是重复项,因为 Gary 使用的是 Windows PC。)

最佳答案

export EDITOR="code -w" 添加到您的 shell 的配置文件或 rc 文件中

(例如,您的 bash 配置文件可以通过 open ~/.bash_profile 访问)

这要求您的路径中已经有 code 二进制文件。

如果你没有,或者不知道你是否有,只需进入 vscode,输入 CMD + SHIFT + P,输入 code 然后点击Shell 命令:在 PATH 中安装“code”命令

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

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

关于git - 将 Visual Studio Code 设置为我的 Mac 的默认编辑器(包括编辑 Git 提交),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33315856/

相关文章:

html - 不接受 VS 代码背景图片

visual-studio-code - 安装在 Program Files 中的 Windows VS Code(用户)

git - 批量更改各种存储库中的git remote origin - bash命令

objective-c - 在 Objective-C 中使用 NSMenu 切换 NSStatusItem

c - lsof 总是报告偏移量等于 OSX 中的文件大小

macos - 为 bitbucket 帐户设置 ssh key (使用 mac osx)

Python 脚本在用 VS 代码打开时找不到文件,但在终端上工作正常

git - git push 和 git push -f 的区别

git - Netlify 环境变量中的 JSON

git - 如何在 git-commit 上启用 vim 中的拼写检查