Search code examples
giteditor

Is it possible to know which editor git is going to use programatically?


Is it possible to know which editor git is going to use programmatically? Something like this is possible?

$git which editor
/usr/bin/nano

Basically, I wanted to invoke a custom editor only for git commit messages. So if file name is '.git/COMMIT_EDITMSG', I will invoke something else, otherwise I want to fallback to default git editor.

I know that git uses $GIT_EDITOR, core.editor, $VISUAL, $EDITOR in order to choose an editor. But all of these are not present, I don't know what to do. Here it says, it uses vi. But in my machine, it uses nano. Hence, it is not really reliable to hardcode that.

Hence, how to know which editor will git use programmatically? If not directly from git, is there any other workaround?


Solution

  • Use the command

    git var GIT_EDITOR
    

    It outputs the value using exactly that algorithm that Git would use to call an editor regardless of the way an editor is configured. If none of the $GIT_EDITOR, core.editor, $VISUAL, $EDITOR are set it outputs some sensible default.

    When nothing is set on my Debian Linux system it outputs editor where /usr/bin/editor is an (indirect) symlink to vim.gtk3. On your system the chain of symlinks is probably pointed to nano.

    If you want to run "the same editor Git would call" to edit a file filename run this:

    `git var GIT_EDITOR` filename
    

    or

    $(git var GIT_EDITOR) filename