Skip to content

Autocmds

github-actions edited this page Jul 25, 2026 · 11 revisions

Conventions

Autocmd events provide by this plugin follows following conventions:

  • Corresponds to Vim autocmd if reasonable, like BufWritePost in a vault will trigger ObsidianNoteWritePost.

Events

name triggered by data
ObsidianNoteCreate Note.create { note, opts }
ObsidianNoteEnter BufEnter
ObsidianNoteLeave BufLeave
ObsidianNoteWritePost BufWritePost
ObsidianNoteWritePre BufWritePre
ObsidianWorkspaceSet When you enter or switch workspace workspace object
ObsidianSyncChanged When obsidian sync changes status
ObsidianAttachmentAdded After adding an attachment { path, ctx }

How to use

vim.api.create_autocmd("User", {
  pattern = "ObsidianNoteEnter",
  callback = function(ev)
    local note = require("obsidian.note").from_buffer(ev.buf)
    --- anything you want to do
  end,
})

Example

Format code blocks

vim.api.nvim_create_autocmd("User", {
  pattern = "ObsidianNoteWritePost",
  callback = function(ev)
    require("conform").format {
      bufnr = ev.buf,
      formatters = { "prettier", "injected" },
    }
  end,
})

Turn off spell based on frontmatter

vim.api.nvim_create_autocmd("User", {
  pattern = "ObsidianNoteEnter",
  callback = function(ev)
    local note = require("obsidian.note").from_buffer(ev.buf)
    if note and note.metadata and note.metadata.spell == false then
      vim.wo.spell = false
    end
  end,
})

Clone this wiki locally