Skip to content

Commit af9a7a0

Browse files
girishjichrisbra
authored andcommitted
patch 9.1.1590: cannot perform autocompletion
Problem: cannot perform autocompletion Solution: Add the 'autocomplete' option value (Girish Palya) This change introduces the 'autocomplete' ('ac') boolean option to enable automatic popup menu completion during insert mode. When enabled, Vim shows a completion menu as you type, similar to pressing |i\_CTRL-N| manually. The items are collected from sources defined in the 'complete' option. To ensure responsiveness, this feature uses a time-sliced strategy: - Sources earlier in the 'complete' list are given more time. - If a source exceeds its allocated timeout, it is interrupted. - The next source is then started with a reduced timeout (exponentially decayed). - A small minimum ensures every source still gets a brief chance to contribute. The feature is fully compatible with other |i_CTRL-X| completion modes, which can temporarily suspend automatic completion when triggered. See :help 'autocomplete' and :help ins-autocompletion for more details. To try it out, use :set ac You should see a popup menu appear automatically with suggestions. This works seamlessly across: - Large files (multi-gigabyte size) - Massive codebases (:argadd thousands of .c or .h files) - Large dictionaries via the `k` option - Slow or blocking LSP servers or user-defined 'completefunc' Despite potential slowness in sources, the menu remains fast, responsive, and useful. Compatibility: This mode is fully compatible with existing completion methods. You can still invoke any CTRL-X based completion (e.g., CTRL-X CTRL-F for filenames) at any time (CTRL-X temporarily suspends 'autocomplete'). To specifically use i_CTRL-N, dismiss the current popup by pressing CTRL-E first. --- How it works To keep completion snappy under all conditions, autocompletion uses a decaying time-sliced algorithm: - Starts with an initial timeout (80ms). - If a source does not complete within the timeout, it's interrupted and the timeout is halved for the next source. - This continues recursively until a minimum timeout (5ms) is reached. - All sources are given a chance, but slower ones are de-prioritized quickly. Most of the time, matches are computed well within the initial window. --- Implementation details - Completion logic is mostly triggered in `edit.c` and handled in insexpand.c. - Uses existing inc_compl_check_keys() mechanism, so no new polling hooks are needed. - The completion system already checks for user input periodically; it now also checks for timer expiry. --- Design notes - The menu doesn't continuously update after it's shown to prevent visual distraction (due to resizing) and ensure the internal list stays synchronized with the displayed menu. - The 'complete' option determines priority—sources listed earlier get more time. - The exponential time-decay mechanism prevents indefinite collection, contributing to low CPU usage and a minimal memory footprint. - Timeout values are intentionally not configurable—this system is optimized to "just work" out of the box. If autocompletion feels slow, it typically indicates a deeper performance bottleneck (e.g., a slow custom function not using `complete_check()`) rather than a configuration issue. --- Performance Based on testing, the total roundtrip time for completion is generally under 200ms. For common usage, it often responds in under 50ms on an average laptop, which falls within the "feels instantaneous" category (sub-100ms) for perceived user experience. | Upper Bound (ms) | Perceived UX |----------------- |------------- | <100 ms | Excellent; instantaneous | <200 ms | Good; snappy | >300 ms | Noticeable lag | >500 ms | Sluggish/Broken --- Why this belongs in core: - Minimal and focused implementation, tightly integrated with existing Insert-mode completion logic. - Zero reliance on autocommands and external scripting. - Makes full use of Vim’s highly composable 'complete' infrastructure while avoiding the complexity of plugin-based solutions. - Gives users C native autocompletion with excellent responsiveness and no configuration overhead. - Adds a key UX functionality in a simple, performant, and Vim-like way. closes: #17812 Signed-off-by: Girish Palya <[email protected]> Signed-off-by: Christian Brabandt <[email protected]>
1 parent 44309b9 commit af9a7a0

File tree

17 files changed

+704
-127
lines changed

17 files changed

+704
-127
lines changed

runtime/doc/insert.txt

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
*insert.txt* For Vim version 9.1. Last change: 2025 Jul 21
1+
*insert.txt* For Vim version 9.1. Last change: 2025 Jul 25
22

33

44
VIM REFERENCE MANUAL by Bram Moolenaar
@@ -1133,6 +1133,22 @@ Stop completion *compl-stop*
11331133
CTRL-X CTRL-Z Stop completion without changing the text.
11341134

11351135

1136+
AUTOCOMPLETION *ins-autocompletion*
1137+
1138+
Vim can display a completion menu as you type, similar to using |i_CTRL-N|,
1139+
but triggered automatically. See |'autocomplete'|. The menu items are
1140+
collected from the sources listed in the |'complete'| option.
1141+
1142+
Unlike manual |i_CTRL-N| completion, this mode uses a decaying timeout to keep
1143+
Vim responsive. Sources earlier in the |'complete'| list are given more time
1144+
(higher priority), but every source is guaranteed a time slice, however small.
1145+
1146+
This mode is fully compatible with other completion modes. You can invoke
1147+
any of them at any time by typing |CTRL-X|, which temporarily suspends
1148+
autocompletion. To use |i_CTRL-N| specifically, press |CTRL-E| first to
1149+
dismiss the popup menu (see |complete_CTRL-E|).
1150+
1151+
11361152
FUNCTIONS FOR FINDING COMPLETIONS *complete-functions*
11371153

11381154
This applies to 'completefunc', 'thesaurusfunc' and 'omnifunc'.

runtime/doc/options.txt

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
*options.txt* For Vim version 9.1. Last change: 2025 Jul 21
1+
*options.txt* For Vim version 9.1. Last change: 2025 Jul 25
22

33

44
VIM REFERENCE MANUAL by Bram Moolenaar
@@ -911,6 +911,13 @@ A jump table for the options with a short description can be found at |Q_op|.
911911
the current directory won't change when navigating to it.
912912
Note: When this option is on some plugins may not work.
913913

914+
*'autocomplete'* *'ac'* *'noautocomplete'* *'noac'*
915+
'autocomplete' 'ac' boolean (default off)
916+
global
917+
{only available on platforms with timing support}
918+
When on, Vim shows a completion menu as you type, similar to using
919+
|i_CTRL-N|, but triggered automatically. See |ins-autocompletion|.
920+
914921
*'autoindent'* *'ai'* *'noautoindent'* *'noai'*
915922
'autoindent' 'ai' boolean (default off)
916923
local to buffer
@@ -2129,9 +2136,9 @@ A jump table for the options with a short description can be found at |Q_op|.
21292136
If the Dict returned by the {func} includes {"refresh": "always"},
21302137
the function will be invoked again whenever the leading text
21312138
changes.
2132-
If generating matches is potentially slow, |complete_check()|
2133-
should be used to avoid blocking and preserve editor
2134-
responsiveness.
2139+
If generating matches is potentially slow, call
2140+
|complete_check()| periodically to keep Vim responsive. This
2141+
is especially important for |ins-autocompletion|.
21352142
F equivalent to using "F{func}", where the function is taken from
21362143
the 'completefunc' option.
21372144
o equivalent to using "F{func}", where the function is taken from
@@ -2278,6 +2285,9 @@ A jump table for the options with a short description can be found at |Q_op|.
22782285
completion in the preview window. Only works in
22792286
combination with "menu" or "menuone".
22802287

2288+
Only "fuzzy", "popup", "popuphidden" and "preview" have an effect when
2289+
'autocomplete' is enabled.
2290+
22812291
This option does not apply to |cmdline-completion|. See 'wildoptions'
22822292
for that.
22832293

runtime/doc/quickref.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
*quickref.txt* For Vim version 9.1. Last change: 2025 Jul 16
1+
*quickref.txt* For Vim version 9.1. Last change: 2025 Jul 25
22

33

44
VIM REFERENCE MANUAL by Bram Moolenaar
@@ -607,6 +607,7 @@ Short explanation of each option: *option-list*
607607
'arabic' 'arab' for Arabic as a default second language
608608
'arabicshape' 'arshape' do shaping for Arabic characters
609609
'autochdir' 'acd' change directory to the file in the current window
610+
'autocomplete' 'ac' automatic completion in insert mode
610611
'autoindent' 'ai' take indent for new line from previous line
611612
'autoread' 'ar' autom. read file when changed outside of Vim
612613
'autoshelldir' 'asd' change directory to the shell's current directory

runtime/doc/tags

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ $quote eval.txt /*$quote*
4141
'] motion.txt /*']*
4242
'^ motion.txt /*'^*
4343
'a motion.txt /*'a*
44+
'ac' options.txt /*'ac'*
4445
'acd' options.txt /*'acd'*
4546
'ai' options.txt /*'ai'*
4647
'akm' options.txt /*'akm'*
@@ -62,6 +63,7 @@ $quote eval.txt /*$quote*
6263
'as' todo.txt /*'as'*
6364
'asd' options.txt /*'asd'*
6465
'autochdir' options.txt /*'autochdir'*
66+
'autocomplete' options.txt /*'autocomplete'*
6567
'autoindent' options.txt /*'autoindent'*
6668
'autoprint' vi_diff.txt /*'autoprint'*
6769
'autoread' options.txt /*'autoread'*
@@ -555,6 +557,7 @@ $quote eval.txt /*$quote*
555557
'mzschemedll' options.txt /*'mzschemedll'*
556558
'mzschemegcdll' options.txt /*'mzschemegcdll'*
557559
'nf' options.txt /*'nf'*
560+
'noac' options.txt /*'noac'*
558561
'noacd' options.txt /*'noacd'*
559562
'noai' options.txt /*'noai'*
560563
'noakm' options.txt /*'noakm'*
@@ -571,6 +574,7 @@ $quote eval.txt /*$quote*
571574
'noas' todo.txt /*'noas'*
572575
'noasd' options.txt /*'noasd'*
573576
'noautochdir' options.txt /*'noautochdir'*
577+
'noautocomplete' options.txt /*'noautocomplete'*
574578
'noautoindent' options.txt /*'noautoindent'*
575579
'noautoread' options.txt /*'noautoread'*
576580
'noautosave' todo.txt /*'noautosave'*
@@ -8585,6 +8589,7 @@ inputlist() builtin.txt /*inputlist()*
85858589
inputrestore() builtin.txt /*inputrestore()*
85868590
inputsave() builtin.txt /*inputsave()*
85878591
inputsecret() builtin.txt /*inputsecret()*
8592+
ins-autocompletion insert.txt /*ins-autocompletion*
85888593
ins-completion insert.txt /*ins-completion*
85898594
ins-completion-menu insert.txt /*ins-completion-menu*
85908595
ins-expandtab insert.txt /*ins-expandtab*

runtime/doc/version9.txt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
*version9.txt* For Vim version 9.1. Last change: 2025 Jul 21
1+
*version9.txt* For Vim version 9.1. Last change: 2025 Jul 25
22

33

44
VIM REFERENCE MANUAL by Bram Moolenaar
@@ -41604,6 +41604,7 @@ Completion~
4160441604
"preinsert" - highlight to be inserted values
4160541605
"nearest" - sort completion results by distance to cursor
4160641606
- new function |wildtrigger()| to trigger wildcard expansion
41607+
- Support for Autocompletion has been added |ins-autocompletion|
4160741608

4160841609
Platform specific~
4160941610
-----------------
@@ -41809,6 +41810,7 @@ Ex-Commands: ~
4180941810

4181041811
Options: ~
4181141812

41813+
'autocompletion' Enable auto completion |ins-autocompletion|
4181241814
'chistory' Size of the quickfix stack |quickfix-stack|.
4181341815
'completefuzzycollect' Enable fuzzy collection of candidates for (some)
4181441816
|ins-completion| modes

runtime/optwin.vim

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
" These commands create the option window.
22
"
33
" Maintainer: The Vim Project <https://github.com/vim/vim>
4-
" Last Change: 2025 Jul 16
4+
" Last Change: 2025 Jul 25
55
" Former Maintainer: Bram Moolenaar <[email protected]>
66

77
" If there already is an option window, jump to that one.
@@ -873,13 +873,15 @@ endif
873873
if has("insert_expand")
874874
call <SID>AddOption("complete", gettext("specifies how Insert mode completion works for CTRL-N and CTRL-P"))
875875
call append("$", "\t" .. s:local_to_buffer)
876-
call <SID>OptionL("cfc")
877-
call <SID>AddOption("completefuzzycollect", gettext("use fuzzy collection for specific completion modes"))
878876
call <SID>OptionL("cpt")
877+
call <SID>AddOption("autocomplete", gettext("automatic completion in insert mode"))
878+
call <SID>BinOptionG("ac", &ac)
879879
call <SID>AddOption("completeopt", gettext("whether to use a popup menu for Insert mode completion"))
880880
call <SID>OptionL("cot")
881881
call <SID>AddOption("completeitemalign", gettext("popup menu item align order"))
882882
call <SID>OptionG("cia", &cia)
883+
call <SID>AddOption("completefuzzycollect", gettext("use fuzzy collection for specific completion modes"))
884+
call <SID>OptionL("cfc")
883885
if exists("+completepopup")
884886
call <SID>AddOption("completepopup", gettext("options for the Insert mode completion info popup"))
885887
call <SID>OptionG("cpp", &cpp)

0 commit comments

Comments
 (0)