如何设置编辑器以在 Windows 上使用 Git?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10564/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I set up an editor to work with Git on Windows?
提问by Patrick McElhaney
I'm trying out Git on Windows. I got to the point of trying "git commit" and I got this error:
我正在Windows 上试用Git。我到了尝试“git commit”的地步,但出现此错误:
Terminal is dumb but no VISUAL nor EDITOR defined. Please supply the message using either -m or -F option.
终端很笨,但没有定义 VISUAL 或 EDITOR。请使用 -m 或 -F 选项提供消息。
So I figured out I need to have an environment variable called EDITOR. No problem. I set it to point to Notepad. That worked, almost. The default commit message opens in Notepad. But Notepad doesn't support bare line feeds. I went out and got Notepad++, but I can't figure out how to get Notepad++ set up as the %EDITOR%
in such a way that it works with Git as expected.
所以我发现我需要一个名为 EDITOR 的环境变量。没问题。我将它设置为指向记事本。这几乎奏效了。默认提交消息在记事本中打开。但记事本不支持裸换行。我出去买了Notepad++,但我不知道如何将 Notepad++ 设置为%EDITOR%
,使其按预期与 Git 一起使用。
I'm not married to Notepad++. At this point I don't mind what editor I use. I just want to be able to type commit messages in an editorrather than the command line (with -m
).
我没有嫁给 Notepad++。在这一点上,我不介意我使用什么编辑器。我只想能够在编辑器中而不是命令行中输入提交消息(使用-m
)。
Those of you using Git on Windows: What tool do you use to edit your commit messages, and what did you have to do to make it work?
那些在 Windows 上使用 Git 的人:您使用什么工具来编辑您的提交消息,您需要做什么才能使其工作?
回答by VonC
Update September 2015 (6 years later)
2015 年 9 月更新(6 年后)
The last release of git-for-Windows (2.5.3)now includes:
git-for-Windows (2.5.3)的最新版本现在包括:
By configuring
git config core.editor notepad
, users can now usenotepad.exe
as their default editor.
Configuringgit config format.commitMessageColumns 72
will be picked up by the notepad wrapper and line-wrap the commit message after the user edits it.
通过配置
git config core.editor notepad
,用户现在可以notepad.exe
将其用作默认编辑器。
配置git config format.commitMessageColumns 72
将由记事本包装器拾取,并在用户编辑提交消息后换行。
See commit 69b301bby Johannes Schindelin (dscho
).
见提交69b301b由约翰内斯Schindelin( )dscho
。
And Git 2.16 (Q1 2018) will show a message to tell the user that it is waiting for the user to finish editing when spawning an editor, in case the editor opens to a hidden window or somewhere obscure and the user gets lost.
并且 Git 2.16(2018 年第一季度)将显示一条消息,告诉用户它在生成编辑器时正在等待用户完成编辑,以防编辑器打开到隐藏窗口或某个不明显的地方并且用户迷路了。
See commit abfb04d(07 Dec 2017), and commit a64f213(29 Nov 2017) by Lars Schneider (larsxschneider
).
Helped-by: Junio C Hamano (gitster
).
(Merged by Junio C Hamano -- gitster
--in commit 0c69a13, 19 Dec 2017)
见提交abfb04d(2017年12月7日),并提交a64f213(2017年11月29日)由拉尔斯·施奈德(larsxschneider
)。
帮助者:Junio C Hamano ( gitster
)。
(由Junio C gitster
Hamano合并-- --在commit 0c69a13,2017 年 12 月 19 日)
launch_editor()
: indicate that Git waits for user inputWhen 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
launch_editor()
: 表示 Git 等待用户输入当图形
GIT_EDITOR
由打开并等待用户输入(例如“git rebase -i
”)的 Git 命令生成时,编辑器窗口可能会被其他窗口遮挡。
用户可能会一直盯着原始的 Git 终端窗口,甚至没有意识到他/她需要在 Git 继续之前与另一个窗口进行交互。对于这个用户来说,Git 似乎挂了。如果终端支持擦除最后一行,则在原始终端打印一条 Git 正在等待编辑器输入的消息,并在编辑器返回时将其删除
Original answer
原答案
I just tested it with git version 1.6.2.msysgit.0.186.gf7512 and Notepad++5.3.1
我刚刚用 git 版本 1.6.2.msysgit.0.186.gf7512 和 Notepad++5.3.1 对其进行了测试
I prefer to nothave to set an EDITOR variable, so I tried:
我更喜欢不必设置 EDITOR 变量,所以我尝试了:
git config --global core.editor "\"c:\Program Files\Notepad++\notepad++.exe\""
# or
git config --global core.editor "\"c:\Program Files\Notepad++\notepad++.exe\" %*"
That always gives:
这总是给出:
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" %*'.
If I define a npp.bat including:
如果我定义一个 npp.bat 包括:
"c:\Program Files\Notepad++\notepad++.exe" %*
and I type:
我输入:
C:\prog\git>git config --global core.editor C:\prog\git\npp.bat
It just works from the DOS session, but not from the git shell.
(not that with the core.editor configuration mechanism, a script with "start /WAIT...
" in it would not work, but only open a new DOS window)
它只适用于 DOS 会话,而不适用于 git shell。
(不是说 core.editor 配置机制,带有“ start /WAIT...
”的脚本不起作用,只能打开一个新的 DOS 窗口)
Bennett's answermentions the possibility to avoid adding a script, but to reference directly the program itself between simple quotes. Note the direction of the slashes! Use /
NOT \
to separate folders in the path name!
Bennett 的回答提到了避免添加脚本的可能性,而是在简单的引号之间直接引用程序本身。注意斜线的方向!使用/
NOT\
在路径名中分隔文件夹!
git config --global core.editor \
"'C:/Program Files/Notepad++/notepad++.exe' -multiInst -notabbar -nosession -noPlugin"
Or if you are in a 64 bit system:
或者,如果您使用的是 64 位系统:
git config --global core.editor \
"'C:/Program Files (x86)/Notepad++/notepad++.exe' -multiInst -notabbar -nosession -noPlugin"
But I prefer using a script (see below): that way I can play with different paths or different options without having to register again a git config
.
但我更喜欢使用脚本(见下文):这样我就可以使用不同的路径或不同的选项,而无需再次注册git config
.
The actual solution (with a script) was to realize that:
what you refer to in the config file is actually a shell (/bin/sh
) script, not a DOS script.
实际的解决方案(使用脚本)是要意识到:
您在配置文件中所指的实际上是 shell ( /bin/sh
) 脚本,而不是 DOS 脚本。
So what does work is:
那么有效的是:
C:\prog\git>git config --global core.editor C:/prog/git/npp.bat
with C:/prog/git/npp.bat
:
与C:/prog/git/npp.bat
:
#!/bin/sh
"c:/Program Files/Notepad++/notepad++.exe" -multiInst "$*"
or
或者
#!/bin/sh
"c:/Program Files/Notepad++/notepad++.exe" -multiInst -notabbar -nosession -noPlugin "$*"
With that setting, I can do 'git config --global --edit
' from DOS or Git Shell, or I can do 'git rebase -i ...
' from DOS or Git Shell.
Bot commands will trigger a new instance of notepad++ (hence the -multiInst
' option), and wait for that instance to be closed before going on.
通过该设置,我可以git config --global --edit
从 DOS 或 Git Shell 执行 ' git rebase -i ...
' ,或者我可以从 DOS 或 Git Shell执行 ' '。
Bot 命令将触发 notepad++ 的新实例(因此有-multiInst
' 选项),并在继续之前等待该实例关闭。
Note that I use only '/', not \
'. And I installed msysgit using option 2.(Add the git\bin
directory to the PATH
environment variable, but without overriding some built-in windows tools)
请注意,我只使用“/”,而不是\
“。我使用选项 2 安装了 msysgit。(将git\bin
目录添加到PATH
环境变量中,但没有覆盖一些内置的 Windows 工具)
The fact that the notepad++ wrapper is called .bat is not important.
It would be better to name it 'npp.sh' and to put it in the [git]\cmd
directory though (or in any directory referenced by your PATH environment variable).
notepad++ 包装器称为 .bat 的事实并不重要。
最好将其命名为“npp.sh”并将其放在[git]\cmd
目录中(或 PATH 环境变量引用的任何目录中)。
See also:
也可以看看:
- How do I view ‘git diff' output with visual diff program?for the general theory
- How do I setup DiffMerge with msysgit / gitk?for another example of external tool (DiffMerge, and WinMerge)
- 如何使用visual diff程序查看'git diff'输出?对于一般理论
- 如何使用 msysgit/gitk 设置 DiffMerge?外部工具的另一个示例(DiffMerge 和 WinMerge)
lightfire228adds in the comments:
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:
对于任何遇到 N++ 只是打开一个空白文件而 git 不接受您的提交消息的问题的人,请参阅“由于空消息而中止提交”:将您的
.bat
或.sh
文件更改为:
"<path-to-n++" .git/COMMIT_EDITMSG -<arguments>.
That will tell notepad++ to open the temp commit file, rather than a blank new one.
这将告诉记事本++打开临时提交文件,而不是一个空白的新文件。
回答by Bennett McElwee
Building on Darren's answer, to use Notepad++ you can simply do this (all on one line):
基于Darren 的回答,要使用 Notepad++,您可以简单地执行此操作(全部在一行上):
git config --global core.editor "'C:/Program Files/Notepad++/notepad++.exe' -multiInst -notabbar -nosession -noPlugin"
Obviously, the C:/Program Files/Notepad++/notepad++.exe
part should be the path to the Notepad++ executable on your system. For example, it might be C:/Program Files (x86)/Notepad++/notepad++.exe
.
显然,该C:/Program Files/Notepad++/notepad++.exe
部分应该是系统上 Notepad++ 可执行文件的路径。例如,它可能是C:/Program Files (x86)/Notepad++/notepad++.exe
.
It works like a charm for me.
它对我来说就像一种魅力。
回答by Darren Bishop
Anyway, I've just been playing around with this and found the following to work nicely for me:
无论如何,我一直在玩这个,发现以下对我来说很好用:
git config --global core.editor "'C:/Program Files/TextPad 5/TextPad.exe' -m"
I don't think CMD likes single-quotes so you must use double quotes "to specify the space embedded string argument".
我认为 CMD 不喜欢单引号,因此您必须使用双引号“指定嵌入的空格字符串参数”。
Cygwin (which I believe is the underlying platform for Git's Bash) on the other hand likes both '
and "
; you can specify a CMD-like paths, using /
instead of \
, so long as the string is quoted i.e. in this instance, using single-quotes.
另一方面,Cygwin(我认为它是 Git 的 Bash 的底层平台)同时喜欢'
和"
; 你可以指定一个类似 CMD 的路径,使用/
而不是\
,只要字符串被引用,即在这种情况下,使用单引号。
The -m
overrides/indicates the use of multiple editors and there is no need for a %*
tacked on the end.
该-m
重写/指示使用多个编辑器,并没有必要为一个%*
上涨的结束。
回答by Nick Knowlson
Edit:After updating to Vim 7.3, I've come to the conclusion that the cleanest and easiest way to do this is:
编辑:更新到 Vim 7.3 后,我得出的结论是,最简洁、最简单的方法是:
Add Vim's main folder to your path (right click on My Computer→ Properties→ Advanced→ Environment Variables)
Run this:
git config --global core.editor "gvim --nofork '%*'"
将 Vim 的主文件夹添加到您的路径(右键单击我的电脑→属性→高级→环境变量)
运行这个:
git config --global core.editor "gvim --nofork '%*'"
If you do it this way, then I am fairly sure it will work with Cygwin as well.
如果你这样做,那么我很确定它也适用于 Cygwin。
Original answer:
原答案:
Even with a couple of Vim-related answers, I was having trouble getting this to work with gVim under Windows (while not using a batch file or %EDITOR% or Cygwin).
即使有几个与 Vim 相关的答案,我也无法在 Windows 下使用 gVim(同时不使用批处理文件或 %EDITOR% 或 Cygwin)。
What I eventually arrived at is nice and clean, and draws from a few of the solutions here:
我最终得出的结论既漂亮又干净,并借鉴了这里的一些解决方案:
git config --global core.editor \
"'C:/Program Files/Vim/vim72/gvim.exe' --nofork '%*'"
One gotcha that took me a while is these are not the Windows-style backslashes. They are normal forward slashes.
我花了一段时间的一个问题是这些不是 Windows 风格的反斜杠。它们是正常的正斜杠。
回答by Patrick Johnmeyer
Notepad++ works just fine, although I choose to stick with Notepad, -m, or even sometimes the built-in "edit."
Notepad++ 工作得很好,尽管我选择坚持使用 Notepad、-m,甚至有时使用内置的“编辑”。
The problem you are encountering using Notepad++ is related to how Git is launching the editor executable. My solution to this is to set environment variable EDITOR
to a batch file, rather than the actual editor executable, that does the following:
您在使用 Notepad++ 时遇到的问题与 Git 如何启动编辑器可执行文件有关。我对此的解决方案是将环境变量EDITOR
设置为批处理文件,而不是实际的编辑器可执行文件,它执行以下操作:
start /WAIT "E:\PortableApps\Notepad++Portable\Notepad++Portable.exe" %*
/WAIT
tells the command line session to halt until the application exits, thus you will be able to edit to your heart's content while Git happily waits for you. %* passes all arguments to the batch file through to Notepad++.
/WAIT
告诉命令行会话暂停,直到应用程序退出,这样你就可以在 Git 愉快地等待你的同时编辑你的心的内容。%* 将批处理文件的所有参数传递给 Notepad++。
C:\src> echo %EDITOR%
C:\tools\runeditor.bat
回答by Gavin
写字板!
I'm happy using Vim, but since I'm trying to introduce Git to the company I wanted something that we'd all have, and found that WordPad seems to work okay (i.e. Git does wait until you're finished editing and close the window).
我很高兴使用 Vim,但由于我正在尝试向公司介绍 Git,我想要一些我们都拥有的东西,并且发现写字板似乎工作正常(即 Git 确实等到您完成编辑并关闭窗户)。
git config core.editor '"C:\Program Files\Windows NT\Accessories\wordpad.exe"'
That's using Git Bash on msysgit; I've not tried from the Windows command prompt (if that makes any difference).
那是在 msysgit 上使用 Git Bash;我还没有从 Windows 命令提示符尝试过(如果这有什么不同的话)。
回答by Tim Henigan
I also use Cygwin on Windows, but with gVim (as opposed to the terminal-based Vim).
我也在 Windows 上使用 Cygwin,但使用 gVim(与基于终端的Vim相反)。
To make this work, I have done the following:
为了使这项工作,我做了以下工作:
- Created a one-line batch file (named
git_editor.bat
) which contains the following:"C:/Program Files/Vim/vim72/gvim.exe" --nofork "%*"
- Placed
git_editor.bat
on in myPATH
. - Set
GIT_EDITOR=git_editor.bat
- 创建了一个单行批处理文件(名为
git_editor.bat
),其中包含以下内容:"C:/Program Files/Vim/vim72/gvim.exe" --nofork "%*"
- 放在
git_editor.bat
我的PATH
. - 放
GIT_EDITOR=git_editor.bat
With this done, git commit
, etc. will correctly invoke the gVim executable.
完成此操作后git commit
, 等将正确调用 gVim 可执行文件。
NOTE 1: The --nofork
option to gVim ensures that it blocks until the commit message has been written.
注意 1:--nofork
gVim的选项确保它阻塞直到提交消息被写入。
NOTE 2: The quotes around the path to gVim is required if you have spaces in the path.
注意 2:如果路径中有空格,则 gVim 路径周围的引号是必需的。
NOTE 3: The quotes around "%*" are needed just in case Git passes a file path with spaces.
注意 3: 需要在 "%*" 周围加上引号,以防 Git 传递带空格的文件路径。
回答by Eddie B
Thanks to the Stack Overflow community ... and a little research I was able to get my favorite editor, EditPad Pro, to work as the core editor with msysgit 1.7.5.GIT and TortoiseGit v1.7.3.0 over Windows XP SP3...
感谢 Stack Overflow 社区 ... 和一些研究,我能够让我最喜欢的编辑器 EditPad Pro作为核心编辑器使用 msysgit 1.7.5.GIT 和 TortoiseGit v1.7.3.0 在 Windows XP SP3 上工作。 ..
Following the advice above, I added the path to a Bash script for the code editor...
按照上面的建议,我为代码编辑器添加了 Bash 脚本的路径...
git config --global core.editor c:/msysgit/cmd/epp.sh
However, after several failed attempts at the above mentioned solutions ... I was finally able to get this working. Per EditPad Pro's documentation, adding the '/newinstance' flag would allow the shell to wait for the editor input...
然而,在上述解决方案的几次失败尝试之后......我终于能够让它工作。根据 EditPad Pro 的文档,添加“/newinstance”标志将允许 shell 等待编辑器输入...
The '/newinstance' flag was the key in my case...
' /newinstance' 标志在我的案例中是关键......
#!/bin/sh
"C:/Program Files/JGsoft/EditPadPro6/EditPadPro.exe" //newinstance "$*"
回答by OmgKemuel
For Atomyou can do
对于Atom你可以做
git config --global core.editor "atom --wait"
and similar for Visual Studio Code
git config --global core.editor "code --wait"
which will open up an Atomor Visual Studio Codewindow for you to commit through,
这将打开一个Atom或Visual Studio Code窗口供您提交,
or for Sublime Text:
或者对于崇高的文本:
git config --global core.editor "subl -n -w"
回答by guwer
Edit .gitconfig file in c:\Users\YourUser folder and add:
编辑 c:\Users\YourUser 文件夹中的 .gitconfig 文件并添加:
[core]
editor = 'C:\Program files\path\to\editor.exe'