epwalsh / epwalsh/obsidian.nvim

mappings not working when loaded within opts by lazy.nvim

Open
#707 0 comments 2 reactions 0 assignees View on GitHub
bug
Dominant language
Lua
Stars
6.2k
Forks
250
PR merge metrics
No merged PRs in 30d

Description

### 🐛 Describe the bug

Following suggestion re installation of obsidian.nvim using [lazy.nvim](https://github.com/epwalsh/obsidian.nvim#using-lazynvim), I add a new table `mappings` within `opts`, but the defined shortcuts don't work.

I've ensured obsidian.nvim has been loaded by 1) opening a markdown file (and confirming that `:Obsidian` shows commands) and 2) disabling lazy loading.

I can set and use the keymap fine using e.g. `nnoremap tc :lua require('obsidian').util.toggle_checkbox()` from command line in neovim, so I could just add the required keymaps elsewhere.

But I'm curious why this `mappings` table isn't being loaded, and as a relative noob this has delayed me quite a lot in getting going with obsidian.nvim and I'd love to help anyone else going down the same dead end!

### Config

~/.config/nvim-test/init.lua
```
-- Bootstrap lazy.nvim
local lazypath = vim.fn.stdpath("data") .. "/lazy/lazy.nvim"
if not (vim.uv or vim.loop).fs_stat(lazypath) then
local lazyrepo = "https://github.com/folke/lazy.nvim.git"
local out = vim.fn.system({ "git", "clone", "--filter=blob:none", "--branch=stable", lazyrepo, lazypath })
if vim.v.shell_error ~= 0 then
vim.api.nvim_echo({
{ "Failed to clone lazy.nvim:\n", "ErrorMsg" },
{ out, "WarningMsg" },
{ "\nPress any key to exit..." },
}, true, {})
vim.fn.getchar()
os.exit(1)
end
end
vim.opt.rtp:prepend(lazypath)

-- Make sure to setup `mapleader` and `maplocalleader` before
-- loading lazy.nvim so that mappings are correct.
-- This is also a good place to setup other settings (vim.opt)
vim.g.mapleader = " "
vim.g.maplocalleader = "\\"

-- Setup lazy.nvim
require("lazy").setup({
spec = {
-- add your plugins here
{
"epwalsh/obsidian.nvim",
version = "*", -- recommended, use latest release instead of latest commit
lazy = true,
ft = "markdown",
dependencies = {
-- Required.
"nvim-lua/plenary.nvim",
},
opts = {
workspaces = {
{
name = "personal",
path = "/home/myusername/Documents/My Notes",
},
},
mappings = {
-- Overrides the 'gf' mapping to work on markdown/wiki links within your vault.
["gf"] = {
action = function()
return require("obsidian").util.gf_passthrough()
end,
opts = { noremap = false, expr = true, buffer = true },
},
-- Toggle check-boxes.
["ch"] = {
action = function()
return require("obsidian").util.toggle_checkbox()
end,
opts = { buffer = true, desc = "Toggle checkbox" },
},
-- Smart action depending on context, either follow link or toggle checkbox.
[""] = {
action = function()
return require("obsidian").util.smart_action()
end,
opts = { buffer = true, expr = true },
},
["os"] = {
action = function()
return require("obsidian").util.ObsidianQuickSwitch()
end,
opts = { silent = true, noremap = true, desc = "[O]bsidian Quick[S]witch" },
},
},
},
},
},
-- colorscheme that will be used when installing plugins.
install = { colorscheme = { "habamax" } },
-- automatically check for plugin updates
checker = { enabled = true },
})
```

### Environment

```
❯ NVIM_APPNAME=nvim-test nvim --version
NVIM v0.10.1
Build type: RelWithDebInfo
LuaJIT 2.1.1720049189
Run "nvim -V1 -v" for more info
```

```
❯ NVIM_APPNAME=nvim-test nvim --headless -c 'lua require("obsidian").info()' -c q
Obsidian.nvim v3.9.0 (ae1f76a75c7ce36866e1d9342a8f6f5b9c2caf9b)
Status:
• buffer directory: nil
• working directory: /home/myusername/.config/nvim-test
Workspaces:
✓ active workspace: Workspace(name='personal', path='/home/myusername/Documents/My Notes', root='/home/myusername/Documents/My Notes')
Dependencies:
✓ plenary.nvim: ec289423a1693aeae6cd0d503bac2856af74edaa
Integrations:
✓ picker: nil
✗ completion: disabled
Tools:
✓ rg: ripgrep 14.1.0
Environment:
• operating system: Linux
Config:
• notes_subdir: nil
```

Contributor guide

Open the contributing guide

Research direction

Start with ~/.config/nvim-test/init.lua and reproduce the configuration using lazy.nvim, then inspect how the obsidian.nvim opts table is loaded when opening a Markdown file. Verify the mapping behavior with Neovim 0.10.1 and the headless require("obsidian").info() command; done means the configured mappings work after lazy loading.

Written by the indexing model from the issue text.

Assessment

Tech stack
lua
Domain
developer-experience
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.