How can I set up an editor to work with Git on Win

2018-12-31 16:21发布

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?

30条回答
长期被迫恋爱
2楼-- · 2018-12-31 16:34

This works for Powershell and cmder-1.2 (when used with powershell). In ~/.gitconfig

[core]
    editor = 'c:/program files/sublime text 3/subl.exe' -w

How can I make Sublime Text the default editor for Git?

查看更多
几人难应
3楼-- · 2018-12-31 16:34

This is working for me using Cygwin and Textpad 6 (EDIT: also working with Textpad 5 as long as you make the obvious change to the script), and presumably the model could be used for other editors as well:

~/.gitconfig:

[core]
    editor = ~/script/textpad.sh

~/script/textpad.sh

#!/bin/bash

APP_PATH=`cygpath "c:/program files (x86)/textpad 6/textpad.exe"`
FILE_PATH=`cygpath -w $1`

"$APP_PATH" -m "$FILE_PATH"

This one-liner works as well:

~/script/textpad.sh (option 2):

"`cygpath "c:/program files (x86)/textpad 6/textpad.exe"`" -m "`cygpath -w $1`"
查看更多
情到深处是孤独
4楼-- · 2018-12-31 16:35

Edit: After updating to vim 7.3, I've come to the conclusion that the cleanest and easiest way to do this is:

  1. Add Vim's main folder to your path (Right click on My Computer -> Properties -> Advanced -> Environment Variables)

  2. Run this: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.

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

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.

查看更多
听够珍惜
5楼-- · 2018-12-31 16:37

Building on Darren's answer, to use Notepad++ you can simply do this (all on one line):

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 .

Works like a charm for me.

查看更多
其实,你不懂
6楼-- · 2018-12-31 16:37

Based on VonC suggestion above, this worked for me (was driving me crazy):

git config --global core.editor "'C:/Program Files (x86)/Sublime Text 3/subl.exe' -wait"

Omitting -wait can cause problems especially if you are working with gerrit and change ids that have to be manually copied to the bottom of your commit message

查看更多
刘海飞了
7楼-- · 2018-12-31 16:39

Notepad++ works just fine, although I choose to stick with Notepad, -m, or even sometimes the built-in "edit."

The problem you are encountering using Notepad++ is related to how git is launching the editor executable. My solution to this is to set EDITOR to a batch file, rather than the actual editor executable, that does the following:

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

c:\src>echo %EDITOR%
c:\tools\runeditor.bat
查看更多
登录 后发表回答