Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 488
Patrick McElhaney Avatar asked Aug 14 '08 01:08

Patrick McElhaney


People also ask

What editor should I use for git windows?

On Windows, if you use Git Bash the default editor will be Vim. Vim is another text editor, like nano or notepad. In order to get started Vim there are only a few commands you must remember. in the terminal.

How do I set git as default editor in Windows?

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.

What editor should I choose for git?

Yes, Git uses Vim as its editor while committing your changes. Show activity on this post. Doing so launches your editor of choice. (This is set by your shell's $EDITOR environment variable – usually vim or emacs, although you can configure it with whatever you want using the git config --global core.

How to configure Git to use notepad as default editor?

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

How do I use Git on my computer?

Git has two modes of use – a bash scripting shell (or command line) and a graphical user interface (GUI). To launch Git Bash open the Windows Start menu, type git bash and press Enter (or click the application icon).

What are the prerequisites to install Git on Windows?

Prerequisites. Administrator privileges; Access to a command-line; Your favorite coding text editor; Username and password for the Github website (optional) Steps For Installing Git for Windows. Installing Git prompts you to select a text editor. If you don’t have one, we strongly advise you to install prior to installing Git.

Why is Git waiting for users to finish editing when spawning?

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.


2 Answers

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 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:

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


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:\prog\git>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 git\bin 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:

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

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.

like image 67
VonC Avatar answered Oct 16 '22 02:10

VonC


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.

It works like a charm for me.

Article How to set Notepad++ as the default Git editor for commits instead of Vim explains parameters of the command.

like image 27
Bennett McElwee Avatar answered Oct 16 '22 02:10

Bennett McElwee