How do I globally configure Git to use a particular editor (e.g. vim) for commit messages?

To configure Git to use a specific editor globally (e.g., Vim, Nano, VS Code) for commit messages, use the git config command to set the core.editor setting. Here’s how:

1. Set the Default Editor Globally

git config --global core.editor "vim"
  • Replace vim with your preferred editor’s command (e.g., nano, code --wait, subl -n -w).
  • This applies to all repositories on your system.

2. Common Editor Examples

EditorCommand
Vimgit config --global core.editor "vim"
Nanogit config --global core.editor "nano"
VS Codegit config --global core.editor "code --wait"
Sublime Textgit config --global core.editor "subl -n -w"
Emacsgit config --global core.editor "emacs"

3. Verify the Configuration

git config --global --get core.editor

This returns the configured editor (e.g., vim).

4. Temporarily Override for a Single Commit

Use the GIT_EDITOR environment variable:

GIT_EDITOR=nano git commit

Notes

  • VS Code/IDEs: Use --wait to force Git to wait until the editor closes.
  • Windows Paths: Use the full path if the editor isn’t in your PATH (e.g., "C:/Program Files/Notepad++/notepad++.exe").
  • System Default: Git uses your $EDITOR shell variable if core.editor isn’t set.

Troubleshooting

  • Editor Not Opening: Ensure the editor command is installed and accessible in your terminal.
  • Spaces in Paths: Wrap paths in quotes (e.g., "C:/Program Files/...").
  • Test with a Commit:
  git commit  # Should open your configured editor

By configuring this once, you’ll streamline your Git workflow!

Leave a Reply

Your email address will not be published. Required fields are marked *