Skip to content

Instantly share code, notes, and snippets.

@gfixler
Created July 25, 2012 18:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gfixler/3177665 to your computer and use it in GitHub Desktop.
Save gfixler/3177665 to your computer and use it in GitHub Desktop.
Vim Mapkey() function: an inverse for maparg()
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