Home Explore Blog CI



neovim

22th chunk of `runtime/doc/map.txt`
a628e20fa1a8a359af9fa78d99801199c26b94160bedb1030000000100000fa2
 for quickfix entries
    -addr=other		  ?	Other kind of range; can use ".", "$" and "%"
				as with "lines" (this is the default for
				-count)


Incremental preview ~
                                                  *:command-preview* {nvim-api}
Commands can show an 'inccommand' (as-you-type) preview by defining a preview
handler (only from Lua, see |nvim_create_user_command()|).

Before the preview callback is executed, Nvim will temporarily disable
'cursorline' and 'cursorcolumn' to avoid highlighting issues.

The preview callback must be a Lua function with this signature: >

    function cmdpreview(opts, ns, buf)
<
where "opts" has the same form as that given to |nvim_create_user_command()|
callbacks, "ns" is the preview namespace id for highlights, and "buf" is the
buffer that your preview routine will directly modify to show the previewed
results (for "inccommand=split", or nil for  "inccommand=nosplit").

Your command preview routine must implement this protocol:

1. Modify the target buffers as required for the preview (see
   |nvim_buf_set_text()| and |nvim_buf_set_lines()|).
2. If preview buffer is provided, add necessary text to the preview buffer.
3. Add required highlights to the target buffers. If preview buffer is
   provided, add required highlights to the preview buffer as well. All
   highlights must be added to the preview namespace which is provided as an
   argument to the preview callback (see |vim.hl.range()| and
   |nvim_buf_set_extmark()| for help on how to add highlights to a namespace).
4. Return an integer (0, 1, 2) which controls how Nvim behaves as follows:
   0: No preview is shown.
   1: Preview is shown without preview window (even with "inccommand=split").
   2: Preview is shown and preview window is opened (if "inccommand=split").
      For "inccommand=nosplit" this is the same as 1.

After preview ends, Nvim discards all changes to all buffers made during the
preview and clears all highlights in the preview namespace.

Here's an example of a command to trim trailing whitespace from lines that
supports incremental command preview:
>
	-- If invoked as a preview callback, performs 'inccommand' preview by
	-- highlighting trailing whitespace in the current buffer.
	local function trim_space_preview(opts, preview_ns, preview_buf)
	  vim.cmd('hi clear Whitespace')
	  local line1 = opts.line1
	  local line2 = opts.line2
	  local buf = vim.api.nvim_get_current_buf()
	  local lines = vim.api.nvim_buf_get_lines(buf, line1 - 1, line2, false)
	  local preview_buf_line = 0

	  for i, line in ipairs(lines) do
	    local start_idx, end_idx = string.find(line, '%s+$')

	    if start_idx then
	      -- Highlight the match
	      vim.hl.range(
	        buf,
	        preview_ns,
	        'Substitute',
	        {line1 + i - 2, start_idx - 1},
	        {line1 + i - 2, end_idx},
	      )

	      -- Add lines and set highlights in the preview buffer
	      -- if inccommand=split
	      if preview_buf then
	        local prefix = string.format('|%d| ', line1 + i - 1)

	        vim.api.nvim_buf_set_lines(
	          preview_buf,
	          preview_buf_line,
	          preview_buf_line,
	          false,
	          { prefix .. line }
	        )
	        vim.hl.range(
	          preview_buf,
	          preview_ns,
	          'Substitute',
	          {preview_buf_line, #prefix + start_idx - 1},
	          {preview_buf_line, #prefix + end_idx},
	        )
	        preview_buf_line = preview_buf_line + 1
	      end
	    end
	  end

	  -- Return the value of the preview type
	  return 2
	end

	-- Trims all trailing whitespace in the current buffer.
	local function trim_space(opts)
	  local line1 = opts.line1
	  local line2 = opts.line2
	  local buf = vim.api.nvim_get_current_buf()
	  local lines = vim.api.nvim_buf_get_lines(buf, line1 - 1, line2, false)

	  local new_lines = {}
	  for i, line in ipairs(lines) do
	    new_lines[i] = string.gsub(line, '%s+$', '')
	  end
	  vim.api.nvim_buf_set_lines(buf, line1 - 1, line2,

Title: Incremental Preview Protocol and Example in Vim
Summary
This section describes the protocol for incremental command preview in Vim, detailing how to modify buffers, add highlights, and control preview behavior using the return value of the preview callback. It also presents a Lua example of a command to trim trailing whitespace with incremental preview, demonstrating how to highlight whitespace and update the preview buffer.