epwalsh / obsidian.nvim

Obsidian šŸ¤ Neovim
Apache License 2.0
4.31k stars 197 forks source link

Custom Mappings in the Vault #118

Closed devstefancho closed 1 year ago

devstefancho commented 1 year ago

šŸš€ The feature, motivation and pitch

It would be great If I can use custom mappings in the Vault For example gd for FollowLink and Enter for creating new link and go into new file (this feature is in vimwiki)

mappings are possible in telescope and also other popular plugins I have no knowlege for creating this stuff, but it would be great If I can defined mappings to using only for the Vault

require('telescope').setup{
  defaults = {
    -- Default configuration for telescope goes here:
    -- config_key = value,
    mappings = {
      i = {
        -- map actions.which_key to <C-h> (default: <C-/>)
        -- actions.which_key shows the mappings for your picker,
        -- e.g. git_{create, delete, ...}_branch for the git_branches picker
        ["<C-h>"] = "which_key"
      }
    }
  },
}

Alternatives

Another solution is just binding with cmd to another keymap. but when I work in the Vault, most files are markdown so I don't need to use "go to definition"(gd) so In this case, I want to override this only in Vault

Additional context

No response

epwalsh commented 1 year ago

Hey @devstefancho, it looks like this would be pretty straightforward to add. We could add an autocommand that only runs in the vault directories and sets buffer-local keymaps like this:

vim.keymap.set("n", "gf", "<cmd>ObsidianFollowLink<cr>", { buffer = true })

Actually we already have the autocommand for this:

https://github.com/epwalsh/obsidian.nvim/blob/0823e607d569d470ef82d1d41c34de58524ae762/lua/obsidian/init.lua#L95-L99

So it's just a matter of adding this option to the config and then setting the keymaps in the lazy_setup() function.

devstefancho commented 1 year ago

@epwalsh Thanks, it works only for my vault šŸ˜ƒ

I have one more question this is not related with this plugin I have already have "gd" keymap with another plugin (lspsaga.nvim) and now I want "gd" keymap have more priority for obisidian only in the vault

-- Definition
keymap("<leader>gd", "<Cmd>Lspsaga lsp_finder<CR>", "[g]o to [d]efinition of usages") -- Show usages

but this obisidian load faster than lspsaga.nvim ( I've tried priority = 1 for obisidian.nvim)

return {
  "epwalsh/obsidian.nvim",
  config = function()
    require("obsidian").setup({
      dir = "~/iCloud/Documents/my-vault",
      daily_notes = {
        folder = "daily",
      },
    })

    vim.keymap.set("n", "<M-o>", "<Cmd>ObsidianQuickSwitch<CR>", { desc = "[Obsidian] Quick Search" })
    vim.keymap.set("n", "gd", "<cmd>ObsidianFollowLink<cr>", { buffer = true }) -- Perfectly Work 
  end,
  lazy = false,
}
epwalsh commented 1 year ago

@devstefancho I think the issue is that you have lazy = false. Have you tried something like this:

return {
  "epwalsh/obsidian.nvim",
  config = function()
    require("obsidian").setup({
      dir = "~/iCloud/Documents/my-vault",
      daily_notes = {
        folder = "daily",
      },
    })

    vim.keymap.set("n", "<M-o>", "<Cmd>ObsidianQuickSwitch<CR>", { desc = "[Obsidian] Quick Search" })
    vim.keymap.set("n", "gd", "<cmd>ObsidianFollowLink<cr>", { buffer = true }) -- Perfectly Work 
  end,
  lazy = true,
  event = { "BufReadPre " .. vim.fn.expand "~" .. "/iCloud/Documents/my-vault/**.md" },
}
devstefancho commented 1 year ago

@epwalsh Thanks! It work, Setting lazy to true then I load :Lazy load obsidian.nvim which is overriding previous global keymap but this keymap is only applied to the first opened buffer, now I need to figure out myself why BufReadPre is not working. so I better to close this issue and will update more comment with my solution