licht1stein / obsidian.el

Obsidian Notes for Emacs
GNU General Public License v3.0
376 stars 27 forks source link

+TITLE: Obsidian Notes for Emacs

[[https://melpa.org/#/obsidian][file:https://melpa.org/packages/obsidian-badge.svg]] [[https://stable.melpa.org/#/obsidian][file:https://stable.melpa.org/packages/obsidian-badge.svg]]

Emacs front-end for [[https://obsidian.md/][Obsidian Notes]].

+begin_src

M-x package-install RET obsidian RET

+end_src

Put this in your ~init.el~:

+begin_src elisp

(require 'obsidian) (obsidian-specify-path "~/MY_OBSIDIAN_FOLDER") ;; If you want a different directory of `obsidian-capture': (setq obsidian-inbox-directory "Inbox") ;; Clicking on a wiki link referring a non-existing file the file can be ;; created in the inbox (t) or next to the file with the link (nil). ;; Default: t - creating in the inbox ;(setq obsidian-wiki-link-create-file-in-inbox nil) ;; You may want to define a folder for daily notes. By default it is the inbox. ;(setq obsidian-daily-notes-directory "Daily Notes") ;; Directory of note templates, unset (nil) by default ;(setq obsidian-templates-directory "Templates") ;; Daily Note template name - requires a template directory. Default: Daily Note Template.md ;(setq obsidian-daily-note-template "Daily Note Template.md")

;; Define obsidian-mode bindings (add-hook 'obsidian-mode-hook (lambda () ;; Replace standard command with Obsidian.el's in obsidian vault: (local-set-key (kbd "C-c C-o") 'obsidian-follow-link-at-point)

 ;; Use either `obsidian-insert-wikilink' or `obsidian-insert-link':
 (local-set-key (kbd "C-c C-l") 'obsidian-insert-wikilink)

 ;; Following backlinks
 (local-set-key (kbd "C-c C-b") 'obsidian-backlink-jump)))

;; Optionally you can also bind a few functions: ;; replace "YOUR_BINDING" with the key of your choice: (global-set-key (kbd "YOUR_BINDING") 'obsidian-jump) ;; Opening a note (global-set-key (kbd "YOUR_BINDING") 'obsidian-capture) ;; Capturing a new note in the inbox (global-set-key (kbd "YOUR_BINDING") 'obsidian-daily-note) ;; Creating daily note

;; Activate detection of Obsidian vault (global-obsidian-mode t)

+end_src

Or using [[https://github.com/jwiegley/use-package][use-package]]:

+begin_src elisp

(use-package obsidian :ensure t :demand t :config (obsidian-specify-path "~/MY_OBSIDIAN_FOLDER") (global-obsidian-mode t) :custom ;; This directory will be used for obsidian-capture' if set. (obsidian-inbox-directory "Inbox") ;; Create missing files in inbox? - when clicking on a wiki link ;; t: in inbox, nil: next to the file with the link ;; default: t ;(obsidian-wiki-link-create-file-in-inbox nil) ;; The directory for daily notes (file name is YYYY-MM-DD.md) (obsidian-daily-notes-directory "Daily Notes") ;; Directory of note templates, unset (nil) by default ;(obsidian-templates-directory "Templates") ;; Daily Note template name - requires a template directory. Default: Daily Note Template.md ;(obsidian-daily-note-template "Daily Note Template.md") :bind (:map obsidian-mode-map ;; Replace C-c C-o with Obsidian.el's implementation. It's ok to use another key binding. ("C-c C-o" . obsidian-follow-link-at-point) ;; Jump to backlinks ("C-c C-b" . obsidian-backlink-jump) ;; If you prefer you can useobsidian-insert-link' ("C-c C-l" . obsidian-insert-wikilink)))

+end_src

Optionally you can specify ~obsidian-inbox-directory~, it will be used by ~obsidian-capture~ to store new notes into. If you don't set it the root folder of your Obsidian vault will be used.

I wanted to work with Obsidian Notes using Emacs. Obviously you already can open your Obsidian folder and start editing markdown files with Emacs. But I want to improve that and split the responsibilities between Emacs and Obsidian the way it makes sense for an Emacs user.

** What should we keep doing in Obsidian?

** What should be possible to do in Emacs? Obsidian.el must empower us to stay in Emacs for things that make sense in Emacs:

When all of the above is ready we will almost never need the Obsidian app on desktop, but will still be able to use it on mobile or when specifically needed.

** company-mode completion [[./resources/tag-completion.png]]

Once the ~obsidian-mode~ is activated obsidian.el scans all markdown files in the vault for tags and links (links still WIP), and stores these lists in it's global variables. It also adds [[http://company-mode.github.io/][company-mode]] backends to suggest links and tags for completion.

** [[https://github.com/abo-abo/hydra][Hydra]] menu

When [[https://github.com/abo-abo/hydra][Hydra]] is installed, ~obsidian-hydra~ will be defined such that it can be used for bindings:

+begin_src elisp

(bind-key (kbd "C-c M-o") 'obsidian-hydra/body 'obsidian-mode-map)

+end_src

[[./resources/hydra-menu.png]]

** Including hidden dot files Obsidian does not track hidden files; obsidian.el can be configured to either track them or ignore them by setting the value of obsidian-include-hidden-files.

** Manual re-scan You can update the lists of tags, links etc. manually if it's lagging for some reason by running an interactive command:

+begin_src

M-x obsidian-update RET

+end_src

** Following links Obsidian.el implements a custom command ~obsidian-follow-link-at-point~ which correctly follows markdown and wiki links generated by the Obsidian App. In the install example above this command is bound to ~C-c C-o~ in ~obsidian-mode~.

+begin_src

M-x obsidian-follow-link-at-point RET

+end_src

Note that the Obsidian app replaces spaces with ~%20~ when inserting markdown links, and doesn't do that when inserting wiki links. Obsidian.el follows this convention to maximize compatibility:

+begin_src markdown

Markdown link with spaces: 2-sub with spaces and буквы

Wikilink with spaces: [[Subdir/2-sub with spaces and буквы]]

+end_src

Both these types of links are correctly handled by ~obsidian-follow-link-at-point~.

** Following backlinks You can quickly jump to backlinks to current file using ~obsidian-backlink-jump~

+begin_src

M-x obsidian-backlink-jump RET

+end_src

*** Multiple matches Obsidian doesn't insert relative path by default, only does it when there are multiple files with the same name. ~obsidian-follow-link-at-point~ handles this correctly. Every time you follow a link it checks, if there's only one match for the filename linked. If there's just one it simply opens that file. If there's more than one it prompts you to select which file to open.

** Inserting links [[./resources/insert-link.png]]

When inserting links, two different formats can be used to specify the file: the filename alone, or the path to the file within the Obsidian vault. The default is to only use the filename, but this behavior can be changed by setting the variable ~obsidian-links-use-vault-path~ to ~t~. Alternately, using the prefix argument before the call to insert a link will toggle this behavior, inserting a link with the format opposite of this variable.

There are two commands to insert links ~obsidian-insert-link~ and ~obsidian-insert-wikilink~, you can choose one depending on your preferred link format:

*** Inserts a link in Markdown format Example: ~Link description~

+begin_src

M-x obsidian-insert-link RET

+end_src

Note, that when you insert a link to file that has spaces in it's name, like "facts about inserting links.md", Obsidian app would html-format the spaces, meaning the link will look like

+begin_src markdown

facts

+end_src

Obsidian.el follows this convention and does the same when inserting markdown links. ~obsidian-follow-link-at-point~ handles this correctly.

*** Insert a link in wikilink format Example: ~[[path/fo/file.md|Link description]]~

+begin_src

M-x obsidian-insert-wikilink RET

+end_src

** Jumping between notes Quickly jump between notes using ~obsidian-jump~

+begin_src

M-x obsidian-jump RET

+end_src

*** Aliases If you have YAML front matter in your note, Obsidian.el will find aliases in it and add them to the ~obsidian-jump~ selection. Both ~aliases~ and ~alias~ keys are supported.

** Capturing new note Use ~obsidian-capture~. If you specified ~obsidian-inbox-directory~, it will create new notes in this directory. Otherwise in your Obsidian vault root directory:

+begin_src

M-x obsidian-capture RET

+end_src

** Searching notes Use ~obsidian-search~ to look for a string or a regular expression:

+begin_src

M-x obsidian-search RET query RET

+end_src

** Finding all notes with a tag Use ~obsidian-tag-find~ to list all notes that contain a tag. Let's you choose a tag from list of all tags:

+begin_src

M-x obsidian-tag-find RET

+end_src

** Move note to another folder Use ~obsidian-move-file~ to move current note to another folder:

+begin_src

M-x obsidian-move-file RET

+end_src

** Templates

Obsidian.el has a basic template support, where the Obsidian app's template placeholders can be used, without customization. {{title}}, {{date}}, and {{time}} can be used. {{title}} is the name of the file without the extension.

*** Development tasks

** Org-roam or any other great Emacs libraries? The answer is mostly the same for all of them. Mobile support. Or rather — NO mobile support. I don't buy into the story that "you don't really need your PKM system on mobile", and "serious work is done only on desktop" etc. These are just excuses for the impossibility of building a full-fledged mobile version of Emacs.

So there were two ways to go about it: build a mobile app for something like org-roam (which would be cool, but is above my front-end skills) or build a light-weight Emacs client for something like Obsidian. I chose the simpler task.