Vim Recipes ‣ Typing ‣ Auto-Completing Text
You want to be able to type the start of a word and then have Vim complete it. For example, you want to type multip, be offered suggestions for words that start that way, then pick one.
To auto-complete a word Vim needs a list of possible words. An obvious source is the current file(s). If your file already contains the word multiplicand, then Vim can use it to auto-complete multip. Simply hit <Ctrl>+n after the p to complete the word. If multiple matches are found you'll be presented with a list from which to choose from.
Another source of words is a dictionary. Get one, using the sidebar for reference, then point Vim to it with :set dictionary=file. Then add the dictionary to list of places Vim looks for words: :set complete+=k. The 'complete' options controls where Vim looks for possible matches.
Getting Dictionaries
If you specify your language with :set spelllang=lang-code, e.g. :set spelllang=it for Italian, Vim should automatically download the correct dictionary for you and set it up. If this doesn't work for you see :help spell and vim/runtime/spell/README.txt.
Auto-complete can also be used to lookup synonyms for the current word. Get a thesaurus file, then instruct Vim to use it with :set thesaurus=file, :set complete+=s.
<Ctrl>-x <Ctrl>-f searches for the filename that starts with the text before the cursor. If one is found, it is inserted before the cursor. If multiple filenames are found, a drop down list is displayed to choose between them.
Eric Van Dewoestine's SuperTab plugin lets you use the <Tab> key to auto-complete text. You select the type of completion that you want using :SuperTabHelp, then just hit <Tab> after a word to see a drop-down menu of completion choices.
Vim can use pretty much any word source imaginable. Consult :help ins-completion for more details.
Vim version 7 and above supports Omni-Completion, which allows custom functions to generate possible completions dynamically. Even better, for programmers at least, is that some popular programming languages already have Omni-Completion functions which are enabled automatically. These typically allow context-sensitive completion of method names, objects, and reserved words. For example, using Ruby I can type an integer, a period, then invoke Omni-Completion to find that Fixnum objects support methods such as %, *, and +.
Languages that don't have Omni-Complete functions available can use their syntax highlighting definitions to achieve a similar affect. The Vim documentation suggests adding the following stanza to your vimrc, after any :filetype command, to enable Omni-Completion using the best available method:
if has("autocmd") && exists("+omnifunc")
autocmd Filetype *
\ if &omnifunc == "" |
\ setlocal omnifunc=syntaxcomplete#Complete |
\ endif
endif
To use Omni-Completion hit Ctrl+x Ctrl+o to be presented with a list of choices for the text behind the cursor.
Apart from programming languages, Omni-Completion also works for HTML and CSS. For example, you can type <p cl, Ctrlx Ctrl+o, then be shown class=" CDATA, onclick=" Script, and ondblclick=" Script.