Created
July 25, 2012 18:19
-
-
Save gfixler/3177665 to your computer and use it in GitHub Desktop.
Vim Mapkey() function: an inverse for maparg()
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function! Mapkey (keys, mode) | |
" Inverse of maparg() | |
" Pass in a key sequence and the first letter of a Vim mode. | |
" Returns key mapping mapped to it in that mode, else '' if none. | |
" <Keys> - e.g. "<Space>" and "<CR>" - are matched case-insensitively, | |
" as are things before dashes, e.g. the "C-S-" part of "<C-S-A>". | |
" Chorded "<keys>" are not handled properly yet (i.e. "<C-Space>") | |
" Example: | |
" :nnoremap <Tab> :bn<CR> | |
" :call Mapkey(':bn<CR>', 'n') | |
" " returns <Tab> | |
redir => mappings | silent! map | redir END | |
let keys = substitute(a:keys, '<\(\w*\|.*-\)\=\(\w*\)\=>', '<\L\1\e\2>', 'g') | |
for map in split(mappings, '\n') | |
let seq = matchstr(map, '\s\+\zs\S*') | |
if substitute(maparg(seq,a:mode), '<\(\w*\|.*-\)\=\(\w*\)\=>', '<\L\1\e\2>', 'g') == keys | |
return seq | |
endif | |
endfor | |
return '' | |
endfunction |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment