avataralpha2phi

Summary

The website content provides a guide for beginners on configuring key mappings in Neovim and setting up the WhichKey plugin to enhance user experience and navigation efficiency.

Abstract

The article "Neovim for Beginners — Key Mappings and WhichKey" is part of a series aimed at helping new users become proficient with Neovim. It outlines how to set up key mappings using Neovim's Lua API and introduces the WhichKey plugin, which provides a popup with possible key bindings as the user types commands. The guide includes instructions for creating a keymaps.lua file within the after/plugin directory, configuring sensible default key mappings, and customizing the WhichKey plugin to display key bindings for various commands and motions. The article emphasizes the importance of customizing key bindings to personal preferences and introduces alternatives to WhichKey, such as legendary.nvim and key-menu.nvim. It also encourages readers to explore further Vim/Neovim articles and offers a recommendation for an AI service called ZAI.chat.

Opinions

  • The author suggests that configuring key mappings is essential for a more efficient Neovim experience.
  • WhichKey is recommended as a tool to help users remember and discover key bindings, enhancing productivity.
  • The article promotes the use of Lua for configuring Neovim, highlighting its advantages over Vimscript.
  • Customization is encouraged, with the author providing examples of how to tailor key mappings and WhichKey settings to individual needs.
  • The author provides a subjective recommendation for ZAI.chat, an AI service, as a cost-effective alternative to ChatGPT Plus (GPT-4).
  • The article implies that the provided Neovim configuration and plugins can significantly improve the user interface and usability of Neovim for beginners.

Neovim for Beginners — Key Mappings and WhichKey

Configure key mappings and the WhichKey plugin.

Neovim for Beginners — Key Mappings and WhichKey

We are going to configure a few sensible default key mappings and set up the WhichKey plugin that displays a popup with possible key bindings of the command we type.

This article is part of the Neovim for Beginners series.

The Neovim configuration files can be found in this repository.

Key Mappings

Let’s configure a few sensible key mappings using the Neovim Lua API (:h nvim_set_keymap).

Note: Check out this article for the Lua keymap APIs (:h lua-keymap).

Create a file called keymaps.lua under theafter/plugin folder.

Remember that both .vim and .lua files are loaded automatically from the runtimepath (:h rtp, :h load-plugins). Vimscript files are sourced before Lua files.

  • In Insert and Terminal mode, we use jk to transition to the Normal mode.
  • We always center the search results when moving back and forth.
  • We map k and j to gj and gk for better lines navigation when lines wrap (:h gk, :h gj).
  • We map < and > in Visual mode for better indentation. gv automatically selects the previously selected area.
  • We use the black hole register “_ (:h registers) to prevent yanking the selected text during pasting in Visual mode.
  • We use <Shift-h> and <Shift-l> to navigate between the previous and next buffer.
  • We use <ESC> to clear the highlighted search matches.
  • We use K and J to move selected lines up and down in Visual mode.
  • We use the arrow keys to resize the split window.

You can add more key mappings, or change any of these key mappings to meet your preferences.

WhichKey

Nobody is going to remember all the key bindings available in Neovim. Let’s configure the WhichKey plugin to show possible key bindings of the command when we start typing.

Installation

  • Firstly, let’s set timeoutlen to 300 milliseconds (default is 1000 milliseconds) in after/plugin/defaults.lua. This setting controls when or how soon WhichKey displays its popup.
-- Time in milliseconds to wait for a mapped sequence to complete.
opt.timeoutlen = 300
-- WhichKey
use {
   "folke/which-key.nvim",
   config = function()
     require("config.whichkey").setup()
   end,
}
  • Restart Neovim and then type:PackerInstall to install the plugin.

Configuration

Now when we press the Leader key (we configure it as <Space> in after/plugin/defaults.lua), WhichKey displays a popup with the key mappings we defined.

WhichKey Popup

We configure a few key mappings for

  • Saving current buffer.
  • Quitting Neovim.
  • Closing current buffer.
  • Closing all buffers except the current one.
  • Commonly used packer.nvim commands.
  • Opening Neogit.

Besides, when you start typing a command, WhichKey can show you the possible key bindings. There are built-in plugins for

  • marks: shows your marks when you hit one of the jump keys.
  • registers: shows the contents of your registers
  • presets: built-in key binding help for motions, text-objects, operators, windows, nav, z and g
  • spelling: spelling suggestions inside the which-key popup
WhichKey Popup for Register
WhichKey Popup for Motion

You can further customize WhichKey to fit your preferences.

Alternatives for WhichKey that you can explore.

Check out the article Learn Neovim The Practical Way for all the Vim/Neovim articles!

If you are not a Medium member yet and want to become one, click here. (A part of your subscription fee will be used to support alpha2phi.)

Neovim
Vim
Programming
Coding
Hacking
Recommended from ReadMedium
avatarAI Rabbit
Goodbye Obsidian

7 min read