首页 > 解决方案 > 疑难解答:如何让 Git 在提交时使用 VS Code?

问题描述

我最近安装了 Oh My Zsh 和 VS 代码。每当我输入git commit时,它都不会打开代码,而是显示以下错误消息:

hint: Waiting for your editor to close the file... error: cannot run code: No such file or directory
error: unable to start editor 'code'
Please supply the message using either -m or -F option.

我可以通过输入code终端来运行代码。


采取的步骤:

我已将以下行添加到我的 .zshrc

# Which plugins would you like to load?
# Standard plugins can be found in ~/.oh-my-zsh/plugins/*
# Custom plugins may be added to ~/.oh-my-zsh/custom/plugins/
# Example format: plugins=(rails git textmate ruby lighthouse)
# Add wisely, as too many plugins slow down shell startup.
plugins=(git)
plugins=(... vscode)

我已经运行了这条线:

git config --global core.editor "code --wait"

我在代码的 settings.json 中添加了这些行

    "terminal.external.osxExec": "iTerm.app",
    "terminal.explorerKind": "external",
    "terminal.integrated.shell.osx": "/bin/zsh",
    "terminal.integrated.shell.linux": "/bin/zsh"
}

我错过了什么?谢谢!

标签: gitvisual-studio-codeterminalzshiterm2

解决方案


根据您的输出,即:

error: cannot run code: No such file or directory

code在您的 $PATH 环境变量中不可用。这很容易解决:

  1. VSCode 中的 CTRL+SHIFT+P(或 Mac 上的 CMD+SHIFT+P)——这将打开命令面板。
  2. 键入“代码路径”之类的内容,或者您​​要执行的确切命令,即:Shell Command: Install 'code' command in PATH.

完成了,这应该可以解决问题。

具体到 Zsh,我的.zshrc文件也有这一行:

export EDITOR="code -w"

但这可能与 Git 的上下文无关,它对于使用 VSCode 作为编辑器的其他二进制文件很有用。


推荐阅读