Buffer-specific mappings are supported by vim out of the box. I am trying to implement window-specific mappings. My plan is to override the default keybinding by one that checks if a window-specific map should be executed, if so, it executes that, and if not, execues the default action for that keybinding (this is the ExecuteWindowMap
function).
I'm having trouble with setting the map, see SetWindowMap
. This function is called with an argument, a:map
, a string of keys. It needs to include pass this string of keys to a function call, so it makes sense to use noremap
; with map
these keys would get pressed instead of passed to the function! On the other hand, I can only call that function using<CR>
, which doesn't work with noremap
!
I see two possible solutions:
Use
map
and somehow escape the key sequence contained ina:map
.Use
noremap
in a way that allows a calling a function.function! SetWindowMap(map) exe "noremap " . a:map . " :call ExecuteWindowMap('" . a:map . "')<CR>" endfunction function! ExecuteWindowMap(map) if exists("w:winMaps") && has_key(w:winMaps, a:map) exe w:winMaps[a:map] else exe "unmap " . a:map exe "'a:map'" call SetWindowMap(a:map) endif endfunction function! WindowMap(map, cmd) if !exists("w:winMaps") let w:winMaps = {} endif let w:winMaps[a:map] = a:cmd call SetWindowMap(a:map) endfunction
lh#on#exit()
), and so on. For instance, you could have a look atlh#menu#map_all()
and its test case, or lh-brackets:DefineMap()
.