Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Help request] Key Maps #786

Open
MZachary opened this issue Dec 3, 2024 · 1 comment
Open

[Help request] Key Maps #786

MZachary opened this issue Dec 3, 2024 · 1 comment

Comments

@MZachary
Copy link

MZachary commented Dec 3, 2024

I'm a novice in dealing with my neovim configuration and creating my own settings, and am having trouble setting some keymaps I want for obsidian functions. I was hoping someone could just help me get the settings right, I just can't seem to get there on my own.

the main thing im struggling with is implementing a global key map for opening the quick switch and search function. I'm using lazy vim, and am doing this outside of the obsidian configuration. Im pasting all of the different methods ive tried to implement it as comments, but none of them have worked yet with various different errors of not finding the command and not referencing a nil data table.

local obsidian = require('obsidian').get_client()
vim.keymap.set('n', '<leader>os', function()
  -- obsidian:command("ObsidianSearch")
  -- require('obsidian.commands').search()
  -- require('obsidian').util.search()
  -- require('obsidian.commands'):search()
end, { desc = 'Obsidian Search' })

as a secondary "problem", I have this code that does work for another keymap, but I just struggle to believe its the right way to do it.
essentially, just doing obsidian:today() would create the file, but not actually open it. So i did the open_note wrapper to fix that.

local obsidian = require('obsidian').get_client()
vim.keymap.set('n', '<leader>od', function()
  obsidian:open_note(obsidian:today().path:resolve())
end, { desc = 'Obsidian Today' })

Thanks so much to anyone that can help, and I completely understand if this gets closed as irrelevant

@aniketgm
Copy link

aniketgm commented Dec 4, 2024

Since you are using LazyVim, I believe you have a keymaps.lua file right ? Is the code you mentioned in that file ?

I have a plugins folder, where all my plugins retain including obsidian.nvim. I have defined keymaps there itself. However, the keymaps can be defined in the keymaps file as well.
This is how I do it in keymaps.lua. I have a keymaps function at the top. You don't have to define a function everytime in every keymap call. Hope this helps.

local kmap_normal = function(key, cmd, desc)
  vim.api.nvim_set_keymap("n", key, cmd, { noremap = true, desc = desc })
end

local kmap_insert = function(key, cmd, desc)
  vim.api.nvim_set_keymap("i", key, cmd, { noremap = true, desc = desc })
end

-- And use it for all keymap configs below. So for your case I would do something like:
kmap_normal("<leader>od", "<cmd>ObsidianDailies<cr>", "Show dailies")
kmap_normal("<leader>of", "<cmd>ObsidianQuickSwitch<cr>", "Find notes")
....

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants