Show What Keys Neovim Thinks You Pressed
Use Ctrlv in Insert mode to see the Neovim code for the next key or key combo you press
Introduction
I use a moonlander keyboard. One reason I got it is the "layers" feature that lets me setup keys with additional functionality. For example, if I hold down the Enter and press g the keyboard sends Option + Shift + g instead. I have Neovim configured to open my Grimoire when it receives that combo.
My keyboard mappings are kind of all over the place. I can always go look at the configuration to find out what a particular key does on a specific layer. That's not necessary, though. Neovim has a built in feature that displays what's pressed. It looks like this:
The Steps
Switch to insert mode with i if you aren't already there
Press Ctrl + v which temporarily outputs a `^`` character
Press a key or key combination to see Neovim's representation of what you pressed
The Output
When I hit Enter + g (and the keyboard sends Option + Shift + g), Neovim displays:
Code
<M-G>
Some things to note about that:
Internally, Neovim doesn't use Mac's Option keys. It uses a Meta key signal instead. I have my terminal setup to translate Option to Meta automatically (see below). The net effet of that is that we see `
The "G" is upper case in the output because the Shift key was part of the signal sent. The same signal could be represented with an explict Shift and a lowercase "g". So, instead of `
Usage
The reason I need to figure out the details of the keypresses is to set up hotkey triggers in my config. For example, if I wanted Enter + a to print "Hello, World" whenever I pressed it I'd used the techniqe above to fiugre out that Neovim sees it as:
Code
<M-A>
I can use that to make the following to print out "Hello, World" whenever I hit the key in Normal mode.
~/.config/nvim/after/plugin/hello-world.lua
vim.keymap.set('n', '<M-A>', function()
print("Hello, World")
end, { desc = "Prints Hello World" })
Wrapping Up
This is one of those small but nice reductions of friction. Since it's so easy to see what my key mappings are I'm a lot more likely to use them. And, I should point out, things aren't always so stright forward, I've got Enter + j mapped to Option + Shift + F11 to avoid conflicting with other mappings. Figuring out what's what quickly is, if you'll forgive the pun, key.
References
-
iTerm2 Set Option To Meta
Under: iTerm -< Preferences -< Profiles -< Keys
Switch "Left Option Key" to "Esc+"
That's what lets Neovim see Option as Meta
-
This is where I learned the Neovim incantation to see what it sees when keys are pressed
-
This is the current state of my moonlander
-
And this is what the moonlander looks like if you haven't seen this particular space keyboard before