From 58d1014324883a1dc2f8a9e03949b3f1b54af427 Mon Sep 17 00:00:00 2001 From: Alexander Courtis Date: Sun, 4 Jun 2023 14:02:32 +1000 Subject: [PATCH] docs: add on_attach to quickstart (#2236) --- README.md | 26 ++++++++++++++++++++++++++ doc/nvim-tree-lua.txt | 24 ++++++++++++++++++++++++ 2 files changed, 50 insertions(+) diff --git a/README.md b/README.md index 1ce9f184..d123ec03 100644 --- a/README.md +++ b/README.md @@ -76,6 +76,32 @@ require("nvim-tree").setup({ }) ``` +Optionally customise your mappings, see [:help nvim-tree-mappings](doc/nvim-tree-lua.txt) + +```lua + local function my_on_attach(bufnr) + local api = require "nvim-tree.api" + + local function opts(desc) + return { desc = "nvim-tree: " .. desc, buffer = bufnr, noremap = true, silent = true, nowait = true } + end + + -- default mappings + api.config.mappings.default_on_attach(bufnr) + + -- custom mappings + vim.keymap.set("n", "", api.tree.change_root_to_parent, opts "Up") + vim.keymap.set("n", "?", api.tree.toggle_help, opts "Help") + end + + -- pass to setup along with your other options + require("nvim-tree").setup { + --- + on_attach = my_on_attach, + --- + } +``` + Open the tree: `:NvimTreeOpen` Show the mappings: `g?` diff --git a/doc/nvim-tree-lua.txt b/doc/nvim-tree-lua.txt index 4fd47088..9b16d3af 100644 --- a/doc/nvim-tree-lua.txt +++ b/doc/nvim-tree-lua.txt @@ -107,6 +107,30 @@ Setup the plugin in your `init.lua` > }, }) < +Optionally customise your |nvim-tree-mappings| via |nvim-tree.on_attach| > + + local function my_on_attach(bufnr) + local api = require "nvim-tree.api" + + local function opts(desc) + return { desc = "nvim-tree: " .. desc, buffer = bufnr, noremap = true, silent = true, nowait = true } + end + + -- default mappings + api.config.mappings.default_on_attach(bufnr) + + -- custom mappings + vim.keymap.set("n", "", api.tree.change_root_to_parent, opts "Up") + vim.keymap.set("n", "?", api.tree.toggle_help, opts "Help") + end + + -- pass to setup along with your other options + require("nvim-tree").setup { + --- + on_attach = my_on_attach, + --- + } +< Open the tree: `:NvimTreeOpen` Show the mappings: `g?`