From 73db7e5faf827bcc75757afbac37616e5c10e62d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micka=C3=ABl=20Menu?= Date: Mon, 10 Jan 2022 10:02:11 +0100 Subject: [PATCH] Improve key mappings example (#18) --- README.md | 62 +++++++++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 56 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 1e06b69..e78fdff 100644 --- a/README.md +++ b/README.md @@ -355,13 +355,63 @@ require("zk.ui").get_pick_notes_list_api_selection(options) ``` ## Example Mappings + +Add these global mappings in your main Neovim config: + +```lua +local opts = { noremap=true, silent=false } + +-- Create a new note after asking for its title. +vim.api.nvim_set_keymap("n", "zn", "ZkNew { title = vim.fn.input('Title: ') }", opts) + +-- Open notes. +vim.api.nvim_set_keymap("n", "zo", "ZkNotes", opts) +-- Open notes associated with the selected tags. +vim.api.nvim_set_keymap("n", "zt", "ZkTags", opts) + +-- Search for the notes matching a given query. +vim.api.nvim_set_keymap("n", "zf", "ZkNotes { match = vim.fn.input('Search: ') }", opts) +-- Search for the notes matching the current visual selection. +vim.api.nvim_set_keymap("v", "zf", ":'<,'>ZkMatch", opts) +``` + +You can add additional key mappings for Markdown buffers located in a `zk` notebook, using `ftplugin`. First, make sure it is enabled in your Neovim config: + +```viml +filetype plugin on +``` + +Then, create a new file under `~/.config/nvim/ftplugin/markdown.lua` to setup the mappings: + ```lua -vim.api.nvim_set_keymap("n", "zc", "ZkNew", { noremap = true }) -vim.api.nvim_set_keymap("x", "zc", ":'<'>ZkNewFromTitleSelection", { noremap = true }) -vim.api.nvim_set_keymap("n", "zn", "ZkNotes", { noremap = true }) -vim.api.nvim_set_keymap("n", "zb", "ZkBacklinks", { noremap = true }) -vim.api.nvim_set_keymap("n", "zl", "ZkLinks", { noremap = true }) -vim.api.nvim_set_keymap("n", "zt", "ZkTags", { noremap = true }) +-- Add the key mappings only for Markdown files in a zk notebook. +if require("zk.util").notebook_root(vim.fn.expand('%:p')) ~= nil then + local function map(...) vim.api.nvim_buf_set_keymap(0, ...) end + local opts = { noremap=true, silent=false } + + -- Open the link under the caret. + map("n", "", "lua vim.lsp.buf.definition()", opts) + + -- Create a new note after asking for its title. + -- This overrides the global `zn` mapping to create the note in the same directory as the current buffer. + map("n", "zn", "ZkNew { dir = vim.fn.expand('%:p:h'), title = vim.fn.input('Title: ') }", opts) + -- Create a new note in the same directory as the current buffer, using the current selection for title. + map("v", "znt", ":'<,'>ZkNewFromTitleSelection { dir = vim.fn.expand('%:p:h') }", opts) + -- Create a new note in the same directory as the current buffer, using the current selection for note content and asking for its title. + map("v", "znc", ":'<,'>ZkNewFromContentSelection { dir = vim.fn.expand('%:p:h'), title = vim.fn.input('Title: ') }", opts) + + -- Open notes linking to the current buffer. + map("n", "zb", "ZkBacklinks", opts) + -- Alternative for backlinks using pure LSP and showing the source context. + --map('n', 'zb', 'lua vim.lsp.buf.references()', opts) + -- Open notes linked by the current buffer. + map("n", "zl", "ZkLinks", opts) + + -- Preview a linked note. + map("n", "K", "lua vim.lsp.buf.hover()", opts) + -- Open the code actions for a visual selection. + map("v", "za", ":'<,'>lua vim.lsp.buf.range_code_action()", opts) +end ``` # Miscellaneous