Which-Key for Neovim

Shaik Zahid
5 min readFeb 6, 2023

--

WhichKey is a lua plugin for Neovim that displays a popup with possible key bindings of the command you started typing. Heavily inspired by the original emacs-which-key and vim-which-key.

This plugin was developed by Folke Lemaitre, who is the owner of Lazy (Package Manager) and many more plugins. He has played a major part in the contribution of extraordinary plugins to our Neovim Community.

Which-Key Installation

  • Integrate which-key in plugins.lua file so that the extension gets installed into your configuration.
-- Plugins.lua file

return {
-- Which-key Extension
"folke/which-key.nvim",
lazy = true,
}
  • The lazy keyword enables lazy-loading of this plugin. This will only load which-key when you want to search inside the editor.
  • Now invoke Lazy(Package Manager) using :Lazycommand in Normal mode to install and load which-key.
Lazy before installing Which-key
  • Install which-key using “shift + i” and we can go on to the next steps of configuring which-key.

Which-Key Configuration

  • Create a new file whichkey.lua in your lua directory.
  • It is recommended to name the file name different from the actual plugin name as it causes conflicts in the compilation of the Neovim configuration.
  • Configuring Neovim is an integral part of our configuration. You can deep dive into comments to get a better understanding of the code.
local status_ok, which_key = pcall(require, "which-key")
if not status_ok then
return
end

local setup = {
plugins = {
marks = true, -- shows a list of your marks on ' and `
registers = true, -- shows your registers on " in NORMAL or <C-r> in INSERT mode
spelling = {
enabled = true, -- enabling this will show WhichKey when pressing z= to select spelling suggestions
suggestions = 20, -- how many suggestions should be shown in the list?
},
-- the presets plugin, adds help for a bunch of default keybindings in Neovim
-- No actual key bindings are created
presets = {
operators = false, -- adds help for operators like d, y, ... and registers them for motion / text object completion
motions = true, -- adds help for motions
text_objects = true, -- help for text objects triggered after entering an operator
windows = true, -- default bindings on <c-w>
nav = true, -- misc bindings to work with windows
z = true, -- bindings for folds, spelling and others prefixed with z
g = true, -- bindings for prefixed with g
},
},
-- add operators that will trigger motion and text object completion
-- to enable all native operators, set the preset / operators plugin above
-- operators = { gc = "Comments" },
key_labels = {
-- override the label used to display some keys. It doesn't effect WK in any other way.
-- For example:
-- ["<space>"] = "SPC",
-- ["<cr>"] = "RET",
-- ["<tab>"] = "TAB",
},
icons = {
breadcrumb = "»", -- symbol used in the command line area that shows your active key combo
separator = "➜", -- symbol used between a key and it's label
group = "+", -- symbol prepended to a group
},
popup_mappings = {
scroll_down = "<c-d>", -- binding to scroll down inside the popup
scroll_up = "<c-u>", -- binding to scroll up inside the popup
},
window = {
border = "rounded", -- none, single, double, shadow
position = "bottom", -- bottom, top
margin = { 1, 0, 1, 0 }, -- extra window margin [top, right, bottom, left]
padding = { 2, 2, 2, 2 }, -- extra window padding [top, right, bottom, left]
winblend = 0,
},
layout = {
height = { min = 4, max = 25 }, -- min and max height of the columns
width = { min = 20, max = 50 }, -- min and max width of the columns
spacing = 3, -- spacing between columns
align = "left", -- align columns left, center or right
},
ignore_missing = true, -- enable this to hide mappings for which you didn't specify a label
hidden = { "<silent>", "<cmd>", "<Cmd>", "<CR>", "call", "lua", "^:", "^ " }, -- hide mapping boilerplate
show_help = true, -- show help message on the command line when the popup is visible
triggers = "auto", -- automatically setup triggers
-- triggers = {"<leader>"} -- or specify a list manually
triggers_blacklist = {
-- list of mode / prefixes that should never be hooked by WhichKey
-- this is mostly relevant for key maps that start with a native binding
-- most people should not need to change this
i = { "j", "k" },
v = { "j", "k" },
},
}

local opts = {
mode = "n", -- NORMAL mode
prefix = "<leader>",
buffer = nil, -- Global mappings. Specify a buffer number for buffer local mappings
silent = true, -- use `silent` when creating keymaps
noremap = true, -- use `noremap` when creating keymaps
nowait = true, -- use `nowait` when creating keymaps
}

local mappings = {

["k"] = { "<cmd>bdelete<CR>", "Kill Buffer" }, -- Close current file
["p"] = { "<cmd>Lazy<CR>", "Plugin Manager" }, -- Invoking plugin manager
["q"] = { "<cmd>wqall!<CR>", "Quit" }, -- Quit Neovim after saving the file
["w"] = { "<cmd>w!<CR>", "Save" }, -- Save current file

}

which_key.setup(setup)
which_key.register(mappings, opts)
  • The setup and opts variables maintain the look and feel of our which-key plugin. Whereas the mappings represents the actual content of which-key.

Source Which-key to Main Configuration File

  • Include whichkey configuration file into our main configuation file
require "keymaps"
require "options"
require "lazy-config"
require "whichkey" -- Added this line to our main configuration

Keyboard Shortcuts

  • Space + k :- Close the Current Editor
  • Space + p :- Invoke Lazy (Package Manager)
  • Space + w :- Save the Current File
  • Space + q :- Quit Neovim

Folder Structure

  • Added which-key configuration implemented in whichkey.lua file inside lua directory.

Neovim From Scratch

  • If you want a complete installation and configuration of Neovim from Scratch, then you can head over to my NEOVIM SERIES.
  • This series is updated regularly, with updates and inclusion of newer plugins which improves the wholesome IDE experience of Neovim.

--

--