|
1 | | -*map.txt* For Vim version 9.2. Last change: 2026 May 23 |
| 1 | +*map.txt* For Vim version 9.2. Last change: 2026 Jun 13 |
2 | 2 |
|
3 | 3 |
|
4 | 4 | VIM REFERENCE MANUAL by Bram Moolenaar |
@@ -1620,6 +1620,11 @@ Completing ":MyCmd2 two va<tab>" will complete with: > |
1620 | 1620 |
|
1621 | 1621 | :MyCmd2 two values |
1622 | 1622 |
|
| 1623 | +Use -nargs=_ when the whole argument area should be taken as a single value |
| 1624 | +as-is. When you need several arguments and an individual argument may itself |
| 1625 | +contain spaces (so the splitter must keep running), use -nargs=+/* together |
| 1626 | +with |:command-completeopt| (-completeopt=escape) instead. |
| 1627 | + |
1623 | 1628 |
|
1624 | 1629 | Note that arguments are used as text, not as expressions. Specifically, |
1625 | 1630 | "s:var" will use the script-local variable in the script where the command was |
@@ -1758,6 +1763,50 @@ the 'path' option: > |
1758 | 1763 | < |
1759 | 1764 | This example does not work for file names with spaces! |
1760 | 1765 |
|
| 1766 | + *:command-completeopt* |
| 1767 | +For the "custom" and "customlist" types you can further opt into specific |
| 1768 | +behaviors with the -completeopt= attribute. It takes a comma-separated list |
| 1769 | +of option names; currently only "escape" is recognized: |
| 1770 | + |
| 1771 | + -completeopt=escape |
| 1772 | + The completion function may return matches containing spaces, |
| 1773 | + tabs or backslashes. When such a match is inserted into the |
| 1774 | + command line each space, tab and backslash is preceded by a |
| 1775 | + backslash so the value is preserved as a single argument. The |
| 1776 | + unescaped form is used for display (popup menu / wildmenu / |
| 1777 | + |getcompletion()|). The ArgLead passed to the completion |
| 1778 | + function is also unescaped: when the user types |
| 1779 | + `:Cmd foo\ b<Tab>` the function is called with `foo b`, not |
| 1780 | + `foo\ b` . In the command's replacement text |<q-args>| and |
| 1781 | + |<f-args>| likewise yield the unescaped value. |
| 1782 | + |
| 1783 | +Example: > |
| 1784 | + :func MyComplete(ArgLead, CmdLine, CursorPos) |
| 1785 | + : return filter(['hello world', 'good morning'], |
| 1786 | + : \ {_, v -> stridx(v, a:ArgLead) ==# 0}) |
| 1787 | + :endfunc |
| 1788 | + :com! -nargs=1 -complete=customlist,MyComplete -completeopt=escape MyCmd |
| 1789 | + \ echo <q-args> |
| 1790 | +< |
| 1791 | +After `:MyCmd <Tab>` the popup shows `hello world` and `good morning`. Typing |
| 1792 | +`:MyCmd hello\ w<Tab>` calls MyComplete with ArgLead set to `hello w` (the |
| 1793 | +backslash before the space is removed), the filter finds `hello world`, and |
| 1794 | +`<Tab>` inserts it as `:MyCmd hello\ world`. `<q-args>` then evaluates to the |
| 1795 | +logical `hello world`. |
| 1796 | + |
| 1797 | +Without -completeopt=escape the literal string `hello world` would be inserted |
| 1798 | +into the command line, so Vim's argument splitter would treat it as two |
| 1799 | +arguments. |
| 1800 | + |
| 1801 | +-completeopt=escape and -nargs=_ (see |:command-nargs|) target different |
| 1802 | +shapes; they are not competing solutions for the same one: |
| 1803 | + "whole line as one argument, no escaping" |
| 1804 | + use -nargs=_ |
| 1805 | + "multiple arguments, each may contain spaces" |
| 1806 | + use -nargs=+ or -nargs=* with -completeopt=escape |
| 1807 | +Because -nargs=_ disables the argument splitter, escaping has no effect there, |
| 1808 | +so combining -nargs=_ with -completeopt=escape is an error (E1579). |
| 1809 | + |
1761 | 1810 |
|
1762 | 1811 | Range handling ~ |
1763 | 1812 | *E177* *E178* *:command-range* *:command-count* |
|
0 commit comments