0

I use a template with Git for keeping my commits messages neat and professional, as explained here.

However, since I use Magit, I would like to insert and expand a snippet with Yasnippets because Yasnippet is more dynamic than a static template.

I would like to know how to trigger the insertion and expansion of a snippet when the buffer for writing the commit message opens in Magit.

  • 2
    I'm not familiar with yasnippet, but you can add custom setup code to Magit commits via git-commit-setup-hook. – Basil May 21 '17 at 20:36
  • @Basil please make your comment an answer – shackra May 23 '17 at 1:16
1

Magit comes with the minor mode git-commit-mode, enabled via the entry-point global-git-commit-mode, which runs the hook and user option git-commit-setup-hook as its last step. You can add your git-commit-mode-specific setup code to this as you would any other hook variable:

(add-hook 'git-commit-setup-hook #'my-git-commit-setup-yasnippet)

or

(add-hook 'git-commit-setup-hook
          (lambda ()
            (do-something-with-yasnippet)))

You could also use the Customize interface to achieve this via M-x customize-option RET git-commit-setup-hook RET. Either way I recommend you look at the documentation and suggested and already enabled hook functions reported by Customize for this hook.

The setup code for git-commit-mode also enables an arbitrary major mode which controls the overall editing behaviour for commit messages. This mode is determined by the user option git-commit-major-mode. Thus the hook for whichever major mode you specify could offer another means of running custom setup code.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.