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 10
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
20205. Ex command-line flags | ex-flags |
21216. Ex special characters | cmdline-special |
22227. Command-line window | cmdline-window |
23+ 8. Command-line autocompletion | cmdline-autocompletion |
2324
2425==============================================================================
25261. Command-line editing *cmdline-editing*
@@ -406,6 +407,9 @@ word before the cursor. This is available for:
406407The number of help item matches is limited (currently to 300) to avoid a long
407408delay 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+
409413These 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
479483ending up back to what was typed. If the first match is not what you wanted,
480484you can use <S-Tab> or CTRL-P to go straight back to what you typed.
481485
482- See also | wildtrigger() | .
483-
484486The 'wildmenu' option can be set to show the matches just above the command
485487line.
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:
0 commit comments