Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Be able to set on filetype or other conditions #132

Closed
Th3Whit3Wolf opened this issue Jun 30, 2020 · 1 comment
Closed

Be able to set on filetype or other conditions #132

Th3Whit3Wolf opened this issue Jun 30, 2020 · 1 comment
Labels

Comments

@Th3Whit3Wolf
Copy link

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.

@liuchengxu
Copy link
Owner

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants