Home Explore Blog CI



neovim

23th chunk of `runtime/doc/map.txt`
0ee037b2efd87994e73933c804ed9b7064f091b1738108960000000100000e74
 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, false, new_lines)
	end

	-- Create the user command
	vim.api.nvim_create_user_command(
	  'TrimTrailingWhitespace',
	  trim_space,
	  { nargs = '?', range = '%', addr = 'lines', preview = trim_space_preview }
	)
<


Special cases ~
					*:command-bang* *:command-bar*
					*:command-register* *:command-buffer*
					*:command-keepscript*
There are some special cases as well:

	-bang	    The command can take a ! modifier (like :q or :w)
	-bar	    The command can be followed by a "|" and another command.
		    A "|" inside the command argument is not allowed then.
		    Also checks for a " to start a comment.
	-register   The first argument to the command can be an optional
		    register name (like :del, :put, :yank).
	-buffer	    The command will only be available in the current buffer.
	-keepscript Do not use the location of where the user command was
		    defined for verbose messages, use the location of where
		    the user command was invoked.

In the cases of the -count and -register attributes, if the optional argument
is supplied, it is removed from the argument list and is available to the
replacement text separately.
Note that these arguments can be abbreviated, but that is a deprecated
feature.  Use the full name for new scripts.


Replacement text ~

The replacement text {repl} for a user defined command is scanned for special
escape sequences, using <...> notation.  Escape sequences are replaced with
values from the entered command line, and all other text is copied unchanged.
The resulting string is executed as an Ex command.  To avoid the replacement
use <lt> in place of the initial <.  Thus to include "<bang>" literally use
"<lt>bang>".

The valid escape sequences are

						*<line1>*
	<line1>	The starting line of the command range.
						*<line2>*
	<line2>	The final line of the command range.
						*<range>*
	<range> The number of items in the command range: 0, 1 or 2
						*<count>*
	<count>	Any count supplied (as described for the '-range'
		and '-count' attributes).
						*<bang>*
	<bang>	(See the '-bang' attribute) Expands to a ! if the
		command was executed with a ! modifier, otherwise
		expands to nothing.
					*<mods>* *<q-mods>* *:command-modifiers*
	<mods>  The command modifiers, if specified. Otherwise, expands to
		nothing. Supported modifiers are |:aboveleft|, |:belowright|,
		|:botright|, |:browse|, |:confirm|, |:hide|, |:horizontal|,
		|:keepalt|, |:keepjumps|, |:keepmarks|, |:keeppatterns|,
		|:leftabove|, |:lockmarks|, |:noautocmd|, |:noswapfile|,
		|:rightbelow|, |:sandbox|, |:silent|, |:tab|, |:topleft|,
		|:unsilent|, |:verbose|, and |:vertical|.
		Note that |:filter| is not supported.
		Examples: >
		    command!

Title: Completing the Whitespace Trimming Command and Special Command Cases in Vim
Summary
This section completes the example for trimming whitespace, showing how the functions are used to create a user command in Vim. It then transitions to discuss special cases in user command definition such as -bang, -bar, -register, -buffer, and -keepscript. Finally, it describes the replacement text feature, detailing escape sequences like <line1>, <line2>, <range>, <count>, <bang>, and <mods> that allow dynamic substitution in user-defined commands.