Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/liuchengxu/vim-which-key

:tulip: Vim plugin that shows keybindings in popup
https://github.com/liuchengxu/vim-which-key

neovim space-vim vim vim-plugin

Last synced: about 2 months ago
JSON representation

:tulip: Vim plugin that shows keybindings in popup

Awesome Lists containing this project

README

        

# vim-which-key

* [Introduction](#introduction)
* [Pros.](#pros)
* [Installation](#installation)
* [Plugin Manager](#plugin-manager)
* [Package management](#package-management)
* [Vim 8](#vim-8)
* [NeoVim](#neovim)
* [Requirement](#requirement)
* [Usage](#usage)
* [`timeoutlen`](#timeoutlen)
* [Special keys](#special-keys)
* [Configuration](#configuration)
* [Minimal Configuration](#minimal-configuration)
* [Example](#example)
* [Hide statusline](#hide-statusline)
* [Commands](#commands)
* [Options](#options)
* [FAQ](#faq)
* [How to map some special keys like ``?](#how-to-map-some-special-keys-like-bs)
* [Credit](#credit)

## Introduction

vim-which-key is vim port of [emacs-which-key](https://github.com/justbur/emacs-which-key) that displays available keybindings in popup.

[emacs-which-key](https://github.com/justbur/emacs-which-key) started as a rewrite of [guide-key](https://github.com/kai2nenobu/guide-key), very likely, [vim-which-key](https://github.com/liuchengxu/vim-which-key) heavily rewrote [vim-leader-guide](https://github.com/hecal3/vim-leader-guide). The features of vim-which-key has evolved a lot since then.





Vim config in the screenshot is space-vim.

## Pros.

- Better UI, vim's `popup` and neovim's `floating_win` are supported.
- Show all mappings following a prefix, e.g., ``, ``, etc.
- Instant response for your every single input.
- Dynamic update on every call.
- Define group names and arbitrary descriptions.

## Installation

### Plugin Manager

Assuming you are using [vim-plug](https://github.com/junegunn/vim-plug):

```vim
Plug 'liuchengxu/vim-which-key'

" On-demand lazy load
Plug 'liuchengxu/vim-which-key', { 'on': ['WhichKey', 'WhichKey!'] }

" To register the descriptions when using the on-demand load feature,
" use the autocmd hook to call which_key#register(), e.g., register for the Space key:
" autocmd! User vim-which-key call which_key#register('', 'g:which_key_map')
```

For other plugin managers please refer to their document for more details.

### Package management

#### Vim 8

```bash
$ mkdir -p ~/.vim/pack/git-plugins/start
$ git clone https://github.com/liuchengxu/vim-which-key.git --depth=1 ~/.vim/pack/git-plugins/start/vim-which-key
```

#### NeoVim

```bash
$ mkdir -p ~/.local/share/nvim/site/pack/git-plugins/start
$ git clone https://github.com/liuchengxu/vim-which-key.git --depth=1 ~/.local/share/nvim/site/pack/git-plugins/start/vim-which-key
```

## Requirement

vim-which-key requires option `timeout` is on, see `:h timeout`.

Since `timeout` is on by default, all you need is not to `set notimeout` in your `.vimrc`.

## Usage

### `timeoutlen`

Let's say SPC is your leader key and you use it to trigger vim-which-key:

```vim
nnoremap :WhichKey ''
```

After pressing leader the guide buffer will pop up when there are no further keystrokes within `timeoutlen`.

```vim
" By default timeoutlen is 1000 ms
set timeoutlen=500
```

Pressing other keys within `timeoutlen` will either complete the mapping or open a subgroup. In the screenshot above SPCb will open up the buffer menu.

Please note that no matter which mappings and menus you configure, your original leader mappings will remain unaffected. The key guide is an additional layer. It will only activate, when you do not complete your input during the timeoutlen duration.

### Special keys

- Use BS to show the upper level mappings.

### Configuration

- For neovim, [nvim-whichkey-setup.lua](https://github.com/AckslD/nvim-whichkey-setup.lua) provides a wrapper around vim-which-key to simplify configuration in lua.
It also solves issues (see #126) when the mapped command is more complex and makes it easy to also map `localleader`.

#### Minimal Configuration

`:WhichKey` and `:WhichKeyVisual` are the primary way of interacting with this plugin.

Assuming your `leader` and `localleader` key are `` and `,`, respectively, even no description dictionary has been registered, all `` and `,` related mappings will be displayed regardless.

```vim
let g:mapleader = "\"
let g:maplocalleader = ','
nnoremap :WhichKey ''
nnoremap :WhichKey ','
```

The raw content displayed is normally not adequate to serve as a cheatsheet. See the following section for configuring it properly.

If no description dictionary is available, the right-hand-side of all mappings will be displayed:

The dictionary configuration is necessary to provide group names or a description text:

```vim
let g:which_key_map = {}
let g:which_key_map['w'] = {
\ 'name' : '+windows' ,
\ 'w' : ['w' , 'other-window'] ,
\ 'd' : ['c' , 'delete-window'] ,
\ '-' : ['s' , 'split-window-below'] ,
\ '|' : ['v' , 'split-window-right'] ,
\ '2' : ['v' , 'layout-double-columns'] ,
\ 'h' : ['h' , 'window-left'] ,
\ 'j' : ['j' , 'window-below'] ,
\ 'l' : ['l' , 'window-right'] ,
\ 'k' : ['k' , 'window-up'] ,
\ 'H' : ['5<' , 'expand-window-left'] ,
\ 'J' : [':resize +5' , 'expand-window-below'] ,
\ 'L' : ['5>' , 'expand-window-right'] ,
\ 'K' : [':resize -5' , 'expand-window-up'] ,
\ '=' : ['=' , 'balance-window'] ,
\ 's' : ['s' , 'split-window-below'] ,
\ 'v' : ['v' , 'split-window-below'] ,
\ '?' : ['Windows' , 'fzf-window'] ,
\ }
call which_key#register('', "g:which_key_map")
```

If you wish to hide a mapping from the menu set it's description to `'which_key_ignore'`. Useful for instance, to hide a list of [1-9] window swapping mappings. For example the below mapping will not be shown in the menu.

```vim
nnoremap 1 :1wincmd w
let g:which_key_map.1 = 'which_key_ignore'
```

If you want to hide a group of non-top level mappings, set the `name` to `'which_key_ignore'`. For example,

```vim
nnoremap _a :echom '_a'
nnoremap _b :echom '_b'
let g:which_key_map['_'] = { 'name': 'which_key_ignore' }
```

If you want to hide all mappings outside of the elements of the description dictionary, use: `let g:which_key_ignore_outside_mappings = 1`.

#### Example

You can configure a Dict for each prefix so that the display is more readable.

To make the guide pop up **Register the description dictionary for the prefix first**. Assuming `Space` is your leader key and the Dict for configuring `Space` is `g:which_key_map`:

```vim
nnoremap :WhichKey ''
vnoremap :WhichKeyVisual ''

call which_key#register('', "g:which_key_map")
```

The above registers the same description dictionary for both normal and visual modes. To use a separate description dictionary for each mode: add a third argument specifying which mode:
```vim
call which_key#register('', "g:which_key_map", 'n')
call which_key#register('', "g:which_key_map_visual", 'v')
```

The next step is to add items to `g:which_key_map`:

```vim
" Define prefix dictionary
let g:which_key_map = {}

" Second level dictionaries:
" 'name' is a special field. It will define the name of the group, e.g., leader-f is the "+file" group.
" Unnamed groups will show a default empty string.

" =======================================================
" Create menus based on existing mappings
" =======================================================
" You can pass a descriptive text to an existing mapping.

let g:which_key_map.f = { 'name' : '+file' }

nnoremap fs :update
let g:which_key_map.f.s = 'save-file'

nnoremap fd :e $MYVIMRC
let g:which_key_map.f.d = 'open-vimrc'

nnoremap oq :copen
nnoremap ol :lopen
let g:which_key_map.o = {
\ 'name' : '+open',
\ 'q' : 'open-quickfix' ,
\ 'l' : 'open-locationlist',
\ }

" =======================================================
" Create menus not based on existing mappings:
" =======================================================
" Provide commands(ex-command, // mapping, etc.)
" and descriptions for the existing mappings.
"
" Note:
" Some complicated ex-cmd may not work as expected since they'll be
" feed into `feedkeys()`, in which case you have to define a decicated
" Command or function wrapper to make it work with vim-which-key.
" Ref issue #126, #133 etc.
let g:which_key_map.b = {
\ 'name' : '+buffer' ,
\ '1' : ['b1' , 'buffer 1'] ,
\ '2' : ['b2' , 'buffer 2'] ,
\ 'd' : ['bd' , 'delete-buffer'] ,
\ 'f' : ['bfirst' , 'first-buffer'] ,
\ 'h' : ['Startify' , 'home-buffer'] ,
\ 'l' : ['blast' , 'last-buffer'] ,
\ 'n' : ['bnext' , 'next-buffer'] ,
\ 'p' : ['bprevious' , 'previous-buffer'] ,
\ '?' : ['Buffers' , 'fzf-buffer'] ,
\ }

let g:which_key_map.l = {
\ 'name' : '+lsp',
\ 'f' : ['spacevim#lang#util#Format()' , 'formatting'] ,
\ 'r' : ['spacevim#lang#util#FindReferences()' , 'references'] ,
\ 'R' : ['spacevim#lang#util#Rename()' , 'rename'] ,
\ 's' : ['spacevim#lang#util#DocumentSymbol()' , 'document-symbol'] ,
\ 'S' : ['spacevim#lang#util#WorkspaceSymbol()' , 'workspace-symbol'] ,
\ 'g' : {
\ 'name': '+goto',
\ 'd' : ['spacevim#lang#util#Definition()' , 'definition'] ,
\ 't' : ['spacevim#lang#util#TypeDefinition()' , 'type-definition'] ,
\ 'i' : ['spacevim#lang#util#Implementation()' , 'implementation'] ,
\ },
\ }
```

The guide will be up to date at all times. Native vim mappings will always take precedence over dictionary-only mappings.

It is possible to call the guide for keys other than `leader`:

```vim
nnoremap :WhichKey ','
vnoremap :WhichKeyVisual ','
```

- Refer to [space-vim](https://github.com/liuchengxu/space-vim/blob/master/core/autoload/spacevim/map/leader.vim) for more detailed example.

#### Hide statusline

Since the theme of provided statusline is not flexible and all the information has been echoed already, I prefer to hide it.

```vim
autocmd! FileType which_key
autocmd FileType which_key set laststatus=0 noshowmode noruler
\| autocmd BufLeave set laststatus=2 showmode ruler
```

### Commands

See more details about commands and options via `:h vim-which-key`.

| Command | Description |
| :------------------- | :---------------------------------------------------: |
| `:WhichKey {prefix}` | Open the guide window for the given prefix |
| `:WhichKey! {dict}` | Open the guide window for a given dictionary directly |

### Options

| Variable | Default | Description |
| :--------------------- | :--------: | :-----------------------------------------: |
| `g:which_key_vertical` | 0 | show popup vertically |
| `g:which_key_position` | `botright` | split a window at the bottom |
| `g:which_key_hspace` | 5 | minimum horizontal space between columns |
| `g:which_key_centered` | 1 | make all keybindings centered in the middle |

### FAQ

#### How to map some special keys like ``?

See [#178](https://github.com/liuchengxu/vim-which-key/issues/178).

#### How to set keybindings on filetype or other condition?

You may use BufEnter/BufLeave [#132](https://github.com/liuchengxu/vim-which-key/issues/132), a `dictionary-function` [#209](https://github.com/liuchengxu/vim-which-key/pull/209), or *[experimental]* setup per buffer [#48](https://github.com/liuchengxu/vim-which-key/pull/48).

#### How to map lua functions?

This is possible via [nvim-whichkey-setup.lua](https://github.com/AckslD/nvim-whichkey-setup.lua). For example, if one wanted to map [spectre's](https://github.com/windwp/nvim-spectre) `open` to `S`, which in vimscipt would be `nnoremap S lua require('spectre').open()`, one could use the following in one's `init.vim`:

```vim
lua<', 'Search'},
}

wk.register_keymap('leader', keymap)
EOF
```

NB that keymaps can only be registered once. The entirety of one's `vim-which-key` configuration must be ported to [nvim-whichkey-setup.lua](https://github.com/AckslD/nvim-whichkey-setup.lua) in order to enable this functionality.

## Credit

- [vim-leader-guide](https://github.com/hecal3/vim-leader-guide)