Skip to content

Commit d7d6a6f

Browse files
girishjichrisbra
authored andcommitted
runtime(doc): Improve doc for cmdline-autocompletion
- Address vim/vim#18219 (comment) - Make the cmdline-autocompletion help more user friendly closes: #18245 Signed-off-by: Girish Palya <girishji@gmail.com> Signed-off-by: Christian Brabandt <cb@256bit.org>
1 parent 3980c86 commit d7d6a6f

File tree

3 files changed

+96
-22
lines changed

3 files changed

+96
-22
lines changed

runtime/doc/builtin.txt

Lines changed: 9 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
*builtin.txt* For Vim version 9.1. Last change: 2025 Sep 06
1+
*builtin.txt* For Vim version 9.1. Last change: 2025 Sep 08
22

33

44
VIM REFERENCE MANUAL by Bram Moolenaar
@@ -12407,31 +12407,22 @@ wildmenumode() *wildmenumode()*
1240712407
wildtrigger() *wildtrigger()*
1240812408
Start wildcard expansion in the command-line, using the
1240912409
behavior defined by the 'wildmode' and 'wildoptions' settings.
12410-
See |cmdline-completion|.
1241112410

1241212411
This function also enables completion in search patterns such
1241312412
as |/|, |?|, |:s|, |:g|, |:v| and |:vimgrep|.
1241412413

1241512414
Unlike pressing 'wildchar' manually, this function does not
1241612415
produce a beep when no matches are found and generally
1241712416
operates more quietly. This makes it suitable for triggering
12418-
completion automatically, such as from an |:autocmd|.
12417+
completion automatically.
12418+
12419+
Note: After navigating command-line history, the first call to
12420+
wildtrigger() is a no-op; a second call is needed to start
12421+
expansion. This is to support history navigation in
12422+
command-line autocompletion.
12423+
12424+
See |cmdline-autocompletion|.
1241912425

12420-
*cmdline-autocompletion*
12421-
Example: To make the completion menu pop up automatically
12422-
while typing on the command line: >
12423-
autocmd CmdlineChanged [:/\?] call wildtrigger()
12424-
set wildmode=noselect:lastused,full wildoptions=pum
12425-
<
12426-
To retain normal history navigation with the up/down keys: >
12427-
cnoremap <expr> <Up> wildmenumode() ? "\<C-E>\<Up>" : "\<Up>"
12428-
cnoremap <expr> <Down> wildmenumode() ? "\<C-E>\<Down>" : "\<Down>"
12429-
<
12430-
To apply an option only during a search, for example to set
12431-
'pumheight': >
12432-
autocmd CmdlineEnter [/\?] set pumheight=8
12433-
autocmd CmdlineLeave [/\?] set pumheight&
12434-
<
1243512426
Return value is always 0.
1243612427

1243712428
Return type: |Number|

runtime/doc/cmdline.txt

Lines changed: 84 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
*cmdline.txt* For Vim version 9.1. Last change: 2025 Aug 08
1+
*cmdline.txt* For Vim version 9.1. Last change: 2025 Sep 08
22

33

44
VIM REFERENCE MANUAL by Bram Moolenaar
@@ -20,6 +20,7 @@ Basic command line editing is explained in chapter 20 of the user manual
2020
5. Ex command-line flags |ex-flags|
2121
6. Ex special characters |cmdline-special|
2222
7. Command-line window |cmdline-window|
23+
8. Command-line autocompletion |cmdline-autocompletion|
2324

2425
==============================================================================
2526
1. Command-line editing *cmdline-editing*
@@ -406,6 +407,9 @@ word before the cursor. This is available for:
406407
The number of help item matches is limited (currently to 300) to avoid a long
407408
delay when there are very many matches.
408409

410+
For automatic completion as you type (without pressing a key like <Tab>),
411+
see |cmdline-autocompletion|.
412+
409413
These are the commands that can be used:
410414

411415
*c_CTRL-D*
@@ -479,8 +483,6 @@ When repeating 'wildchar' or CTRL-N you cycle through the matches, eventually
479483
ending up back to what was typed. If the first match is not what you wanted,
480484
you can use <S-Tab> or CTRL-P to go straight back to what you typed.
481485

482-
See also |wildtrigger()|.
483-
484486
The 'wildmenu' option can be set to show the matches just above the command
485487
line.
486488

@@ -1340,4 +1342,83 @@ The character used for the pattern indicates the type of command-line:
13401342
@ string for |input()|
13411343
- text for |:insert| or |:append|
13421344

1345+
==============================================================================
1346+
8. Command-line autocompletion *cmdline-autocompletion*
1347+
1348+
Autocompletion makes the command-line more efficient and easier to navigate by
1349+
automatically showing a popup menu of suggestions as you type, whether
1350+
searching (/ or ?) or entering commands (:).
1351+
1352+
A basic setup is: >
1353+
autocmd CmdlineChanged [:/\?] call wildtrigger()
1354+
set wildmode=noselect:lastused,full
1355+
set wildoptions=pum
1356+
1357+
With this configuration, suggestions appear immediately, and you can
1358+
move through them with <Tab> or the arrow keys.
1359+
1360+
To retain normal command-line history navigation with <Up>/<Down>: >
1361+
cnoremap <expr> <Up> wildmenumode() ? "\<C-E>\<Up>" : "\<Up>"
1362+
cnoremap <expr> <Down> wildmenumode() ? "\<C-E>\<Down>" : "\<Down>"
1363+
1364+
Options can also be applied only for specific command-lines. For
1365+
example, to use a shorter popup menu height only during search: >
1366+
autocmd CmdlineEnter [/\?] set pumheight=8
1367+
autocmd CmdlineLeave [/\?] set pumheight&
1368+
1369+
EXTRAS *fuzzy-file-picker* *live-grep*
1370+
1371+
Command-line autocompletion can also be extended for advanced uses.
1372+
For example, you can turn the native |:find| command into a fuzzy, interactive
1373+
file picker: >
1374+
1375+
set findfunc=Find
1376+
func Find(arg, _)
1377+
if get(s:, 'filescache', []) == []
1378+
let s:filescache = systemlist(
1379+
\ 'find . -path "*/.git" -prune -o -type f -print')
1380+
endif
1381+
return a:arg == '' ? s:filescache : matchfuzzy(s:filescache, a:arg)
1382+
endfunc
1383+
autocmd CmdlineEnter : let s:filescache = []
1384+
1385+
The `:Grep` command searches for lines matching a pattern and updates the
1386+
results dynamically as you type (triggered after two characters; note: needs
1387+
the `CmdlineLeavePre` autocmd from the next section): >
1388+
1389+
command! -nargs=+ -complete=customlist,<SID>Grep
1390+
\ Grep call <SID>VisitFile()
1391+
1392+
func s:Grep(arglead, cmdline, cursorpos)
1393+
let cmd = $'grep -REIHns "{a:arglead}" --exclude-dir=.git
1394+
\ --exclude=".*"'
1395+
return len(a:arglead) > 1 ? systemlist(cmd) : []
1396+
endfunc
1397+
1398+
func s:VisitFile()
1399+
let item = getqflist(#{lines: [s:selected]}).items[0]
1400+
let pos = '[0,\ item.lnum,\ item.col,\ 0]'
1401+
exe $':b +call\ setpos(".",\ {pos}) {item.bufnr}'
1402+
call setbufvar(item.bufnr, '&buflisted', 1)
1403+
endfunc
1404+
1405+
Automatically select the first item in the completion list when leaving the
1406+
command-line, and for `:Grep`, add the typed pattern to the command-line
1407+
history: >
1408+
1409+
autocmd CmdlineLeavePre :
1410+
\ if get(cmdcomplete_info(), 'matches', []) != [] |
1411+
\ let s:info = cmdcomplete_info() |
1412+
\ if getcmdline() =~ '^\s*fin\%[d]\s' && s:info.selected == -1 |
1413+
\ call setcmdline($'find {s:info.matches[0]}') |
1414+
\ endif |
1415+
\ if getcmdline() =~ '^\s*Grep\s' |
1416+
\ let s:selected = s:info.selected != -1
1417+
\ ? s:info.matches[s:info.selected] : s:info.matches[0] |
1418+
\ call setcmdline(s:info.cmdline_orig) |
1419+
\ endif |
1420+
\ endif
1421+
1422+
For autocompletion in insert mode, see |ins-autocompletion|.
1423+
13431424
vim:tw=78:ts=8:noet:ft=help:norl:

runtime/doc/tags

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6673,7 +6673,7 @@ cmdarg-variable eval.txt /*cmdarg-variable*
66736673
cmdbang-variable eval.txt /*cmdbang-variable*
66746674
cmdcomplete_info() builtin.txt /*cmdcomplete_info()*
66756675
cmdline-arguments vi_diff.txt /*cmdline-arguments*
6676-
cmdline-autocompletion builtin.txt /*cmdline-autocompletion*
6676+
cmdline-autocompletion cmdline.txt /*cmdline-autocompletion*
66776677
cmdline-changed version5.txt /*cmdline-changed*
66786678
cmdline-completion cmdline.txt /*cmdline-completion*
66796679
cmdline-editing cmdline.txt /*cmdline-editing*
@@ -7680,6 +7680,7 @@ function-range-example userfunc.txt /*function-range-example*
76807680
function-search-undo userfunc.txt /*function-search-undo*
76817681
function_key intro.txt /*function_key*
76827682
functions eval.txt /*functions*
7683+
fuzzy-file-picker cmdline.txt /*fuzzy-file-picker*
76837684
fuzzy-matching pattern.txt /*fuzzy-matching*
76847685
fvwm.vim syntax.txt /*fvwm.vim*
76857686
fvwm2rc syntax.txt /*fvwm2rc*
@@ -8829,6 +8830,7 @@ listener_remove() builtin.txt /*listener_remove()*
88298830
lite.vim syntax.txt /*lite.vim*
88308831
literal-Dict eval.txt /*literal-Dict*
88318832
literal-string eval.txt /*literal-string*
8833+
live-grep cmdline.txt /*live-grep*
88328834
lnum-variable eval.txt /*lnum-variable*
88338835
load-plugins starting.txt /*load-plugins*
88348836
load-vim-script repeat.txt /*load-vim-script*

0 commit comments

Comments
 (0)