Ken Muse

Configuring Git's Text Editor

Git is a powerful tool, but some of the functionality is tied to having a text editor that can update settings, configure changes, or modify the contents of files. Because the defaults may not match your preferences, it’s important to specify the editor you want to use and the commands required to use it.

To determine the editor to use, Git examples the environment and configuration settings. First, it looks for the GIT_EDITOR environment variable. If that’s not found, it looks for the local (and then global) configuration setting core.editor. After that, it looks for two more environment variables – VISUAL followed by EDITOR before defaulting to vi. That’s a lot of attempts to find the right editor!

Personally, I enjoy using Visual Studio Code, so I use the following global default:

1git config --global core.editor "code --wait"

The --wait is important. It ensures that Git waits for VS Code to close the file before proceeding. Code will open a tab for the file, and the process will continue once the tab closes. If you prefer a new window, use code --wait --new-window for the editor configuration instead.

On Linux, I tend to prefer nano, which uses this command:

1git config --global core.editor "nano -w"

Resetting the environment to use its defaults is simple – just unset the variable:

1git config --global --unset core.editor

Need the configuration for a difference text editor? GitHub has been kind enough to document how to associate the most common text editors with Git for Windows, Mac, and Linux. The Git Book provides a broader list of editors.

Git also has the ability to configure a tool to use for merging and a tool to use for diff’ing files. The configuration for VS Code looks like this:

1git config --global merge.tool vscode
2git config --global mergetool.vscode.cmd 'code --wait $MERGED'
4git config --global diff.tool vscode
5git config --global difftool.vscode.cmd 'code --wait --diff $LOCAL $REMOTE'

This globally configures Visual Studio code to handle any Diff or Merge requests for you.

I actually keep this organized using Dotfiles to provide consistency across environments. This also makes it easy for my configuration to follow me wherever I go.

That’s all there is to it. A few simple changes, and you can be working within your editor of choice!

Happy DevOp’ing!