0

If I type an unfinished word in Vim (in insert mode) and press CTRL+N I get a list of autocomplete suggestions.
(this list contains words being used in that file and other files that are currently open that start with the word I just typed)

I want this list in vimscript as an array so I can loop it, for example:

let autoCompleteList = getAutocompleteList("regex")

I pass a string to the function("regex") and it returns a list of matches.

Does a function like getAutocompleteList exist?
If not, is there another way to get this list?

There is complete_info() which can give you a list of matches while you are in insert mode, while using the complete feature (e.g. CTRL+N) and while the completion menu is visible. I want that list but without opening the completion menu, without being in insert mode and by passing my own search term as arg instead of using the word that is next to the cursor.

Conclusion:
I assume this functionality does not exist. I might write my own function that replicates this behaviour using something like matchstrpos() and executing that on all open buffers. My concern is that that will be less efficient than using the already built in functionality of Vim.

8
  • 1
    i have searched a lot and could not find it, if you know the name of the function please tell me
    – Alexanus
    Commented Feb 11 at 7:14
  • 1
    I'm not sure that API exist. May be could you tell us more about what you try to achieve. May be we could redirect you to an alternative way. Commented Feb 11 at 9:35
  • 4
    @romainl Please don’t engage to say “I won’t help you.” That’s not helpful nor kind, and doesn’t further the question being answered.
    – D. Ben Knoble
    Commented Feb 11 at 15:08
  • 1
    @romainl, my understanding of the remark of Ben is that he wonders why you bother to educate people not asking question that could be answered by the documentation of Vim. Vim documentation is very good and internet contains a lot of additional information such that with enough experience probably most of the answers of this site could be answered without the help of this site (I believe 99.9% easily). But my understanding of this site is also to Welcome new Vim users, creates a community where some knowledgeable or experienced people help them to encourage them to use Vim and contribute. Commented Feb 11 at 20:22
  • 1
    The list is computed when you press <C-n> etc. It doesn't exist before and it stops existing once the completion is done. The only time you can get that list is during completion… because that's the only context where it matters. If you want something that looks like that list outside of that context, you have two options: a) build it yourself or b) enter insert mode, trigger completion, grab the list, cancel completion, and leave insert mode, hoping it won't be too noticeable for the user.
    – romainl
    Commented Feb 11 at 20:33

1 Answer 1

1

I didn't fond exactly the function @romainl knows but here is an approximation.

If the completion is triggered and the completion menu visible (Ctrl n or Ctrl p) the following calls gives you a the list of words proposed by the menu:

map(complete_info(["items"]).items, {_, v -> v.word})
2
  • 1
    That is the one.
    – romainl
    Commented Feb 11 at 20:18
  • thanks, i saw it too, i edited my question to make it more clear what i want
    – Alexanus
    Commented Feb 11 at 20:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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