created July 19, 2001 · complexity basic · author Brian Medley · version 5.7
This tip will explain how to use the dictionary completion facilities provided by Vim. This can be useful if you use Vim to type your email, edit code, etc.
Dictionary completion is one of many search facilities provided by Insert mode completion. It allows the user to get a list of keywords, based off of the current word at the cursor. This is useful if you are typing a long word (e.g. acknowledgeable) and don't want to finish typing or don't remember the spelling.
To start, we must first tell Vim where our dictionary is located. This is done via the 'dictionary' option. Below is an example. Your location may vary. See :help 'dictionary' for hints as to where you should look.
:set dictionary-=/usr/share/dict/words dictionary+=/usr/share/dict/words
Now, to use this list we have to enter insert mode completion. This is done by hitting CTRL-X while in insert mode. Next, you have to specify what you want to complete. For dictionaries use CTRL-K. Once in this mode the keys CTRL-N and CTRL-P will cycle through the matches. So, to complete the word "acknowledgeable" I would do the following in insert mode:
It can be cumbersome to type CTRL-X CTRL-K for many different completions. So, Vim gives us a shortcut. While in insert mode CTRL-N and CTRL-P will pull completion results from multiple sources. This set is defined by the 'complete' option and by default dictionary completion is not enabled. Add 'k' to the list to enable dictionary completion source:
Now, while in insert mode we can type the following to complete our example:
This shortcut may not save a whole lot of typing. However, I find that it requires less hand movement to only worry myself with two key combinations, rather than four. However, it also means more results in your ins-completion list (rather than just other words in the buffer or whatever other options are set).
Another way to achieve this is to map-in-insert-mode the cumbersome <C-X><C-K><C-N> with:
:imap <F12>i <C-X><C-K><C-N>
- <F12> can be any <F.> or <F..> key and
- i from <F12> can be any character of your preference !
Then, when in insert mode, typing
rolls down the auto-complete suggestion list out of above mentioned dictionaries.
Filetype-specific Dictionaries Edit
You can also use filetype-specific dictionaries. If the user would like to automatically load a word file from their ~/.vim/words/ directory for the particular filetype they are working on, the following line can be inserted into the .vimrc file:
au FileType * execute 'setlocal dict+=~/.vim/words/'.&filetype.'.txt'
This allows you to create ~/.vim/words/tex.txt for inserting jargon while composing a thesis in latex or ~/.vim/words/r.txt for statistics jargon in R.
Similar use casesEdit
Completion for Java/Perl/php/etc keywordsEdit
Instead of a dictionary, see Completion using a syntax file.
Tag files can be also searched for completion by adding "]" or "t" (the default) to your "complete" setting.
Invoke completion with CTRL-N.
Most distributions of Linux come with wordlists, but Windows does not. You can find word lists from the scowl archives: http://wordlist.sourceforge.net
Great tip so far, but how can I get the dictionary to understand compound words? e.g. 'Doctors of philosophy'? How does one complete those in without completion cut off after 'doctors'?
Normally pressing <Ctrl-X><Ctrl-N> in sucession pulls the words after the last completion. Unfortunately this works only with sentences in the active buffer not with dictionaries as far as I could test.