Out of the box Autocompletion
| language: | english |
|---|---|
| Date: | 2009-03 |
| Author: | Travis Whitton |
| source: | http://dailyvim.blogspot.com/2009/03/out-of-box-autocompletion.html |
Thanks to Chris Sutter for contributing the following tip:
(The following applies to versions of vim higher than 7.0)
If you are editing a file in vim which ends with .php, .html, .css, .js, .sql, .rb, or .py, you can type (while in insert mode) ctrl-x followed by ctrl-o and vim's "omnifunc" feature combines with its built-in autocomplete feature to show autocomplete options specific to the corresponding language/markup. You can subsequently press ctrl-n and ctrl-p to browse through this list. In short, vim has autocomplete functionality for all common web development contexts (and probably some others) out of the box. But it's not just autocomplete.
If you are editing php (or, i imagine, ruby/python but i didn't try) and the text on which you are autocompleting is the beginning of a valid (builtin) function name, a small window will open at the top of your vim screen showing the method signature with argument names! No more jumping over to php.net to see if it's (needle, haystack) or (haystack, needle)...
Example:
Edit a file called foo.php, enter insert mode, type:
<?php str_r
Now press ctrl-x ctrl-o and vim shows a box below the cursor containing the following, with the first entry highlighted:
str_repeat( f str_replace( f str_rot13( f
The "f"s indicate that each entry is a function. If you had entered $_ and pressed c-x c-o, the results would have been "$_COOKIE", "$_ENV", and so on, labeled with "v" for variable)
The window opened above (since the selection is a function) shows:
str_repeat(string input, int multiplier | string
telling you the full function name, the argument types and names and finally the return type.
Admittedly, the c-x c-o and c-n/c-p stuff is awkward. I imagine you could remap to tab/shift-tab` which is more common for autocompletion browsing in unix environments. The SuperTab plugin for vim does this for normal file-wise autocompletion (otherwise done in insert mode with ``c-n and c-p). It could probably be hacked to incorporate omnifunc stuff, too. If I see anything like that out there, I'll send something about it.
Please let me know if there's anything you think vim can't do and I'll see to ensuring you that, in fact, it can and it's easier than you think!
