liuchengxu / vim-which-key

:tulip: Vim plugin that shows keybindings in popup

Home Page:https://liuchengxu.github.io/vim-which-key/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Be able to set on filetype or other conditions

Th3Whit3Wolf opened this issue · comments

I tried a few different ways to set this up but none of them worked. I'd like to have keybindings that only work for certain conditions. I have keybindings set for code compiler, runner, and testing functions. They can only be used with certain filetypes and I'd like to only see them in vim-which-key at those times. Similarly I have keybindings for git based plugins and I'd like to make them only visible when in a git repository.

I'm not exactly sure what the solution would look but I was hoping you might.

You need a hook to reset the current registered setting, e.g, BufEnter.

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_git = {'name': '+git'}
let g:which_key_map_git.s = ['Gstatus', 'git-status']

autocmd BufEnter * call SetWhichKey()

function! SetWhichKey() abort
  let git_dir = system('git rev-parse --git-dir')
  " if in a git repo
  if !v:shell_error
    let g:which_key_map.g = g:which_key_map_git
  elseif has_key(g:which_key_map, 'g')
    unlet g:which_key_map.g
  endif
endfunction