How to get org-roam keybindings working after an emacs restart

Here’s the portion of my org-roam config where I define keybindings:

(use-package org-roam
  :ensure nil
  :after org
  :bind (("C-c n l" . org-roam-buffer-toggle)
         ("C-c n f" . org-roam-node-find)
         ("C-c n g" . org-roam-graph)
         ("C-c n i" . org-roam-node-insert)
         ("C-c n c" . org-roam-capture)
		 ("C-c n j" . org-roam-dailies-capture-today))
  :bind-keymap (("C-c n d" . org-roam-dailies-map))
  :custom
  (org-roam-directory (expand-file-name "~/Documents/org/roam/"))

However, after an emacs restart, the C-c n keychord is not available to me, emacs says C-c n is undefined. I can use M-x org-roam-node-find and only then my C-c n keychord become available to me. How can I get it available to me even after a fresh emacs start?


Edit: I have use-package-always-defer enabled, if it matters:

(use-package use-package
  :demand t
  :custom
  (use-package-hook-name-suffix nil)
  (use-package-compute-statistics t)
  (use-package-always-defer t))

I noticed that my deferring of org mode was a contributing factor. And once I visit a single org file (and hence load the org mode in my emacs), the C-c n keychord becomes available to me.

Is there something like org-roam-prefix-map? Grepping the source code got me zero hits.

org-roam comes with one major mode and one minor mode, both of which should create a keymap. But… I have a feeling that’s not what you are after.

If you want to use these keybindings after init, and yet defer the loading of commands for quicker init, I think all you need is to remove :after org. You can try, relaunch Emacs, and see what happens. I have a feeling that’s what you wish to do?

  :after org
  :bind (("C-c n l" . org-roam-buffer-toggle)
         ("C-c n f" . org-roam-node-find)
         ("C-c n g" . org-roam-graph)
         ("C-c n i" . org-roam-node-insert)
         ("C-c n c" . org-roam-capture)
		 ("C-c n j" . org-roam-dailies-capture-today))

With :after org, your whole (use-package org-roam ...) form gets evaluated only after org is loaded, so the keybindings are undefined after init.

Without :after org, (use-package org-roam ...)should get evaluated when your init is evaluated, so keybindings can be used after init. But because of how :bind works in use-package it will defer loading of these org-roam commands.

I could be wrong, but you can try this and let us know if I am mistaken.

If I am mistaken about :bind you can still directly define keybindings and defer loading of the comands.

1 Like

You were right about :after. I removed it. It now works.