gpt4 book ai didi

windows - 如何设置编辑器以在 Windows 上使用 Git?

转载 作者:IT王子 更新时间:2023-10-29 01:09:42 28 4
gpt4 key购买 nike

我正在尝试 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++ 设置为 %EDITOR%,以便它按预期与 Git 一起工作。

我没有和 Notepad++ 结婚。在这一点上,我不介意我使用什么编辑器。我只是希望能够在编辑器中键入提交消息,而不是命令行(使用-m)。

那些在 Windows 上使用 Git 的人:您使用什么工具来编辑您的提交消息,您必须做什么才能让它工作?

最佳答案

2015 年 9 月更新(6 年后)

last release of 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.

参见 commit 69b301b通过 Johannes Schindelin (dscho) .

而 Git 2.16 (Q1 2018) 会在生成编辑器时显示一条消息,告诉用户它正在等待用户完成编辑,以防编辑器打开一个 stash 的窗口或某处不起眼的地方,用户得到迷路了。

参见 commit abfb04d (2017 年 12 月 7 日),和 commit a64f213 (2017 年 11 月 29 日)Lars Schneider (larsxschneider) .
帮助:Junio C Hamano (gitster) .
(由 Junio C Hamano -- gitster -- merge 于 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 session ,但不适用于 git shell
(不是说有了 core.editor 配置机制,里面有 "start/WAIT..."的脚本不会工作,只会打开一个新的 DOS 窗口)


Bennett's answer提到避免添加脚本的可能性,而是直接引用程序本身 在简单引号之间。注意斜线的方向!使用 / 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”,或者我可以执行“git rebase -i ...' 来自 DOS 或 Git Shell。
机器人命令将触发一个新的 notepad++ 实例(因此有 -multiInst' 选项),并等待该实例关闭后再继续。

请注意,我只使用'/',而不是\'。而我installed msysgit using option 2. (将git\bin目录添加到PATH环境变量中,但不要覆盖一些内置的windows工具)

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


另见:


lightfire228添加 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 - 如何设置编辑器以在 Windows 上使用 Git?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10564/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com