diff --git a/README.md b/README.md index 7fced059..0f3c8682 100644 --- a/README.md +++ b/README.md @@ -43,37 +43,6 @@ let g:nvim_tree_show_icons = { "1 by default, notice that if 'files' is 1, it will only display "if nvim-web-devicons is installed and on your runtimepath -" You can edit keybindings be defining this variable -" You don't have to define all keys. -" NOTE: the 'edit' key will wrap/unwrap a folder and open a file -let g:nvim_tree_bindings = { - \ 'edit': ['', 'o'], - \ 'edit_vsplit': '', - \ 'edit_split': '', - \ 'edit_tab': '', - \ 'close_node': ['', ''], - \ 'toggle_ignored': 'I', - \ 'toggle_dotfiles': 'H', - \ 'refresh': 'R', - \ 'preview': '', - \ 'cd': '', - \ 'create': 'a', - \ 'remove': 'd', - \ 'rename': 'r', - \ 'full_rename': '', - \ 'cut': 'x', - \ 'copy': 'c', - \ 'paste': 'p', - \ 'prev_git_item': '[c', - \ 'next_git_item': ']c', - \ 'dir_up': '-', - \ 'close': 'q', - \ } - -" Disable default mappings by plugin -" Bindings are enable by default, disabled on any non-zero value -" let nvim_tree_disable_keybindings=1 - " default will show icon by default if no icon is provided " default shows no icon by default let g:nvim_tree_icons = { @@ -108,8 +77,10 @@ highlight NvimTreeFolderIcon guibg=blue ## KeyBindings +### Default actions + - move around like in any vim buffer -- `` on `..` will cd in the above directory +- `` or `o` on `..` will cd in the above directory - `` will cd in the directory under the cursor - `` will close current opened directory or parent - type `a` to add a file. Adding a directory requires leaving a leading `/` at the end of the path. @@ -135,6 +106,28 @@ highlight NvimTreeFolderIcon guibg=blue - Double left click acts like `` - Double right click acts like `` +### Setup + +You can disable default mappings with +```vim +" let nvim_tree_disable_keybindings=1 +``` +But you won't be able to map any keys from the setup with nvim_tree_bindings. Use with caution. + + +Default keybindings can be overriden. You can also define your own keymappings for the tree view: +```vim +lua <"] = ":YourVimFunction()", + ["u"] = ":lua require'some_module'.some_function()", + } +EOF +``` +You can check the default binding table in `nvim-tree/config.lua`, under the `get_bindings` function. +This basically maps the default keys to the `on_keypress` function with different string parameters. +All mappings are set in `normal mode`. + ## Note This plugin is very fast because it uses the `libuv` `scandir` and `scandir_next` functions instead of spawning an `ls` process which can get slow on large files when combining with `stat` to get file informations. diff --git a/doc/nvim-tree-lua.txt b/doc/nvim-tree-lua.txt index 4e1637b4..0a795844 100644 --- a/doc/nvim-tree-lua.txt +++ b/doc/nvim-tree-lua.txt @@ -236,27 +236,18 @@ INFORMATIONS *nvim-tree-info* |g:nvim_tree_bindings| *g:nvim_tree_bindings* -you can change default keybindings by defining this variable. -default keybindings will be applied to undefined keys. +Default keybindings can be overriden. You can also define your own keymappings for the tree view: > - let g:nvim_tree_bindings = { - \ edit: ['', 'o'], // Multiple keys provided via list - \ edit_vsplit: '', - \ edit_split: '', - \ edit_tab: '', - \ close_node: ['', ''], - \ cd: '', - \ preview: '', - \ create: 'a', - \ remove: 'd', - \ rename: 'r', - \ cut: 'x', - \ copy: 'c', - \ paste: 'p', - \ prev_git_item: '[c', - \ next_git_item: ']c', - \ dir_up: '-', - \ } + lua <"] = ":YourVimFunction()", + ["u"] = ":lua require'some_module'.some_function()", + } + EOF + +You can check the default binding table in `nvim-tree/config.lua`, under the `get_bindings` function. +This basically maps the default keys to the `on_keypress` function with different string parameters. +All mappings are set in `normal` `mode`. |Features| *nvim-tree-features* diff --git a/lua/nvim-tree/config.lua b/lua/nvim-tree/config.lua index bb2ac16a..5a340c15 100644 --- a/lua/nvim-tree/config.lua +++ b/lua/nvim-tree/config.lua @@ -51,31 +51,39 @@ function M.get_icon_state() } end +local function get_lua_cb(cb_name) + return string.format(":lua require'nvim-tree'.on_keypress('%s')", cb_name) +end + function M.get_bindings() local keybindings = vim.g.nvim_tree_bindings or {} - return { - edit = keybindings.edit or {'', 'o'}, - edit_vsplit = keybindings.edit_vsplit or '', - edit_split = keybindings.edit_split or '', - edit_tab = keybindings.edit_tab or '', - close_node = keybindings.close_node or {'', ''}, - preview = keybindings.preview or '', - toggle_ignored = keybindings.toggle_ignored or 'I', - toggle_dotfiles = keybindings.toggle_dotfiles or 'H', - refresh = keybindings.refresh or 'R', - cd = keybindings.cd or '', - create = keybindings.create or 'a', - remove = keybindings.remove or 'd', - rename = keybindings.rename or 'r', - full_rename = keybindings.full_rename or '', - cut = keybindings.cut or 'x', - copy = keybindings.copy or 'c', - paste = keybindings.paste or 'p', - prev_git_item = keybindings.prev_git_item or '[c', - next_git_item = keybindings.next_git_item or ']c', - dir_up = keybindings.dir_up or '-', - close = keybindings.close or 'q', - } + return vim.tbl_extend('force', { + [""] = get_lua_cb("edit"), + ["o"] = get_lua_cb("edit"), + ["<2-LeftMouse>"] = get_lua_cb("edit"), + ["<2-RightMouse>"] = get_lua_cb("cd"), + [""] = get_lua_cb("cd"), + [""] = get_lua_cb("vsplit"), + [""] = get_lua_cb("split"), + [""] = get_lua_cb("tabnew"), + [""] = get_lua_cb("close_node"), + [""] = get_lua_cb("close_node"), + [""] = get_lua_cb("preview"), + ["I"] = get_lua_cb("toggle_ignored"), + ["H"] = get_lua_cb("toggle_dotfiles"), + ["R"] = get_lua_cb("refresh"), + ["a"] = get_lua_cb("create"), + ["d"] = get_lua_cb("remove"), + ["r"] = get_lua_cb("rename"), + [""] = get_lua_cb("full_rename"), + ["x"] = get_lua_cb("cut"), + ["c"] = get_lua_cb("copy"), + ["p"] = get_lua_cb("paste"), + ["[c"] = get_lua_cb("prev_git_item"), + ["]c"] = get_lua_cb("next_git_item"), + ["-"] = get_lua_cb("dir_up"), + ["q"] = get_lua_cb("close"), + }, keybindings) end function M.window_options() diff --git a/lua/nvim-tree/lib.lua b/lua/nvim-tree/lib.lua index ce5ce7b1..7030e449 100644 --- a/lua/nvim-tree/lib.lua +++ b/lua/nvim-tree/lib.lua @@ -211,9 +211,9 @@ function M.open_file(mode, filename) end if type(ecmd) == 'string' then - api.nvim_command(string.format('%s %s', ecmd, vim.fn.fnameescape(filename))) + api.nvim_command(string.format('%s %s', ecmd, vim.fn.fnameescape(filename))) else - ecmd() + ecmd() end if mode == 'preview' then @@ -243,54 +243,22 @@ function M.change_dir(foldername) M.init(false, M.Tree.bufnr ~= nil) end -local function set_mapping(buf, key, fn) - api.nvim_buf_set_keymap(buf, 'n', key, ':lua require"nvim-tree".'..fn..'', { - nowait = true, noremap = true, silent = true - }) +local function set_mapping(buf, key, cb) + api.nvim_buf_set_keymap(buf, 'n', key, cb, { + nowait = true, noremap = true, silent = true + }) end local function set_mappings() if vim.g.nvim_tree_disable_keybindings == 1 then - return + return end local buf = M.Tree.bufnr local bindings = config.get_bindings() - local mappings = { - ['<2-LeftMouse>'] = 'on_keypress("edit")'; - ['<2-RightMouse>'] = 'on_keypress("cd")'; - [bindings.cd] = 'on_keypress("cd")'; - [bindings.edit] = 'on_keypress("edit")'; - [bindings.edit_vsplit] = 'on_keypress("vsplit")'; - [bindings.edit_split] = 'on_keypress("split")'; - [bindings.edit_tab] = 'on_keypress("tabnew")'; - [bindings.close_node] = 'on_keypress("close_node")'; - [bindings.toggle_ignored] = 'on_keypress("toggle_ignored")'; - [bindings.toggle_dotfiles] = 'on_keypress("toggle_dotfiles")'; - [bindings.refresh] = 'on_keypress("refresh")'; - [bindings.create] = 'on_keypress("create")'; - [bindings.remove] = 'on_keypress("remove")'; - [bindings.rename] = 'on_keypress("rename")'; - [bindings.full_rename] = 'on_keypress("full_rename")'; - [bindings.preview] = 'on_keypress("preview")'; - [bindings.cut] = 'on_keypress("cut")'; - [bindings.copy] = 'on_keypress("copy")'; - [bindings.paste] = 'on_keypress("paste")'; - [bindings.prev_git_item] = 'on_keypress("prev_git_item")'; - [bindings.next_git_item] = 'on_keypress("next_git_item")'; - [bindings.dir_up] = 'on_keypress("dir_up")'; - [bindings.close] = 'on_keypress("close")'; - } - - for k,v in pairs(mappings) do - if type(k) == 'table' then - for _, key in pairs(k) do - set_mapping(buf, key, v) - end - else - set_mapping(buf, k, v) - end + for key,cb in pairs(bindings) do + set_mapping(buf, key, cb) end end