Home Explore Blog CI



neovim

21th chunk of `runtime/doc/map.txt`
a4fab84afe4a186672046496d6fae048bd63a9940b817fe60000000100000fa2
 function arguments are:
	ArgLead		the leading portion of the argument currently being
			completed on
	CmdLine		the entire command line
	CursorPos	the cursor position in it (byte index)
The function may use these for determining context.  For the "custom"
argument, it is not necessary to filter candidates against the (implicit
pattern in) ArgLead.  Vim will filter the candidates with its regexp engine
after function return, and this is probably more efficient in most cases. If
'wildoptions' contains "fuzzy", then the candidates will be filtered using
|fuzzy-matching|.  For the "customlist" argument, Vim will not
filter the returned completion candidates and the user supplied function
should filter the candidates.

The following example lists user names to a Finger command >
    :com -complete=custom,ListUsers -nargs=1 Finger !finger <args>
    :fun ListUsers(A,L,P)
    :    return system("cut -d: -f1 /etc/passwd")
    :endfun

The following example completes filenames from the directories specified in
the 'path' option: >
    :com -nargs=1 -bang -complete=customlist,EditFileComplete
			\ EditFile edit<bang> <args>
    :fun EditFileComplete(A,L,P)
    :    return split(globpath(&path, a:A), "\n")
    :endfun
<
This example does not work for file names with spaces!


Range handling ~
				*E177* *E178* *:command-range* *:command-count*
By default, user-defined commands do not accept a line number range.  However,
it is possible to specify that the command does take a range (the -range
attribute), or that it takes an arbitrary count value, either in the line
number position (-range=N, like the |:split| command) or as a "count"
argument (-count=N, like the |:Next| command).  The count will then be
available in the argument with |<count>|.

Possible attributes are:

	-range	    Range allowed, default is current line
	-range=%    Range allowed, default is whole file (1,$)
	-range=N    A count (default N) which is specified in the line
		    number position (like |:split|); allows for zero line
		    number.
	-count=N    A count (default N) which is specified either in the line
		    number position, or as an initial argument (like |:Next|).
	-count	    Acts like -count=0

Note that -range=N and -count=N are mutually exclusive - only one should be
specified.

                                                  *:command-addr*
It is possible that the special characters in the range like `.`, `$` or `%`
which by default correspond to the current line, last line and the whole
buffer, relate to arguments, (loaded) buffers, windows or tab pages.

Possible values are (second column is the short name used in listing):
    -addr=lines			Range of lines (this is the default for -range)
    -addr=arguments	  arg	Range for arguments
    -addr=buffers	  buf	Range for buffers (also not loaded buffers)
    -addr=loaded_buffers  load	Range for loaded buffers
    -addr=windows	  win	Range for windows
    -addr=tabs		  tab	Range for tab pages
    -addr=quickfix	  qf	Range 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

Title: Custom Command Examples, Range Handling, and Incremental Preview in Vim
Summary
This section provides examples of custom completion functions for file names, details how to handle ranges and counts in user-defined commands using the `-range` and `-count` attributes, and explains how to implement incremental preview using `inccommand` (available only through Lua via `nvim_create_user_command()`). It describes the function signature and protocol required for the preview callback.