简体   繁体   中英

Mapping 1 command to multiple key combinations in vim

Im using nerdtree plugin in vim. With let NERDTreeMapActivateNode="<Right>" a tree node opens up bs using the right arrow key. But I want also the left arrow key do the same. Is that even possible that 2 Keymappings can do the same thing? If so, how?

Yes, it is definitely possible to have a single command mapped to many key sequences:

nnoremap a diw
nnoremap b diw
nnoremap c diw

But this is a special case, where the mapping is done internally by a plugin and it is arbitrarily limited to one.

Apparently , the plugin sets the filetype of its buffer as nerdtree , so you might be able to deal with this in an agnostic way by mapping <Left> to <Right> only in the desired context:

" after/ftplugin/nerdtree.vim (relative to your `.vim` or `vimfiles`)
nmap <buffer> <Left> <Right>

But a better solution would probably be to use the plugin's own :help NERDTreeKeymapAPI .

created a file ~/.vim/nerdtree_plugin/mymapping.vim

call NERDTreeAddKeyMap({                                                                                                                                              
    \ 'key': '<Left>',                                                                                                                                     
    \ 'callback': 'FunctionLEFTkey',
    \ 'quickhelpText': 'echo full path of current node',                                                                                                   
            \ 'scope': 'DirNode' })

function! FunctionLEFTkey(dummy_node_arg_required_cozof_scope)
    execute 'normal' g:NERDTreeMapActivateNode                                                                                                                    
endfunction                                                                                                                                                          

call NERDTreeAddKeyMap({                                                                                                                                              
    \ 'key': '<Right>',                                                                                                                                    
    \ 'callback': 'FunctionRIGHTkey',                                                                                                                      
    \ 'quickhelpText': 'echo full path of current node',                                                                                                   
    \ 'scope': 'all' })                                                                                                                                    

function! FunctionRIGHTkey()                                                                                                                                          
    execute 'normal' g:NERDTreeMapActivateNode                                                                                                                    
endfunction
  • Pressing Leftarrow in nerdtree closes or opens a directory node.
  • Pressing Rightarrow in nerdtree closes or open a directory or opens a file.

Works as long NERDTreeMapActivateNode (in vimrc) isn't set to Leftarrow or Rightarrow key because then only this key can toggle NERDTreeMapActivateNode and NERDTreeAddKeyMap doesn't work anymore.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM