如何全局配置git以使用特定的编辑器(例如vim)提交消息?


当前回答

设置Git的默认编辑器

选择一个:

在Git配置中设置core.editor:git-config—全局core.editor“vim”设置GIT_EDITOR环境变量:导出GIT_EDITOR=vim


设置所有程序的默认编辑器

设置标准化的VISUAL和EDITOR环境变量*:

export VISUAL=vim
export EDITOR="$VISUAL"

注意:不一定需要同时设置这两个,但某些程序可能不会使用更正确的VISUAL。参见视觉与编辑。


修复兼容性问题

有些编辑器需要--wait标志,否则将打开空白页。例如:

Sublime文本(如果设置正确;或使用可执行文件的完整路径代替subl):export VISUAL=“subl--等待”VS代码(添加shell命令后):export VISUAL=“code--wait”

其他回答

在Ubuntu和Debian(谢谢@MichielB)上,也可以通过运行以下命令来更改默认编辑器:

sudo update-alternatives --config editor

这将提示以下内容:

There are 4 choices for the alternative editor (providing /usr/bin/editor).

  Selection    Path                Priority   Status
------------------------------------------------------------
  0            /bin/nano            40        auto mode
  1            /bin/ed             -100       manual mode
  2            /bin/nano            40        manual mode
* 3            /usr/bin/vim.basic   30        manual mode
  4            /usr/bin/vim.tiny    10        manual mode

Press enter to keep the current choice[*], or type selection number: 

对于EmEditor用户

要将EmEditor设置为Git的默认文本编辑器,请打开Git Bash,然后键入:

git config--全局core.editor“emeditor.exe-sp”

需要EmEditor v19.9.2或更高版本。

在Mac OSX 10中将Sublime Text 2设置为Git提交编辑器

运行以下命令:

$ git config --global core.editor "/Applications/Sublime\ Text\ 2.app/Contents/SharedSupport/bin/subl"

或者只是:

$ git config --global core.editor "subl -w"

只是因为我来这里是为了寻找一个一次性的解决方案(在我的情况下,我通常使用vim,但这一次我想使用VS代码),其他人可能也想知道:

GIT_EDITOR='code -w' git rebase -i …

这是我的git/hub版本,仅用于上下文:

git version 2.24.2 (Apple Git-127)
hub version 2.14.1

对于Textmate用户

这将在您想要编辑提交时打开Textmate编辑器。需要安装textmate命令行工具。

git-config--全局core.editor“mate-w”