vim word and code completion

Microsoft’s intellisense is an awesome power for developers to wield code faster.

I prefer vim for various reasons which I will not list.

Sometimes completing words or variable names in vim would be nice. Particularly when I forget the case of a variable because I am not using consistent style of variable names, or when I’m using someone elses code.

I’ve been familiar with vim and tags for a while. Vim will read a file named tags from the current directory and use it to index other files. The tags file is generated by the program ctags which is hopefully the version from the exuberant ctags project. Its great to be able to navigate to function implementations and member declarations. Tags are great.

I’m new to vim’s dictionary functions. I learned about it in vim tip #91. I added

" enable words completion
set dictionary+=/usr/share/dict/words
" use ctrl-n ctrl-n instead of ctrl-x ctrl-k
set complete-=k complete+=k

to my .vimrc.

Now I can ctrl-n to cycle through variables, function, in the tags file, or words in dict/words.

The piece which is a bonus is when I don’t have a match in those files, vim actually scans included files. I stumbled on this when testing in a C file. I don’t write much C, but it turns out vim will scan included files in your current file for word matches. This are the complete+i and d options. See :help complete for more information.

It turns out ctrl-n automatically completes variables based on tags, includes, and other things listed in the vim help. I did not need to add anything to my vimrc to have this support, I just needed to know to use it. The vimrc additions just include using ctrl-n for the vim dictionary feature.

As a “P” person (perl/python/php), it would be awesome to have “includes” include searching perl/python/php based on use/import/require&include. But I’ve not found how to do this. For now, tags will do. Same for C#.

Coupled with code folding, there is not much vim won’t do. I’ve been using vim for years, and I’ve always found it difficult to find good documentation. There is so much out there geared toward new users, that finding intermediate to advanced tips is difficult. :help is sometimes good enough, but other times I want more of a howto or users guide.

“Learn vi” should always be translated to learn vim, including regex, code folds and tags. Now I extend this list to include completion.

2 thoughts on “vim word and code completion”

Comments are closed.