BREAKING CHANGE: make keybindings more configurable and add option to disable default
This commit is contained in:
parent
86188a4b9d
commit
10e845e01c
92
README.md
92
README.md
@ -148,54 +148,70 @@ highlight NvimTreeFolderIcon guibg=blue
|
||||
|
||||
You can disable default mappings with
|
||||
```vim
|
||||
" let nvim_tree_disable_keybindings=1
|
||||
" let g: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.
|
||||
|
||||
You can use only your mappings with
|
||||
```vim
|
||||
let g:nvim_tree_disable_default_keybindings = 1
|
||||
```
|
||||
|
||||
Default keybindings can be overriden. You can also define your own keymappings for the tree view:
|
||||
You can define your own keymaps with this syntax:
|
||||
```vim
|
||||
lua <<EOF
|
||||
local tree_cb = require'nvim-tree.config'.nvim_tree_callback
|
||||
vim.g.nvim_tree_bindings = {
|
||||
["<CR>"] = ":YourVimFunction()<cr>",
|
||||
["u"] = ":lua require'some_module'.some_function()<cr>",
|
||||
|
||||
-- default mappings
|
||||
["<CR>"] = tree_cb("edit"),
|
||||
["o"] = tree_cb("edit"),
|
||||
["<2-LeftMouse>"] = tree_cb("edit"),
|
||||
["<2-RightMouse>"] = tree_cb("cd"),
|
||||
["<C-]>"] = tree_cb("cd"),
|
||||
["<C-v>"] = tree_cb("vsplit"),
|
||||
["<C-x>"] = tree_cb("split"),
|
||||
["<C-t>"] = tree_cb("tabnew"),
|
||||
["<"] = tree_cb("prev_sibling"),
|
||||
[">"] = tree_cb("next_sibling"),
|
||||
["<BS>"] = tree_cb("close_node"),
|
||||
["<S-CR>"] = tree_cb("close_node"),
|
||||
["<Tab>"] = tree_cb("preview"),
|
||||
["I"] = tree_cb("toggle_ignored"),
|
||||
["H"] = tree_cb("toggle_dotfiles"),
|
||||
["R"] = tree_cb("refresh"),
|
||||
["a"] = tree_cb("create"),
|
||||
["d"] = tree_cb("remove"),
|
||||
["r"] = tree_cb("rename"),
|
||||
["<C-r>"] = tree_cb("full_rename"),
|
||||
["x"] = tree_cb("cut"),
|
||||
["c"] = tree_cb("copy"),
|
||||
["p"] = tree_cb("paste"),
|
||||
["y"] = tree_cb("copy_name"),
|
||||
["Y"] = tree_cb("copy_path"),
|
||||
["gy"] = tree_cb("copy_absolute_path"),
|
||||
["[c"] = tree_cb("prev_git_item"),
|
||||
["]c"] = tree_cb("next_git_item"),
|
||||
["-"] = tree_cb("dir_up"),
|
||||
["q"] = tree_cb("close"),
|
||||
{ key = {"<CR>", "o" }, cb = ":lua some_func()<cr>"}
|
||||
{ key = "<Tab>", mode = "v", cb = ":lua some_func()<cr>"}
|
||||
}
|
||||
EOF
|
||||
```
|
||||
Notes:
|
||||
- `key` can be either a string or a table of strings
|
||||
- `mode` is `n` by default if you don't specify it
|
||||
- `cb` is the command that will be called when the keymap is triggered
|
||||
|
||||
If you don't use one of the options above, your keymaps will be added to the default keymaps.
|
||||
|
||||
```vim
|
||||
lua <<EOF
|
||||
local tree_cb = require'nvim-tree.config'.nvim_tree_callback
|
||||
-- default mappings
|
||||
vim.g.nvim_tree_bindings = {
|
||||
{ key = {"<CR>", "o", "<2-LeftMouse>"}, cb = tree_cb("edit") },
|
||||
{ key = {"<2-RightMouse>", "<C-}>"}, cb = tree_cb("cd") },
|
||||
{ key = "<C-v>", cb = tree_cb("vsplit") },
|
||||
{ key = "<C-x>", cb = tree_cb("split") },
|
||||
{ key = "<C-t>", cb = tree_cb("tabnew") },
|
||||
{ key = "<", cb = tree_cb("prev_sibling") },
|
||||
{ key = ">", cb = tree_cb("next_sibling") },
|
||||
{ key = "P", cb = tree_cb("parent_node") },
|
||||
{ key = "<BS>", cb = tree_cb("close_node") },
|
||||
{ key = "<S-CR>", cb = tree_cb("close_node") },
|
||||
{ key = "<Tab>", cb = tree_cb("preview") },
|
||||
{ key = "K", cb = tree_cb("first_sibling") },
|
||||
{ key = "J", cb = tree_cb("last_sibling") },
|
||||
{ key = "I", cb = tree_cb("toggle_ignored") },
|
||||
{ key = "H", cb = tree_cb("toggle_dotfiles") },
|
||||
{ key = "R", cb = tree_cb("refresh") },
|
||||
{ key = "a", cb = tree_cb("create") },
|
||||
{ key = "d", cb = tree_cb("remove") },
|
||||
{ key = "r", cb = tree_cb("rename") },
|
||||
{ key = "<C->", cb = tree_cb("full_rename") },
|
||||
{ key = "x", cb = tree_cb("cut") },
|
||||
{ key = "c", cb = tree_cb("copy") },
|
||||
{ key = "p", cb = tree_cb("paste") },
|
||||
{ key = "y", cb = tree_cb("copy_name") },
|
||||
{ key = "Y", cb = tree_cb("copy_path") },
|
||||
{ key = "gy", cb = tree_cb("copy_absolute_path") },
|
||||
{ key = "[c", cb = tree_cb("prev_git_item") },
|
||||
{ key = "}c", cb = tree_cb("next_git_item") },
|
||||
{ key = "-", cb = tree_cb("dir_up") },
|
||||
{ key = "q", cb = tree_cb("close") },
|
||||
{ key = "g?", cb = tree_cb("toggle_help") },
|
||||
}
|
||||
EOF
|
||||
```
|
||||
All mappings are set in `normal mode`.
|
||||
You can toggle the help UI by pressing `g?`.
|
||||
|
||||
## Note
|
||||
|
||||
@ -192,6 +192,11 @@ Can be `0` or `1`. When `1`, will disable all keybindings by the plugin.
|
||||
|g:nvim_tree_bindings| as well as default bindings will not take effect.
|
||||
Default is 0
|
||||
|
||||
|g:nvim_tree_disable_default_keybindings| *g:nvim_tree_disable_default_keybindings*
|
||||
|
||||
Can be `0` or `1`. When `1`, will disable default keybindings and use only the one you defined.
|
||||
Default is 0
|
||||
|
||||
|g:nvim_tree_indent_markers| *g:nvim_tree_indent_markers*
|
||||
|
||||
Can be `0` or `1`. When `1`, will display indent markers when folders are open
|
||||
@ -346,57 +351,58 @@ INFORMATIONS *nvim-tree-info*
|
||||
|
||||
|g:nvim_tree_bindings| *g:nvim_tree_bindings*
|
||||
|
||||
You can disable default mappings with
|
||||
You can define your own keymaps with this syntax:
|
||||
>
|
||||
let nvim_tree_disable_keybindings=1
|
||||
lua <<EOF
|
||||
vim.g.nvim_tree_bindings = {
|
||||
{ key = {"<CR>", "o" }, cb = ":lua some_func()<cr>"}
|
||||
{ key = "<Tab>", mode = "v", cb = ":lua some_func()<cr>"}
|
||||
}
|
||||
EOF
|
||||
<
|
||||
But you won't be able to map any keys from the setup with nvim_tree_bindings. Use with caution.
|
||||
`key` can be either a string or a table of strings
|
||||
`mode` is `n` by default if you don't specify it
|
||||
`cb` is the command that will be called when the keymap is triggered
|
||||
|
||||
Default keybindings can be overriden. You can also define your own keymappings for the tree view:
|
||||
Default mappings:
|
||||
>
|
||||
lua <<EOF
|
||||
local tree_cb = require'nvim-tree.config'.nvim_tree_callback
|
||||
vim.g.nvim_tree_bindings = {
|
||||
["<CR>"] = ":YourVimFunction()<cr>",
|
||||
["u"] = ":lua require'some_module'.some_function()<cr>",
|
||||
|
||||
-- default mappings
|
||||
["<CR>"] = tree_cb("edit"),
|
||||
["o"] = tree_cb("edit"),
|
||||
["<2-LeftMouse>"] = tree_cb("edit"),
|
||||
["<2-RightMouse>"] = tree_cb("cd"),
|
||||
["<C-]>"] = tree_cb("cd"),
|
||||
["<C-v>"] = tree_cb("vsplit"),
|
||||
["<C-x>"] = tree_cb("split"),
|
||||
["<C-t>"] = tree_cb("tabnew"),
|
||||
["<"] = tree_cb("prev_sibling"),
|
||||
[">"] = tree_cb("next_sibling"),
|
||||
["P"] = tree_cb("parent_node"),
|
||||
["<BS>"] = tree_cb("close_node"),
|
||||
["<S-CR>"] = tree_cb("close_node"),
|
||||
["<Tab>"] = tree_cb("preview"),
|
||||
["K"] = tree_cb("first_sibling"),
|
||||
["J"] = tree_cb("last_sibling"),
|
||||
["I"] = tree_cb("toggle_ignored"),
|
||||
["H"] = tree_cb("toggle_dotfiles"),
|
||||
["R"] = tree_cb("refresh"),
|
||||
["a"] = tree_cb("create"),
|
||||
["d"] = tree_cb("remove"),
|
||||
["r"] = tree_cb("rename"),
|
||||
["<C-r>"] = tree_cb("full_rename"),
|
||||
["x"] = tree_cb("cut"),
|
||||
["c"] = tree_cb("copy"),
|
||||
["p"] = tree_cb("paste"),
|
||||
["[c"] = tree_cb("prev_git_item"),
|
||||
["]c"] = tree_cb("next_git_item"),
|
||||
["-"] = tree_cb("dir_up"),
|
||||
["q"] = tree_cb("close"),
|
||||
}
|
||||
EOF
|
||||
lua <<EOF
|
||||
local tree_cb = require'nvim-tree.config'.nvim_tree_callback
|
||||
vim.g.nvim_tree_bindings = {
|
||||
{ key = {"<CR>", "o", "<2-LeftMouse>"}, cb = tree_cb("edit") },
|
||||
{ key = {"<2-RightMouse>", "<C-}>"}, cb = tree_cb("cd") },
|
||||
{ key = "<C-v>", cb = tree_cb("vsplit") },
|
||||
{ key = "<C-x>", cb = tree_cb("split") },
|
||||
{ key = "<C-t>", cb = tree_cb("tabnew") },
|
||||
{ key = "<", cb = tree_cb("prev_sibling") },
|
||||
{ key = ">", cb = tree_cb("next_sibling") },
|
||||
{ key = "P", cb = tree_cb("parent_node") },
|
||||
{ key = "<BS>", cb = tree_cb("close_node") },
|
||||
{ key = "<S-CR>", cb = tree_cb("close_node") },
|
||||
{ key = "<Tab>", cb = tree_cb("preview") },
|
||||
{ key = "K", cb = tree_cb("first_sibling") },
|
||||
{ key = "J", cb = tree_cb("last_sibling") },
|
||||
{ key = "I", cb = tree_cb("toggle_ignored") },
|
||||
{ key = "H", cb = tree_cb("toggle_dotfiles") },
|
||||
{ key = "R", cb = tree_cb("refresh") },
|
||||
{ key = "a", cb = tree_cb("create") },
|
||||
{ key = "d", cb = tree_cb("remove") },
|
||||
{ key = "r", cb = tree_cb("rename") },
|
||||
{ key = "<C->", cb = tree_cb("full_rename") },
|
||||
{ key = "x", cb = tree_cb("cut") },
|
||||
{ key = "c", cb = tree_cb("copy") },
|
||||
{ key = "p", cb = tree_cb("paste") },
|
||||
{ key = "y", cb = tree_cb("copy_name") },
|
||||
{ key = "Y", cb = tree_cb("copy_path") },
|
||||
{ key = "gy", cb = tree_cb("copy_absolute_path") },
|
||||
{ key = "[c", cb = tree_cb("prev_git_item") },
|
||||
{ key = "}c", cb = tree_cb("next_git_item") },
|
||||
{ key = "-", cb = tree_cb("dir_up") },
|
||||
{ key = "q", cb = tree_cb("close") },
|
||||
{ key = "g?", cb = tree_cb("toggle_help") },
|
||||
}
|
||||
EOF
|
||||
<
|
||||
|
||||
All mappings are set in `normal mode`.
|
||||
|
||||
|Features| *nvim-tree-features*
|
||||
|
||||
File icons with vim-devicons.
|
||||
|
||||
@ -42,40 +42,37 @@ M.View = {
|
||||
bufhidden = 'hide';
|
||||
},
|
||||
bindings = {
|
||||
["<CR>"] = M.nvim_tree_callback("edit"),
|
||||
["o"] = M.nvim_tree_callback("edit"),
|
||||
["<2-LeftMouse>"] = M.nvim_tree_callback("edit"),
|
||||
["<2-RightMouse>"] = M.nvim_tree_callback("cd"),
|
||||
["<C-]>"] = M.nvim_tree_callback("cd"),
|
||||
["<C-v>"] = M.nvim_tree_callback("vsplit"),
|
||||
["<C-x>"] = M.nvim_tree_callback("split"),
|
||||
["<C-t>"] = M.nvim_tree_callback("tabnew"),
|
||||
["<"] = M.nvim_tree_callback("prev_sibling"),
|
||||
[">"] = M.nvim_tree_callback("next_sibling"),
|
||||
["P"] = M.nvim_tree_callback("parent_node"),
|
||||
["<BS>"] = M.nvim_tree_callback("close_node"),
|
||||
["<S-CR>"] = M.nvim_tree_callback("close_node"),
|
||||
["<Tab>"] = M.nvim_tree_callback("preview"),
|
||||
["K"] = M.nvim_tree_callback("first_sibling"),
|
||||
["J"] = M.nvim_tree_callback("last_sibling"),
|
||||
["I"] = M.nvim_tree_callback("toggle_ignored"),
|
||||
["H"] = M.nvim_tree_callback("toggle_dotfiles"),
|
||||
["R"] = M.nvim_tree_callback("refresh"),
|
||||
["a"] = M.nvim_tree_callback("create"),
|
||||
["d"] = M.nvim_tree_callback("remove"),
|
||||
["r"] = M.nvim_tree_callback("rename"),
|
||||
["<C-r>"] = M.nvim_tree_callback("full_rename"),
|
||||
["x"] = M.nvim_tree_callback("cut"),
|
||||
["c"] = M.nvim_tree_callback("copy"),
|
||||
["p"] = M.nvim_tree_callback("paste"),
|
||||
["y"] = M.nvim_tree_callback("copy_name"),
|
||||
["Y"] = M.nvim_tree_callback("copy_path"),
|
||||
["gy"] = M.nvim_tree_callback("copy_absolute_path"),
|
||||
["[c"] = M.nvim_tree_callback("prev_git_item"),
|
||||
["]c"] = M.nvim_tree_callback("next_git_item"),
|
||||
["-"] = M.nvim_tree_callback("dir_up"),
|
||||
["q"] = M.nvim_tree_callback("close"),
|
||||
["g?"] = M.nvim_tree_callback("toggle_help")
|
||||
{ key = {"<CR>", "o", "<2-LeftMouse>"}, cb = M.nvim_tree_callback("edit") },
|
||||
{ key = {"<2-RightMouse>", "<C-}>"}, cb = M.nvim_tree_callback("cd") },
|
||||
{ key = "<C-v>", cb = M.nvim_tree_callback("vsplit") },
|
||||
{ key = "<C-x>", cb = M.nvim_tree_callback("split") },
|
||||
{ key = "<C-t>", cb = M.nvim_tree_callback("tabnew") },
|
||||
{ key = "<", cb = M.nvim_tree_callback("prev_sibling") },
|
||||
{ key = ">", cb = M.nvim_tree_callback("next_sibling") },
|
||||
{ key = "P", cb = M.nvim_tree_callback("parent_node") },
|
||||
{ key = "<BS>", cb = M.nvim_tree_callback("close_node") },
|
||||
{ key = "<S-CR>", cb = M.nvim_tree_callback("close_node") },
|
||||
{ key = "<Tab>", cb = M.nvim_tree_callback("preview") },
|
||||
{ key = "K", cb = M.nvim_tree_callback("first_sibling") },
|
||||
{ key = "J", cb = M.nvim_tree_callback("last_sibling") },
|
||||
{ key = "I", cb = M.nvim_tree_callback("toggle_ignored") },
|
||||
{ key = "H", cb = M.nvim_tree_callback("toggle_dotfiles") },
|
||||
{ key = "R", cb = M.nvim_tree_callback("refresh") },
|
||||
{ key = "a", cb = M.nvim_tree_callback("create") },
|
||||
{ key = "d", cb = M.nvim_tree_callback("remove") },
|
||||
{ key = "r", cb = M.nvim_tree_callback("rename") },
|
||||
{ key = "<C->", cb = M.nvim_tree_callback("full_rename") },
|
||||
{ key = "x", cb = M.nvim_tree_callback("cut") },
|
||||
{ key = "c", cb = M.nvim_tree_callback("copy") },
|
||||
{ key = "p", cb = M.nvim_tree_callback("paste") },
|
||||
{ key = "y", cb = M.nvim_tree_callback("copy_name") },
|
||||
{ key = "Y", cb = M.nvim_tree_callback("copy_path") },
|
||||
{ key = "gy", cb = M.nvim_tree_callback("copy_absolute_path") },
|
||||
{ key = "[c", cb = M.nvim_tree_callback("prev_git_item") },
|
||||
{ key = "}c", cb = M.nvim_tree_callback("next_git_item") },
|
||||
{ key = "-", cb = M.nvim_tree_callback("dir_up") },
|
||||
{ key = "q", cb = M.nvim_tree_callback("close") },
|
||||
{ key = "g?", cb = M.nvim_tree_callback("toggle_help") }
|
||||
}
|
||||
}
|
||||
|
||||
@ -131,18 +128,33 @@ function M.setup()
|
||||
vim.bo[M.View.bufnr][k] = v
|
||||
end
|
||||
|
||||
if vim.g.nvim_tree_disable_keybindings ~= 1 then
|
||||
M.View.bindings = vim.tbl_extend(
|
||||
'force',
|
||||
M.View.bindings,
|
||||
vim.g.nvim_tree_bindings or {}
|
||||
)
|
||||
for key, cb in pairs(M.View.bindings) do
|
||||
a.nvim_buf_set_keymap(M.View.bufnr, 'n', key, cb, { noremap = true, silent = true, nowait = true })
|
||||
end
|
||||
vim.cmd "au! BufWinEnter * lua require'nvim-tree.view'._prevent_buffer_override()"
|
||||
if vim.g.nvim_tree_disable_keybindings == 1 then
|
||||
return
|
||||
end
|
||||
|
||||
vim.cmd "au! BufWinEnter * lua require'nvim-tree.view'._prevent_buffer_override()"
|
||||
local user_mappings = vim.g.nvim_tree_bindings or {}
|
||||
if vim.g.nvim_tree_disable_default_keybindings == 1 then
|
||||
M.View.bindings = user_mappings
|
||||
else
|
||||
M.View.bindings = vim.tbl_extend('force', M.View.bindings, user_mappings)
|
||||
end
|
||||
|
||||
for _, b in pairs(M.View.bindings) do
|
||||
-- TODO: remove this in a few weeks
|
||||
if type(b) == "string" then
|
||||
local warn_str = "Wrong configuration for keymaps, refer to the new documentation. User keymaps setup aborted"
|
||||
require'nvim-tree.utils'.echo_warning(warn_str)
|
||||
break
|
||||
end
|
||||
if type(b.key) == "table" then
|
||||
for _, key in pairs(b.key) do
|
||||
a.nvim_buf_set_keymap(M.View.bufnr, b.mode or 'n', key, b.cb, { noremap = true, silent = true, nowait = true })
|
||||
end
|
||||
else
|
||||
a.nvim_buf_set_keymap(M.View.bufnr, b.mode or 'n', b.key, b.cb, { noremap = true, silent = true, nowait = true })
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
local goto_tbl = {
|
||||
|
||||
Loading…
Reference in New Issue
Block a user