That is, doing autocompletition with C-n, is there a way to obtain the list of possible matches, same as returned into the popup menu ?

Given the example below

enter image description here

is it possible to retrieve {aaa, aaab, ababa} from a vim inner list ? In other words, where these matches are stored ?


If your Vim binary includes the patch 8.1.1068, you can use complete_info(). To get the list of all matches, while the pum is visible, you could use it like this:

map(get(complete_info(['items']), 'items', {}), {_,v -> v.word})

If your Vim binary also includes 8.1.1803, you can rewrite the expression in a more readable way, using the method call operator:

complete_info(['items'])->get('items', {})->map({_,v -> v.word})
                        ^^                ^^

As an example, this installs a C-x C-x mapping which – when pressed while the pum is visible – will assign the list of completion matches to the variable g:matches:

ino <silent> <c-x><c-x> <c-r>=<sid>get_list_of_matches()<cr>
fu s:get_list_of_matches() abort
    if ! pumvisible() | return '' | endif
    let g:matches = complete_info(['items'])->get('items', {})->map({_,v -> v.word})
    return ''

Instead of assigning the variable manually (via a mapping), you can also assign it automatically, by installing an autocmd listening to the CompleteChanged event, provided your Vim binary includes 8.1.1138:

augroup get_list_of_matches
    au CompleteChanged * let g:matches = complete_info(['items'])->get('items', {})->map({_,v -> v.word})
augroup END

For more info, see:

:h complete_info(
:h CompleteChanged

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.