起因是因为想用 which-key这个插件. 但是如果想要用这个就需要为每一个快捷键添加一个描述. 每次添加快捷键需要用两行代码:
nnoremap <silent> <leader>wd :close<CR>
let g:which_key_map.w.d = 'delete'
感觉十分不方便, 就想写一个函数.
function! keymap#nnore_leader_key_map(key_sequence, map, description)
let len = len(a:key_sequence)
let index = 0
let current_map = g:which_key_map
echo current_map
while index < len
let key = a:key_sequence[index]
if index < len -1
""" 创建中间的节点
if !has_key(current_map, key)
let current_map[key] = {}
endif
let current_map = current_map[key]
else
" 写入描述
let current_map[key] = a:description
endif
let index = index + 1
endwhile
nnoremap <silent> "<leader>".key_sequence map
endfunction
前面都正常, 但是 nnoremap 命令不生效. 请问这里要怎么修改呢?
1
Guaidaodl OP 发现是自己理解有误, nnoremap 是命令, 应该先拼出命令再用 execute 就好了.
``` let cmd = "nnoremap <silent> <leader>" . a:key_sequence . " " . a:map execute cmd ``` |