0

I want gj keys to execute function org-next-visible-heading (in evil normal mode). How should I do it in Spacemacs?

I was reading documentation and googling but I can't figure it out. What I tried already is adding these keys to different functions/key maps but nothing works. I tried to add these lines (one by one):

  (define-key org-mode-map "gj" 'org-next-visible-heading)
  (evil-define-key 'normal global-map  "gj" 'org-next-visible-heading)
  (define-key evil-normal-state-map "gj" 'org-next-visible-heading)
  (spacemacs/set-leader-keys-for-major-mode 'org-mode "gj" 'org-next-visible-heading)
  (spacemacs/set-leader-keys "gj" 'org-next-visible-heading)
  (define-key evil-normal-state-map "gj" 'org-next-visible-heading)
  (define-key evil-org-mode-map "gj" 'org-next-visible-heading)

in my ~/.spacemacs at the end of section dotspacemacs/user-config but it doesn't work.

1 Answer 1

2

EDIT

Looking a bit further, we find that evil-org-mode defines a minor-mode with the same name. And in the documentation for evil-define-key we find that the syntax below can be used to configure key bindings for minor modes.

Alternatively you could use

(evil-define-minor-mode-key 'normal 'evil-org-mode "gj" #'org-next-visible-heading)

For differences between these two evil macros, read the Mode Specific Keybindings section of noctuid's evil-guide.

END EDIT

It seems that Spacemacs uses the evil-org package for configuring the evil org-mode keybidnings. Indeed it is not so trivial to find how to configure those bindings. However, we can find the current binding using SPC h d k g j to find it is bound to org-forward-element. Subsequently, using ripgrep (SPC s f), we can search for org-forward-element in the .emacs.d/elpa directory. Finally, we find that we can configure the binding as follows:

(evil-define-key 'normal 'evil-org-mode "gj" #'org-next-visible-heading)

If you want to use it from your 'user-config' make sure to evaluate it after the function evil-org-set-key-theme has been evaluated. One way to achieve that is by advising it:

  (advice-add 'evil-org-set-key-theme :after (lambda (_)
      (evil-define-key 'normal 'evil-org-mode "gj" #'org-next-visible-heading)))
5
  • It actually doesn't work until I manually run SPC f e R (dostspacemacs/sync-configuration-layers) every time i start emacs. Do you know why? Maybe some other package runs later (after my dotspacemacs/user-config function in dotfile) and rebinds my keys? How to make it work without me running sync-configuration-layers every time?
    – Kossak
    Oct 31, 2022 at 15:41
  • I guess you have to wrap it in a 'with-eval-after-load': (with-eval-after-load 'org-evil (evil-define-key 'normal 'evil-org-mode "gj" #'org-next-visible-heading)) Oct 31, 2022 at 16:43
  • if I use with-eval-after-load the keys don't work at all, even after reloading dotfile. Maybe I'll create new topic for this problem. Thank you very much for help.
    – Kossak
    Nov 8, 2022 at 8:25
  • 1
    Interesting, well I did not test it indeed, but that is usually how it works. However, it turns out evil-org uses some special 'mechanism' to set the keybingings. I have updated the answer with a better solution. Nov 8, 2022 at 10:19
  • 1
    It works thank you very much :)
    – Kossak
    Nov 8, 2022 at 15:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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