In my last post on configuring vim as a (Python) IDE, there were two topics I forgot to cover and another I feel it necessary to amplify. The missing: viewing your code at a high level, and navigation therein; insertion of “code snippets” (i.e. completion of common code contructs). The short-shrifted: Types of code completion and how to set them up.
There are a few simple steps to getting this all working, but it’s definitely worth it. You will end up with the ability to bring up a window showing all the classes and functions in your current file, and be able to jump to any of them.
First you need to install exuberant-ctags. It should be available in most repositories under that name. Then you need the vim taglist plugin (drop the plugin in ~/.vim/plugin and the readme in ~/.vim/doc). Go into ~/.vim/doc, open vim, and run “:helptags”. Now all should be installed and ready.
Before using it, let’s clean up the default configuration. Add this to your ~/.vimrc:
" Taglist variables
" Display function name in status bar:
" Automatically start script
" Displays taglist results in a vertical window:
" Shorter commands to toggle Taglist display
nnoremap TT :TlistToggle<CR>
map <F4> :TlistToggle<CR>
" Various Taglist diplay config:
let Tlist_Use_Right_Window = 1
let Tlist_Compact_Format = 1
let Tlist_Exit_OnlyWindow = 1
let Tlist_GainFocus_On_ToggleOpen = 1
let Tlist_File_Fold_Auto_Close = 1
Once you close vim and reopen it (for best results, open a reasonably complex piece of code), you can press “TT” or <F4> to bring up a new window, displaying all classes and functions in the current file:
The options set above cause the window to appear vertically, on the right, and in a compact form, making display easy to configure. Additionally, the last three options cause the tag window to have focus when opened, as well as to close when the file being edited is closed. These make for a much more intuitive experience.
There are a number of possible goals here, as well as possible methods. I have tried out a few, and will share my results. First, consider the types of completion you might want. It is possible to provide matches for completion based on phrases in your current file, on entries in a dictionary of Python syntax, as well as to display contextual help for various matches.
For setting up completion based on current file contents (as well as other useful things like an English dictionary and filesystem locations!), check out this post over at The Geek Stuff.
For completion based on language constructs (in Python), pydiction is perfect. To make things easier, I also have installed the SuperTab plugin. This lets me perform all completions with <Tab>. In the following case, I typed “os.path.” then pressed <Tab>:
It’s faster than omni-completion, and includes a wider range of modules.
The name is somewhat worthlessly vague, but the purpose is definite: You start typing a common language construct, such as “def” or “for”, press <Tab>, and get a skeleton of that contruct. Not only that, but the skeleton matches the type of the file being edited, and you can tab through the skeleton’s fields!
[EDIT, 2009-07-19]: I’ve switched to snipMate for snippets in vim, and have been loving it. Check out this post for more. I’d recommend reading that and skipping the rest of this post.
To get this working, you need the snippetsEmu vim plugin. This was my first install of a Vimball based plugin, but it was simple to work with. Create a directory ~/.vim/after/ftplugin, if it does not exist. Download snippy_plugin.vba and snippy_bundles.vba there. Open each in vim and run “:source %”. All should now be installed.
In example.py, I typed “def” and pressed <Tab>. The result:
The same thing works for “for” loops, classes and many other constructs. The bundles .vba provides access to such constructs across a wide range of languages, e.g. Python, Ruby, HTML, PHP, even Djano-specific constructs.
Once you have pressed <Tab> and the skeleton is added, start typing to fill out the first field. Once done, press <Tab> again to move through the successive fields the skeleton provides. Once done, you will be within the construct created.
One thing to become accustomed to are the trigger words used by snippetsEmu. Most of the time they are perfectly intuitive, like “def”, but others might not be as you expect. Run “:help snippets” to view the documention.