tail -f findings.out

« Python and vim: Make your own IDE

We’re not in CONNEBRASKACTICUTAH anymore: Adventures with sed »

Code navigation, completion and snippets in vim

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.

Code Navigation

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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
" Taglist variables
" Display function name in status bar:
let g:ctags_statusline=1
" Automatically start script
let generate_tags=1
" Displays taglist results in a vertical window:
let Tlist_Use_Horiz_Window=0
" 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.

Code Completion

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>:

pydiction1

It’s faster than omni-completion, and includes a wider range of modules.

Code snippets

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.

Share and Enjoy:
  • email
  • LinkedIn
  • Slashdot
  • StumbleUpon
  • Technorati
  • Netvibes

Post to Twitter Post to Delicious Post to Digg Post to Reddit

Possibly Related (no promises):

  1. Light at the end of the carpal tunnel: snippets in vim with snipMate
  2. More efficient HTML editing in vim
  3. A better Ruby prompt
  4. A better way to search for methods of Python objects

Related posts brought to you by Yet Another Related Posts Plugin.

Tags: , , , , ,
February 17, 2009 - 1:59 AM
11 comments »
  1. How to Get Six Pack Fast
  2. Samuel Huckins
Leave a reply

Subscribe without commenting

Twitter links powered by Tweet This v1.6.1, a WordPress plugin for Twitter.