Site Logo
mnjm

NeoVim - Show macro recording when cmdheight is set to 0

Posted on 1 min

(Neo)vim

When cmdheight is set to 0 in NeoVim, the indication (recording @x) for macro recording won’t pop up anymore. This can be problematic if q is accidentally pressed, causing NeoVim to freak out at times.

I’ve found two solutions to this:

  1. Display the register returned from the vim.fn.reg_recording() call on the statusline. While this works great, it needs to be added to the statusline and can become problematic when using statusline plugins.

  2. I recall seeing RecordingEnter and RecordingLeave events in the autocmd section of the Vim help. Utilizing these, I was able to set triggers to toggle cmdheight when recording macros.

Here’s the code to set these autocmds:

local cmdheight = vim.api.nvim_get_option_value('cmdheight', {})
if cmdheight == 0 then
  vim.api.nvim_create_autocmd({'RecordingEnter'}, {
    command = "set cmdheight=1",
    desc = "Show cmdline when recording macro"
  })
  vim.api.nvim_create_autocmd({'RecordingLeave'}, {
    command = "set cmdheight=0",
    desc = "Remove cmdline when macro recording ended"
  })
end