Skip to content

Instantly share code, notes, and snippets.

@louwers
Last active May 25, 2016 20:20
Show Gist options
  • Save louwers/7ab67f35d5161b23b1d45c622e9f6f53 to your computer and use it in GitHub Desktop.
Save louwers/7ab67f35d5161b23b1d45c622e9f6f53 to your computer and use it in GitHub Desktop.
Window-specific keybindings in vim
function! SetWindowMap(map)
exe "noremap <silent> " . a:map . " :call ExecuteWindowMap('" . a:map . "')<CR>"
endfunction
function! ExecuteWindowMap(map)
if exists("w:winMaps") && has_key(w:winMaps, a:map)
call feedkeys(w:winMaps[a:map])
else
exe "unmap " . a:map
let escaped = escape(a:map, "<") "may cause unexpected behavior when mapping "<"
silent call feedkeys(escaped)
call SetWindowMap(a:map)
endif
endfunction
"Usage:
":call WindowMap("<F2>", ":echo \"test\"<CR>")
function! WindowMap(map, cmd)
if !exists("w:winMaps")
let w:winMaps = {}
endif
let w:winMaps[a:map] = a:cmd
call SetWindowMap(a:map)
endfunction
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment