-
-
Notifications
You must be signed in to change notification settings - Fork 65
Set keybindings based on filetype or other condition
rene-descartes2021 edited this page Jun 8, 2022
·
1 revision
Here is an example using a dictionary-function
, which is a function that returns a dictionary, which maps keybindings to descriptions. The example shows how the <Space>g
keymapping can be changed based on filetype.
An alternative approach to adjusting the registered description dictionary (registered with which_key#register()) based on a condition is to use BufEnter/BufLeave #138.
set nocompatible
call plug#begin('~/.vim/plugged')
Plug 'liuchengxu/vim-which-key'
call plug#end()
let mapleader = "\<Space>"
nnoremap <silent> <leader> :<c-u>WhichKey '<Space>'<CR>
nnoremap <silent> <localleader> :<c-u>WhichKey ','<CR>
call which_key#register('<Space>', "g:which_key_map")
let g:which_key_map = {}
let g:which_key_map.c = { 'name' : '+coc' }
let g:which_key_map.c.g = { 'name' : '+goto'}
let g:which_key_map.c.g.d = [ '<Plug>(coc-definition)', "coc-definition"]
let g:which_key_map.g = {'name': '+git'}
let g:which_key_map.g = ['Gstatus', 'git-status']
" ~/.vim/ftplugin/cs.vim
" A `Dictionary-function` containing vim-which-key mappings
function! s:MyHotkeyDictionaryFunction()
let s:keep_mapping = get(s:, 'keep_mapping', deepcopy(g:which_key_map['g']))
if &filetype ==# 'cs'
let l:key_map = {
\ 'name': '+goto',
\ 'd': ['<Plug>(omnisharp_go_to_definition)', 'definition'],
\ 'f': {
\ 'name': '+find',
\ 't': ['<Plug>(omnisharp_find_type)' , 'type-definition'],
\ 'i': ['<Plug>(omnisharp_find_implementations)', 'implementation'],
\ 's': ['<Plug>(omnisharp_find_symbol)' , 'symbol'],
\ },
\ }
return l:key_map
else
return s:keep_mapping
endif
endfunction
" Integrate dictionary-function with dictionary used in earlier which_key#register()
let s:leader = g:which_key_map
let s:leader['g'] = function('s:MyHotkeyDictionaryFunction')