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


当前回答

在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: 

其他回答

有一个命令列表,您可以使用,但对于vs代码,请使用此命令

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

这是所有编辑器的链接:https://git-scm.com/book/en/v2/Appendix-C%3A-Git命令设置和配置

使用git review时,我必须修改sequence.editor值,以便能够进行交互式rebase(gitrebase-I-p):

git config --global sequence.editor "gvim"  # or whatever your prefer

gvim要求:apt-installvim-gtk

工具书类

#2944 Interactive Rebase失败,出现警告:“Vim:警告:输出不到终端”安装sjubar/rebase编辑器的部分

对于Mac OS X,使用TextEdit或自然环境编辑器编辑文本:

git config --global core.editor "open -W -n"

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

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

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

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

设置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”