Stickies

Autocompletion in Vim (Mac OSX)

Overview

Vim has some powerful autocompletion features. Out of the box autocompletion for insert mode is a sub mode which you access using Ctrl X. If you view the help file:

:help ins-completion

You can find out all you need to know about the default Vim autocomplete functionality.

Autocompletion can draw it’s matches from many different sources. Below is a list ( taken from the help file ), which I am going to explore a bit.

  1. Whole lines |i_CTRL-X_CTRL-L|
  2. keywords in the current file |i_CTRL-X_CTRL-N|
  3. keywords in ‘dictionary’ |i_CTRL-X_CTRL-K|
  4. keywords in ‘thesaurus’, thesaurus-style |i_CTRL-X_CTRL-T|
  5. keywords in the current and included files |i_CTRL-X_CTRL-I|
  6. tags |i_CTRL-X_CTRL-]|
  7. file names |i_CTRL-X_CTRL-F|
  8. definitions or macros |i_CTRL-X_CTRL-D|
  9. Vim command-line |i_CTRL-X_CTRL-V|
  10. User defined completion |i_CTRL-X_CTRL-U|
  11. omni completion |i_CTRL-X_CTRL-O|
  12. Spelling suggestions |i_CTRL-X_s|
  13. keywords in ‘complete’ |i_CTRL-N|

The dictionary

On Mac OSX Lion, the dictionary files are located at

/usr/share/dict/words

so, in your vimrc

:set dictionary='/usr/share/dict/words'

Now you have autocompletion with 235886 words at your disposal! You can actually give ‘dictionary’ a list, for example you could also add the propernames file at /usr/share/dict/propernames.

The thesaurus

Unfortunately, the thesaurus on a Mac is stored in a binary format, so is inaccessible to Vim. Instead, check out https://github.com/sgur/thesaurus-gutenberg.vim, it’s a pre-defined thesaurus for Vim.

** Update: not sure if this plugin works **

Included files

This option lets you include words from other files in the include path, if you use ctags, this is almost not necessary in my mind.

Tags

Vim’s autocomplete can use the tags file generated by ctags to give you a match, this is very useful if you want to use class, method or varibale names accross classes.

Definitions and macros

Not really sure how this works

Completing Vim commands

This is really just for when you are writing vimscript and want to autocomplete some Vim command, it doesn’t work in Visual Mode or Command line mode.

User defined completion

This function looks like it could be really powerful as it lets you define your own custom function for autocompletion. Perhaps the topic of another post :)

Plugins

My personal favorite, as recommended by a colleague: https://github.com/ervandew/supertab

Also: https://github.com/Shougo/neocomplcache

Useful resources

http://vim.wikia.com/wiki/Any_word_completion