如何设置编辑器以在 Windows 上使用 Git? [英] How can I set up an editor to work with Git on Windows?

查看:47
本文介绍了如何设置编辑器以在 Windows 上使用 Git?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试 Windows 上的 Git.我到了尝试git commit"的地步,但出现此错误:

<块引用>

终端很笨,但没有 VISUAL 也没有编辑器定义.请提供使用 -m 或 -F 选项的消息.

所以我发现我需要一个名为 EDITOR 的环境变量.没问题.我将它设置为指向记事本.这几乎奏效了.默认提交消息在记事本中打开.但记事本不支持裸换行.我出去得到了 Notepad++,但我不知道如何获得Notepad++ 设置为 %EDITOR% 以使其按预期与 Git 一起工作.

我没有嫁给 Notepad++.在这一点上,我不介意我使用什么编辑器.我只想能够在编辑器中输入提交消息而不是命令行(使用-m).

那些在 Windows 上使用 Git 的人:您使用什么工具来编辑提交消息,以及您需要做什么才能使其正常工作?

解决方案

2015 年 9 月更新(6 年后)

最新版本的 git-for-Windows (2.5.3) 现在包括:

<块引用>

通过配置git config core.editor notepad,用户现在可以使用 notepad.exe 作为他们的默认编辑器.
配置 git config format.commitMessageColumns 72 将被记事本包装器拾取,并在用户编辑提交消息后换行.

参见 commit 69b301b 来自 约翰内斯·辛德林 (dscho).

并且 Git 2.16(2018 年第一季度)将显示一条消息,告诉用户它在生成编辑器时正在等待用户完成编辑,以防编辑器打开一个隐藏的窗口或某个不知名的地方,用户得到丢了.

参见commit abfb04d(2017 年 12 月 7 日)和 commit a64f213(2017 年 11 月 29 日)由 Lars Schneider (larsxschneider).
帮助者:Junio C Hamano (gitster).
(由 Junio C Hamano 合并 -- gitster --提交 0c69a13,2017 年 12 月 19 日)

<块引用>

launch_editor():表示Git等待用户输入

当图形 GIT_EDITOR 由打开的 Git 命令生成时并等待用户输入(例如git rebase -i"),然后是编辑器窗口可能会被其他窗口遮挡.
用户可能会盯着原始的 Git 终端窗口甚至没有意识到他/她需要在 Git 可以继续之前与另一个窗口交互.给这个用户 Git看起来挂了.

在原文中打印一条Git正在等待编辑器输入的消息终端并在编辑器返回时摆脱它,如果终端支持擦除最后一行

<小时>

原答案

我刚刚用 git 版本 1.6.2.msysgit.0.186.gf7512 和 Notepad++5.3.1 测试了它

我更喜欢必须设置一个 EDITOR 变量,所以我尝试了:

git config --global core.editor ""c:Program FilesNotepad++
otepad++.exe""# 或者git config --global core.editor ""c:Program FilesNotepad++
otepad++.exe" %*"

这总是给:

C:proggit>git config --global --edit"c:Program FilesNotepad++
otepad++.exe" %*: c:Program FilesNotepad++
otepad++.exe: 命令未找到错误:编辑器 '"c:Program FilesNotepad++
otepad++.exe" %*' 有问题.

如果我定义了一个npp.bat,包括:

"c:Program FilesNotepad++
otepad++.exe" %*

然后我输入:

C:proggit>git config --global core.editor C:proggit
pp.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:proggit>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',或者我可以执行 'git rebase -i ...' 来自 DOS 或 Git Shell.
Bot 命令将触发 notepad++ 的新实例(因此有 -multiInst' 选项),并在继续之前等待该实例关闭.

请注意,我只使用了'/',而不是'.我使用选项 2 安装了 msysgit.(将gitin目录添加到PATH环境变量中,但不覆盖一些windows内置工具)

notepad++ 包装器被称为 .bat 的事实并不重要.
最好将其命名为npp.sh"并将其放在 [git]cmd 目录中(或 PATH 环境变量引用的任何目录中).

<小时>

另见:

<小时>

lightfire228 增加了 在评论中:

<块引用>

对于任何遇到 N++ 只是打开一个空白文件而 git 不接受您的提交消息的问题的人,请参阅中止提交由于空消息":将您的 .bat.sh 文件更改为:

".

<块引用>

这将告诉记事本++打开临时提交文件,而不是一个空白的新文件.

I'm trying out Git on Windows. I got to the point of trying "git commit" and I got this error:

Terminal is dumb but no VISUAL nor EDITOR defined. Please supply the message using either -m or -F option.

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.

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 editor rather than the command line (with -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?

解决方案

Update September 2015 (6 years later)

The last release of git-for-Windows (2.5.3) now includes:

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.

See commit 69b301b by Johannes 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.

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)

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


Original answer

I just tested it with git version 1.6.2.msysgit.0.186.gf7512 and Notepad++5.3.1

I prefer to not have to set an EDITOR variable, so I tried:

git config --global core.editor ""c:Program FilesNotepad++
otepad++.exe""
# or
git config --global core.editor ""c:Program FilesNotepad++
otepad++.exe" %*"

That always gives:

C:proggit>git config --global --edit
"c:Program FilesNotepad++
otepad++.exe" %*: c:Program FilesNotepad++
otepad++.exe: command not found
error: There was a problem with the editor '"c:Program FilesNotepad++
otepad++.exe" %*'.

If I define a npp.bat including:

"c:Program FilesNotepad++
otepad++.exe" %*

and I type:

C:proggit>git config --global core.editor C:proggit
pp.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)


Bennett's answer mentions 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!

git config --global core.editor 
"'C:/Program Files/Notepad++/notepad++.exe' -multiInst -notabbar -nosession -noPlugin"

Or if you are in a 64 bit system:

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.


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.

So what does work is:

C:proggit>git config --global core.editor C:/prog/git/npp.bat

with 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.

Note that I use only '/', not '. And I installed msysgit using option 2. (Add the gitin directory to the PATH environment variable, but without overriding some built-in windows tools)

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).


See also:


lightfire228 adds in the comments:

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.

这篇关于如何设置编辑器以在 Windows 上使用 Git?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆