Skip to content

Commit c384ab7

Browse files
authored
Merge pull request #5 from kyazdani42/user-keybindings
Feature: add option for user keybindings
2 parents c003626 + ff860ec commit c384ab7

File tree

5 files changed

+56
-9
lines changed

5 files changed

+56
-9
lines changed

README.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,20 @@ let g:lua_tree_show_icons = {
3131
"1 by default, notice that if 'files' is 1, it will only display
3232
"if web-devicons is installed and on your runtimepath
3333
34+
" You can edit keybindings be defining this variable
35+
" You don't have to define all keys.
36+
" NOTE: the 'edit' key will wrap/unwrap a folder and open a file
37+
let g:lua_tree_bindings = {
38+
\ 'edit': '<CR>',
39+
\ 'edit_vsplit': '<C-v>',
40+
\ 'edit_split': '<C-x>',
41+
\ 'edit_tab': '<C-t>',
42+
\ 'cd': '.',
43+
\ 'create': 'a',
44+
\ 'remove': 'd',
45+
\ 'rename': 'r'
46+
\ }
47+
3448
nnoremap <C-n> :LuaTreeToggle<CR>
3549
nnoremap <leader>r :LuaTreeRefresh<CR>
3650
nnoremap <leader>n :LuaTreeFindFile<CR>

doc/nvim-tree-lua.txt

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,22 @@ INFORMATIONS *nvim-tree-info*
109109
- Double left click acts like '<CR>'
110110
- Double right click acts like '.'
111111

112+
|g:lua_tree_bindings| *g:lua_tree_bindings*
113+
114+
you can change default keybindings by defining this variable.
115+
default keybindings will be applied to undefined keys.
116+
>
117+
let g:lua_tree_bindings = {
118+
\ edit: '<cr>',
119+
\ edit_vsplit: '<c-v>',
120+
\ edit_split: '<c-x>',
121+
\ edit_tab: '<c-t>',
122+
\ cd: '.',
123+
\ create: 'a',
124+
\ remove: 'd',
125+
\ rename: 'r'
126+
\ }
127+
112128
|Features| *nvim-tree-features*
113129

114130
File icons with vim-devicons.

doc/tags

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
:LuaTreeToggle nvim-tree-lua.txt /*:LuaTreeToggle*
44
g:lua_tree_auto_close nvim-tree-lua.txt /*g:lua_tree_auto_close*
55
g:lua_tree_auto_open nvim-tree-lua.txt /*g:lua_tree_auto_open*
6+
g:lua_tree_bindings nvim-tree-lua.txt /*g:lua_tree_bindings*
67
g:lua_tree_follow nvim-tree-lua.txt /*g:lua_tree_follow*
78
g:lua_tree_ignore nvim-tree-lua.txt /*g:lua_tree_ignore*
89
g:lua_tree_show_icons nvim-tree-lua.txt /*g:lua_tree_show_icons*

lua/lib/config.lua

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,24 @@ local colors = {
2727
dark_red = get('terminal_color_9', 'DarkRed'),
2828
}
2929

30+
local keybindings = get('lua_tree_bindings', {});
31+
32+
local bindings = {
33+
edit = keybindings.edit or '<CR>',
34+
edit_vsplit = keybindings.edit_vsplit or '<C-v>',
35+
edit_split = keybindings.edit_split or '<C-x>',
36+
edit_tab = keybindings.edit_tab or '<C-t>',
37+
cd = keybindings.cd or '.',
38+
create = keybindings.create or 'a',
39+
remove = keybindings.remove or 'd',
40+
rename = keybindings.remove or 'r',
41+
}
42+
3043
return {
3144
SHOW_FOLDER_ICON = SHOW_FOLDER_ICON,
3245
SHOW_FILE_ICON = SHOW_FILE_ICON,
3346
SHOW_GIT_ICON = SHOW_GIT_ICON,
34-
colors = colors
47+
colors = colors,
48+
bindings = bindings
3549
}
3650

lua/lib/winutils.lua

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ local highlight = libformat.highlight_buffer
88
local stateutils = require 'lib/state'
99
local get_tree = stateutils.get_tree
1010

11+
local bindings = require 'lib/config'.bindings
12+
1113
local function get_buf()
1214
local regex = '.*'..BUF_NAME..'$';
1315

@@ -132,16 +134,16 @@ local function set_mappings()
132134
if not buf then return end
133135

134136
local mappings = {
135-
['<CR>'] = 'open_file("edit")';
136137
['<2-LeftMouse>'] = 'open_file("edit")';
137138
['<2-RightMouse>'] = 'open_file("chdir")';
138-
['<C-v>'] = 'open_file("vsplit")';
139-
['<C-x>'] = 'open_file("split")';
140-
['<C-t>'] = 'open_file("tabnew")';
141-
['.'] = 'open_file("chdir")';
142-
a = 'edit_file("create")';
143-
d = 'edit_file("remove")';
144-
r = 'edit_file("rename")';
139+
[bindings.edit] = 'open_file("edit")';
140+
[bindings.edit_vsplit] = 'open_file("vsplit")';
141+
[bindings.edit_split] = 'open_file("split")';
142+
[bindings.edit_tab] = 'open_file("tabnew")';
143+
[bindings.cd] = 'open_file("chdir")';
144+
[bindings.create] = 'edit_file("create")';
145+
[bindings.remove] = 'edit_file("remove")';
146+
[bindings.rename] = 'edit_file("rename")';
145147
}
146148

147149
for k,v in pairs(mappings) do

0 commit comments

Comments
 (0)