如何在Windows上设置可以与Git一起使用的编辑器?
我正在Windows上试用Git。我到了尝试" git commit"的地步,并且出现了这个错误:
Terminal is dumb but no VISUAL nor EDITOR defined. Please supply the message using either -m or -F option.
所以我发现我需要一个名为EDITOR的环境变量。没问题。我将其设置为指向记事本。那几乎奏效了。默认提交消息在记事本中打开。但是记事本不支持裸线换行。我出去了,得到了Notepad ++,但是我不知道如何将Notepad ++设置为可以像预期的那样与Git一起工作的%EDITOR%
。
我没有嫁给Notepad ++。在这一点上,我不介意我使用什么编辑器。我只希望能够在编辑器中而不是在命令行(带有-m)中键入提交消息。
那些在Windows上使用Git的人:我们使用什么工具来编辑提交消息,并且需要做些什么才能使其生效?
解决方案
回答
我很难让git与写字板,KomodoEdit以及我提供的几乎所有其他编辑器合作。多数打开以进行编辑,但是git显然不等待保存/关闭发生。
作为拐杖,我一直在做
git commit -m "Fixed the LoadAll method"
保持进展。倾向于使我的提交消息比应该的短一些,但是显然在Windows版本的git上需要做一些工作。
GitGUI也还不错。它需要一些定位,但是在那之后,它运行得很好。
回答
尽管我选择坚持使用记事本,-m甚至有时是内置的"编辑",但Notepad ++仍然可以正常工作。
使用Notepad ++遇到的问题与git如何启动编辑器可执行文件有关。我的解决方案是将EDITOR设置为批处理文件,而不是实际的编辑器可执行文件,该文件将执行以下操作:
start /WAIT "E:\PortableApps\Notepad++Portable\Notepad++Portable.exe" %*
/ WAIT告诉命令行会话停止,直到应用程序退出,因此我们可以在git高兴地等待同时编辑内心内容。 %*将所有参数传递到批处理文件,再传递到Notepad ++。
c:\src>echo %EDITOR% c:\tools\runeditor.bat
回答
Vim / Gvim对我来说效果很好。
>echo %EDITOR% c:\Vim\Vim71\vim.exe
回答
如果路径中有空格,似乎Git找不到编辑器。因此,我们必须将Patrick的答案中提到的批处理文件放入非空白路径。
回答
2015年9月更新(6年后)
git-for-Windows(2.5.3)的最新版本现在包括:
By configuring git config core.editor notepad, users can now use notepad.exe as their default editor. Configuring git config format.commitMessageColumns 72 will be picked up by the notepad wrapper and line-wrap the commit message after the user edits it.
参见Johannes Schindelin(dscho
)的commit 69b301b。
Git 2.16(Q1 2018)将显示一条消息,告知用户在生成编辑器时正在等待用户完成编辑,以防编辑器
打开一个隐藏的窗口或者晦涩的地方,用户
丢失的。
参见commit abfb04d(2017年12月7日)和commits a64f213(2017年11月29日)作者Lars Schneider(larsxschneider
)。
帮助人:Junio C Hamano(gitster
)。
(由Junio C Hamano -gitster
-in commit 0c69a13合并,2017年12月19日)
launch_editor(): indicate that Git waits for user input When a graphical GIT_EDITOR is spawned by a Git command that opens and waits for user input (e.g. "git rebase -i"), then the editor window might be obscured by other windows. The user might be left staring at the original Git terminal window without even realizing that s/he needs to interact with another window before Git can proceed. To this user Git appears hanging. Print a message that Git is waiting for editor input in the original terminal and get rid of it when the editor returns, if the terminal supports erasing the last line
原始答案
我刚刚使用git版本1.6.2.msysgit.0.186.gf7512和Notepad ++ 5.3.1进行了测试
我宁愿不必设置EDITOR变量,因此尝试了:
git config --global core.editor "\"c:\Program Files\Notepad++\notepad++.exe\"" # or git config --global core.editor "\"c:\Program Files\Notepad++\notepad++.exe\" %*"
这总是给:
C:\prog\git>git config --global --edit "c:\Program Files\Notepad++\notepad++.exe" %*: c:\Program Files\Notepad++\notepad++.exe: command not found error: There was a problem with the editor '"c:\Program Files\Notepad++\notepad++.exe" %*'.
如果我定义一个npp.bat,包括:
"c:\Program Files\Notepad++\notepad++.exe" %*
然后输入:
C:\prog\git>git config --global core.editor C:\prog\git\npp.bat
它仅可在DOS会话中使用,而不能从git shell中使用。
(不是使用core.editor配置机制,其中包含"start / WAIT ...
"的脚本将不起作用,而只能打开一个新的DOS窗口)
Bennett的答案提到可以避免添加脚本,而是直接在简单引号之间引用程序本身。注意斜线的方向!使用/
NOT\
来分隔路径名中的文件夹!
git config --global core.editor \ "'C:/Program Files/Notepad++/notepad++.exe' -multiInst -notabbar -nosession -noPlugin"
或者,如果我们使用的是64位系统:
git config --global core.editor \ "'C:/Program Files (x86)/Notepad++/notepad++.exe' -multiInst -notabbar -nosession -noPlugin"
但是我更喜欢使用脚本(如下所示):这样,我可以使用不同的路径或者不同的选项进行游戏,而无需再次注册git config
。
实际的解决方案(使用脚本)是要意识到:
我们在配置文件中引用的实际上是一个shell(/ bin / sh
)脚本,而不是DOS脚本。
所以有效的是:
C:\prog\git>git config --global core.editor C:/prog/git/npp.bat
与C:/ prog / git / npp.bat
:
#!/bin/sh "c:/Program Files/Notepad++/notepad++.exe" -multiInst "$*"
或者
#!/bin/sh "c:/Program Files/Notepad++/notepad++.exe" -multiInst -notabbar -nosession -noPlugin "$*"
有了这个设置,我可以在DOS或者Git Shell中进行'git config --global --edit,或者在DOS或者Git Shell中进行'git rebase -i ...'。 Bot命令将触发一个新的notepad ++实例(因此带有-multiInst
选项),并等待该实例关闭后再继续。
请注意,我仅使用" /",而不使用" "。我使用选项2安装了msysgit。(将git \ bin目录添加到环境变量PATH中,但不覆盖某些内置Windows工具)
notepad ++包装器称为.bat的事实并不重要。
最好将其命名为" npp.sh"并将其放置在" [git] \ cmd"目录中(或者放置在PATH环境变量引用的任何目录中)。
也可以看看:
- 如何使用视觉差异程序查看git diff输出?对于一般理论
- 如何使用msysgit / gitk设置DiffMerge?有关外部工具的另一个示例(DiffMerge和WinMerge)
lightfire228在评论中添加:
For anyone having an issue where N++ just opens a blank file, and git doesn't take your commit message, see "Aborting commit due to empty message": change your .bat or .sh file to say:
"<path-to-n++" .git/COMMIT_EDITMSG -<arguments>.
That will tell notepad++ to open the temp commit file, rather than a blank new one.
回答
无论如何,我一直在玩这个游戏,发现以下内容对我来说很不错:
git config --global core.editor "'C:/Program Files/TextPad 5/TextPad.exe' -m"
我认为CMD不喜欢单引号,因此我们必须使用双引号"指定空格嵌入的字符串参数"。
Cygwin(我相信它是Git Bash的基础平台)同时喜欢'和
";我们可以指定类似CMD的路径,使用
/代替
`,只要字符串用单引号引起来,即在这种情况下。
-m会覆盖/指示使用多个编辑器,并且最后不需要添加%*。
回答
我在Windows上使用Cygwin,因此我使用:
export EDITOR="emacs -nw"
-nw适用于no-windows,即告诉Emacs不要尝试使用X11.
Emacs键盘绑定不适用于Windows shell,因此我只能从Cygwin shell中使用...(建议使用rxvt。)
回答
我遇到了同样的问题,找到了不同的解决方案。我正在
error: There was a problem with the editor 'ec'
我的路径上有VISUAL = ec
和一个名为ec.bat
的批处理文件,其中包含一行:
c:\emacs\emacs-23.1\bin\emacsclient.exe %*
这使我可以使用ec <filename>
在命令行中编辑文件,并且具有可视化设置意味着大多数unixy程序也可以选择它。 Git似乎搜索路径与其他命令不同,尽管当我在ProcMon中查看git commit
时,我发现它在路径上的每个文件夹中查找ec
和ec.exe
,而不是查找ec .bat
。我添加了另一个环境变量(GIT_EDITOR = ec.bat
),一切都很好。
回答
基于Darren的答案,要使用Notepad ++,我们可以简单地做到这一点(全部一行):
在32位操作系统上:
`git config --global core.editor"'C:/ Program Files / Notepad ++ / notepad ++。exe'-multiInst -notabbar -nosession -noPlugin"
在64位操作系统上
`git config --global core.editor"'C:/程序文件(x86)/ Notepad ++ / notepad ++。exe'-multiInst -notabbar -nosession -noPlugin"
显然," C:/ Program Files / Notepad ++ / notepad ++。exe"部分应该是系统上Notepad ++可执行文件的路径。例如,在Windows 7上,可能是C:/ Program Files(x86)/ Notepad ++ / notepad ++。exe。感谢评论者指出这一点。
对我来说就像是一种魅力。
回答
我也在Windows上使用Cygwin,但使用的是gvim
(与基于终端的vim
相对)。
为了完成这项工作,我做了以下工作:
- 创建了一个包含以下内容的单行批处理文件(名为git_editor.bat
):
" C:/ Program Files / Vim / vim72 / gvim.exe" --nofork"%*"` - 将git_editor.bat放在我的PATH中。
- 设置
GIT_EDITOR = git_editor.bat
完成后,git commit
等将正确调用gvim可执行文件。
注1:gvim的--nofork
选项确保它一直阻塞,直到写入了提交消息为止。
注2:如果路径中有空格,则必须在gvim的路径周围加上引号。
注意3:需要用引号"%*"括起来,以防git传递带有空格的文件路径。
回答
我的PortableGit 1.6正常运行,但是升级到PortableGit-1.7 Windows版本后出现问题。有些git命令可以很好地打开Notepad ++。exe,但有些则不能,特别是git rebase的行为有所不同。
问题是某些命令运行Windows cmd进程,而某些命令使用unix cmd进程。我想将启动属性赋予Notepad ++编辑器,因此需要具有自定义脚本。我的解决方案是这样。
1)创建一个脚本以运行适当的文本编辑器。脚本看起来很奇怪,但是可以同时处理Windows和Unix变体。
c:/PortableGit/cmd/git-editor.bat
#!/bin/sh #open a new instance function doUnix() { "c:\program files\notepad++\notepad++.exe" -multiInst -nosession -notabbar $* exit } doUnix $* :WINCALL "c:\program files\notepad++\notepad++.exe" -multiInst -nosession -notabbar %*
2)设置全局core.editor变量
脚本已保存到git / cmd文件夹,因此它已经在gitconsole路径中,这是强制性的,因为完整路径可能无法正常工作。
git config --global core.editor "git-editor.bat"
现在,我可以运行git commit -a和git rebase -i master命令。如果我们在Git Windows工具中遇到问题,请尝试一下。
回答
我更喜欢使用emacs。进行设置可能有些棘手。
- 下载emacs并将其解压缩到
c:\ emacs
之类的地方。 - 运行
c:\ emacs \ bin \ addpm.exe
。如果我们使用的是Windows Vista或者更高版本,则需要右键单击并"以管理员身份运行"。这会将可执行文件放在路径中。 - 在.emacs文件中的某处添加
(server-start)
。有关将.emacs文件放置在何处的建议,请参阅Emacs Windows常见问题解答。 - git config --global core.editor emacsclientw`
Git现在将在现有emacs进程中打开文件。我们将必须从c:\ emacs \ bin \ runemacs.exe
中手动运行该现有进程。
回答
写字板!
我对使用vim很满意,但是由于我试图向公司介绍Git,所以我想要我们所有人都拥有的东西,并发现Wordpad似乎可以正常工作(即Git确实要等到完成编辑并关闭后再进行窗户)。
git config core.editor '"C:\Program Files\Windows NT\Accessories\wordpad.exe"'
那是在msysgit上使用Git Bash。我没有从Windows命令提示符下尝试过(如果有任何区别)。
回答
这是我使用Geany作为git编辑器的设置:
git config --global core.editor C:/path/to/geany.bat
在geany.bat中具有以下内容:
#!/bin/sh "C:\Program Files\Geany\bin\Geany.exe" --new-instance "$*"
它可以在DOS控制台和msysgit中使用。