Is there a common way to remap keys used in specific Vim plugins, like NERDTree or TagList? I've been trying to remap some keys for the TagList plugin but I've been unable to do so. NERDTree's keys have been easier to remap, but is that because of the way the plugin is written?
-
1Yes, plugin must support user-defined mappings or remapping won't be possible in most generic way (though there are some workarounds). Unlike built-in shortcuts you can't do something like `noremap : ; | noremap ; :` and expect it to work for plugin shortcuts, though you can use something like [this](http://stackoverflow.com/questions/6391022/how-to-change-vim-insert-mode-mappings-behavior/6397405#6397405) (you need to see code until lines marked `" Create a new mapping with unique lhs` inclusive). – ZyX Jun 25 '11 at 23:20
-
Do you want to "free" the keys used by plugin or not? If not, simple map can do the trick. Otherwise, you have to rely on plugin to give you this mapping freedom. – Maxim Sloyko Jun 26 '11 at 10:28
-
The easy way is modifying the plugin source code. Search for "map", and change the key-bindings. The problem is: if you upgrade the plugin, you need to do it again. – kev Aug 05 '12 at 00:13
2 Answers
Modern plugins can be written to be re-mappable like built-in vim commands if they use <Plug>
(:help using-<Plug>
). The plugin can check if the user has mapped something to the <Plug>
command (example: <Plug>(textobj-indent-a)
), if not then it uses its own mapping. That way the user can define the maps they want and the plugin will fill in the rest.
In rkulla's answer, he shows you NERD_tree's mapping code. What they're doing is providing variables for the user to set to change their mappings:
let g:NERDTreeMapDeleteBookmark = 'A'
let g:NERDTreeMapMenu = 'B'
let g:NERDTreeMapHelp = 'C'
Those statements can be added to your vimrc to define your own maps (instead of changing the plugin code).
Other plugins will use a variable named something like no_nerdtree_maps
(different name for each plugin). If you set this variable in your vimrc, then you can create your own maps to the plugin's functions/commands.

- 10,975
- 5
- 66
- 107
Just open NERD_tree.vim in your plugins dir and you'll see a section called "Init variable calls for key mappings" with calls like:
call s:initVariable("g:NERDTreeMapDeleteBookmark", "D")
call s:initVariable("g:NERDTreeMapMenu", "m")
call s:initVariable("g:NERDTreeMapHelp", "?")
...
the second argument to s:initVariable() is the keyboard shortcut to use. Simply change it to what you want.

- 2,494
- 1
- 18
- 16
-
1I strongly suggest never change plugins' code unless you are a developer and have a fork of this plugin controlled by VCS (so you can put your changes there): everything what you do will be vanished when you update modified plugin. In your case I guess `s:initVariable` function is going to initialize variable if it is not yet defined, so all you need is to define this variable in the vimrc. – ZyX Jun 25 '11 at 23:16
-
fair enough. I thought he was suggesting that something like that wouldn't work for him because of some conflict or something. – rkulla Jun 26 '11 at 03:41
-
I have been able to remap NERDTree's keys to my own liking. But like I said, I wonder if there's a more general way to do it since I can't remap TagList's keys because they don't provide the same way to remap keys like NERDTree. – bow Jun 26 '11 at 07:32