0

I have this magit configuration which automatically sets the magit commit message buffer to evil INSERT mode, but it assumes that evil is installed and active which may not be the case. How can I configure it so that it will only apply if evil is installed and active?

(use-package magit
  :ensure t
  :bind (("C-x g" . magit-status))
  :config
  (setq magit-ediff-dwim-show-on-hunks t)
  (add-hook 'git-commit-setup-hook #'evil-insert-state)
  )
  • featurep can be used to check if a feature is present. You'd have to look up what feature evil provides and conditionally add the hook. – DoMiNeLa10 Sep 10 '17 at 11:22
2

If you only want the evil-related section to load if/when evil is loaded, you could wrap it with with-eval-after-load 'evil, like so:

(use-package magit
  :ensure t
  :bind (("C-x g" . magit-status))
  :config
  (setq magit-ediff-dwim-show-on-hunks t)
  (with-eval-after-load 'evil
    (add-hook 'git-commit-setup-hook #'evil-insert-state)))

If you want the whole magit config to only load if evil is loaded, you could use the :after keyword like so:

(use-package magit
  :ensure t
  :after evil
  :bind (("C-x g" . magit-status))
  :config
  (setq magit-ediff-dwim-show-on-hunks t)
  (add-hook 'git-commit-setup-hook #'evil-insert-state))
1

Use package has a :if directive that will

Initialize and load only if EXPR evaluates to a non-nil value.

Adding that to your config will stop magit from being loaded if evil is not already loaded.

(use-package magit
  :if (featurep 'evil-mode)
  :ensure t
  :bind (("C-x g" . magit-status))
  :config
  (setq magit-ediff-dwim-show-on-hunks t)
  (add-hook 'git-commit-setup-hook #'evil-insert-state)
  )

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.