Can I add a set of words to the Vim autocomplete vocabulary?
Solution 1
You can use abbreviations in your ~/.vimrc
file for just a couple of words:
:abbr supe superuser :abbr autoc autocomplete :abbr que question
These will auto-complete after pressing Space or Enter. So if you typed que
then pressed Space or Enter it would finish the word "question" for you.
If you are adding a lot and want this interface:
You can use dictionaries. Simply set up a file with a word on each line, then in your .vimrc add a line like this:
set dictionary+=/home/john/dict.txt
Replace the path with your dictionary file's location. You can then use Ctrl + x and Ctrl + k to bring up the suggestions. Ctrl + n and Ctrl + p to select the next/previous out of multiple selections.
Solution 2
The "sources" to the regular autocomplete (the one you get from Ctrl+N) are taken from the complete
option (see :h 'complete'
) The default is
complete=.,w,b,u,t
which means
-
.
scan the current buffer -
w
scan buffers from other windows -
b
scan other loaded buffers that are in the buffer list -
u
scan the unloaded buffers that are in the buffer list -
t
tag completion
you can add your own dictionary with
set complete+=k~/.vim/keywords.txt
and add the keywords one per line in ~/.vim/keywords.txt
. This way you can access the completions directly with Ctrl+N (there's no need to explicity invoke dictionary completion with Ctrl+X, Ctrl+K). I found this to be specially useful for code completions where I have all the common used function names in keywords.txt
Related videos on Youtube
madh
painter, beer brewer, pub quiz master, and, at times, a golfer. trying to be a hacker.
Updated on September 17, 2022Comments
-
madh over 1 year
In Vim, I'm aware of using CTRL-n and CTRL-p to autocomplete words found in the document. This is great for promoting the use of descriptive method and variable names.
I would like to add a set of words to autocomplete that I often but are not in the source file I'm currently editing. My current solution is to just add the list of words to a comment at the bottom of my template file, but that seems a bit wasteful and kludgey at best.
Is there a way to add words to Vim's autocomplete vocabulary that would be accessible to every Vim session?
-
madh over 14 yearsfantastic! i am going to use dictionaries!
-
madh over 14 yearsThis worked! I don't get that nice looking menu though. I'm using VIM 6.3.82, which is not the latest version.
-
John T over 14 yearsAh... does updating help? You need to press the sequence Ctrl-x, Ctrl-k to bring up the menu. Glad to help though :)
-
at. over 14 yearsThis is the proper way.
-
madh about 14 yearsJust checked that this works in VIM 7.2. Thanks again!
-
John T about 14 yearsYou're very welcome!
-
UncleZeiv about 14 yearslooks like the picture is not there anymore