Home

Awesome

<!-- LTeX: enabled=false -->

nvim-recorder 📹

<!-- LTeX: enabled=true --> <a href="https://dotfyle.com/plugins/chrisgrieser/nvim-recorder"> <img alt="badge" src="https://dotfyle.com/plugins/chrisgrieser/nvim-recorder/shield"/></a>

Enhance the usage of macros in Neovim.

<!-- toc --> <!-- tocstop -->

Features

Setup

Installation

-- lazy.nvim
{
	"chrisgrieser/nvim-recorder",
	dependencies = "rcarriga/nvim-notify", -- optional
	opts = {}, -- required even with default settings, since it calls `setup()`
},

-- packer
use {
	"chrisgrieser/nvim-recorder",
	requires = "rcarriga/nvim-notify", -- optional
	config = function() require("recorder").setup() end,
}

Calling setup() (or lazy's opts) is required.

Configuration

-- default values
require("recorder").setup {
	-- Named registers where macros are saved (single lowercase letters only).
	-- The first register is the default register used as macro-slot after
	-- startup.
	slots = { "a", "b" },

	mapping = {
		startStopRecording = "q",
		playMacro = "Q",
		switchSlot = "<C-q>",
		editMacro = "cq",
		deleteAllMacros = "dq",
		yankMacro = "yq",
		-- ⚠️ this should be a string you don't use in insert mode during a macro
		addBreakPoint = "##",
	},

	-- Clears all macros-slots on startup.
	clear = false,

	-- Log level used for non-critical notifications; mostly relevant for nvim-notify.
	-- (Note that by default, nvim-notify does not show the levels `trace` & `debug`.)
	logLevel = vim.log.levels.INFO, -- :help vim.log.levels

	-- If enabled, only essential notifications are sent.
	-- If you do not use a plugin like nvim-notify, set this to `true`
	-- to remove otherwise annoying messages.
	lessNotifications = false,

	-- Use nerdfont icons in the status bar components and keymap descriptions
	useNerdfontIcons = true,

	-- Performance optimzations for macros with high count. When `playMacro` is
	-- triggered with a count higher than the threshold, nvim-recorder
	-- temporarily changes changes some settings for the duration of the macro.
	performanceOpts = {
		countThreshold = 100,
		lazyredraw = true, -- enable lazyredraw (see `:h lazyredraw`)
		noSystemClipboard = true, -- remove `+`/`*` from clipboard option
		autocmdEventsIgnore = { -- temporarily ignore these autocmd events
			"TextChangedI",
			"TextChanged",
			"InsertLeave",
			"InsertEnter",
			"InsertCharPre",
		},
	},

	-- [experimental] partially share keymaps with nvim-dap.
	-- (See README for further explanations.)
	dapSharedKeymaps = false,
}

If you want to handle multiple macros or use cmdheight=0, it is recommended to also set up the status line components:

Status Line Components

-- Indicates whether you are currently recording. Useful if you are using
-- `cmdheight=0`, where recording-status is not visible.
require("recorder").recordingStatus()

-- Displays non-empty macro-slots (registers) and indicates the selected ones.
-- Only displayed when *not* recording. Slots with breakpoints get an extra `#`.
require("recorder").displaySlots()

[!TIP] Use with the config clear = true to see recordings you made this session.

Example for adding the status line components to lualine:

lualine_y = {
	{ require("recorder").displaySlots },
},
lualine_z = {
	{ require("recorder").recordingStatus },
},

[!TIP] Put the components in different status line segments, so they have a different color, making the recording status more distinguishable from saved recordings

Basic Usage

[!TIP] For recursive macros (playing a macro inside a macro), you can still use the default command @a.

Advanced Usage

Performance Optimizations

Running macros with a high count can be demanding on the system and result in lags. For this reason, nvim-recorder provides some performance optimizations that are temporarily enabled when a macro with a high count is run.

Note that these optimizations do have some potential drawbacks.

Macro Breakpoints

nvim-recorder allows you to set breakpoints in your macros which can be helpful for debugging macros. Breakpoints are automatically ignored when you trigger the macro with a count.

Setting Breakpoints

Playing Macros with Breakpoints

[!TIP] You can do other things in between playing segments of the macro, like moving a few characters to the left or right. That way you can also use breakpoints to manually correct irregularities.

Ignoring Breakpoints
When you play the macro with a count (for example 50Q), breakpoints are automatically ignored.

[!TIP] Add a count of 1 (1Q) to play a macro once and still ignore breakpoints.

Shared Keybindings with nvim-dap
If you are using nvim-dap, you can use dapSharedKeymaps = true to set up the following shared keybindings:

  1. addBreakPoint maps to dap.toggle_breakpoint() outside a recording. During a recording, it adds a macro breakpoint instead.
  2. playMacro maps to dap.continue() if there is at least one DAP-breakpoint. If there is no DAP-breakpoint, plays the current macro-slot instead.

Note that this feature is experimental, since the respective API from nvim-dap is non-public and can be changed without deprecation notice.

Lazy-loading the plugin

nvim-recorder is best lazy-loaded on the mappings for startStopRecording and playMacro. However, adding the statusline components to lualine will cause the plugin to load before you start or play a recording.

To avoid this, the statusline components need to be loaded only in the plugin's config. The drawback of this method is that no component is shown when until you start or play a recording (which you can completely disregard when you set clear = true, though).

Nonetheless, the plugin is pretty lightweight (~400 lines of code), so not lazy-loading it should not have a big impact.

-- minimal config for lazy-loading with lazy.nvim
{
	"chrisgrieser/nvim-recorder",
	dependencies = "rcarriga/nvim-notify",
	keys = {
		-- these must match the keys in the mapping config below
		{ "q", desc = " Start Recording" },
		{ "Q", desc = " Play Recording" },
	},
	config = function()
		require("recorder").setup({
			mapping = {
				startStopRecording = "q",
				playMacro = "Q",
			},
		})

			local lualineZ = require("lualine").get_config().sections.lualine_z or {}
			local lualineY = require("lualine").get_config().sections.lualine_y or {}
			table.insert(lualineZ, { require("recorder").recordingStatus })
			table.insert(lualineY, { require("recorder").displaySlots })

			require("lualine").setup {
				tabline = {
					lualine_y = lualineY,
					lualine_z = lualineZ,
				},
			}
	end,
},

About the developer

In my day job, I am a sociologist studying the social mechanisms underlying the digital economy. For my PhD project, I investigate the governance of the app economy and how software ecosystems manage the tension between innovation and compatibility. If you are interested in this subject, feel free to get in touch.

Blog
I also occasionally blog about vim: Nano Tips for Vim

Profiles

<a href='https://ko-fi.com/Y8Y86SQ91' target='_blank'> <img height='36' style='border:0px;height:36px;' src='https://cdn.ko-fi.com/cdn/kofi1.png?v=3' border='0' alt='Buy Me a Coffee at ko-fi.com' /></a>

Footnotes

  1. As opposed to vim, Neovim already allows you to use Q to play the last recorded macro. Considering this, the simplified controls really only save you one keystroke for one-off macros. However, as opposed to Neovim's built-in controls, you can still keep using Q for playing the not-most-recently recorded macro.