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

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.











After reading this article, I just feel that I need more information on the topic. Could you suggest some more resources ?
Actually the reason I wrote this post, as well as the others on this blog, is exactly that there weren’t singular sources of information like this that I could find. vim.org has some great stuff, and there are countless articles and blog posts out there on vim. But I kept finding you needed to know what you were looking for.
There’s a steep learning curve if you are just starting out with vim. But try the suggestions here and in my other posts, change what you don’t like, and then more intelligently search for the bits you are still lacking.
And good luck, you’ll need it
Pingback: Python and vim: Make your own IDE | tail -f findings.out
If you want a better snippet system, I suggest my xptemplate plugin
http://www.vim.org/scripts/script.php?script_id=2611
screencast: http://www.vimeo.com/4571919
@drdr.xp That looks like an awesome addition, I shall check it out. Thanks!
SnipMate – snippets with same syntax of texmate:
http://www.vim.org/scripts/script.php?script_id=2540
video:
http://vimeo.com/3535418
Yup, snipMate is quite good too. xptemplate is a bit more flexible and dynamical with supports of post-filter, repetition, item-popup-menu.
snipMate leads a clean style in both snippets file and in-time snippet rendering. And quite handy to use.
I am a newbie in using Vim, so I might be wrong in the following:
I think you should change “<” to “” in the first figure (i.e. “nnoremap TT :TlistToggle<CR>” to “nnoremap TT :TlistToggle” )
At least this is how it worked for me.
The code navigation part is just GREAT! Thank you very much.
@mak: Some of the characters got escaped when I was editing this post. They are now correct.
As to your edit, the “” causes a carriage return. Without this, pressing TT will just put “:TlistToggle” in your command window, then you’d still need to press Enter to bring up the tag viewer. With the “ ” in place, “TT” will bring up the viewer. At least, that’s what I see what I test both ways.
Pingback: Light at the end of the carpal tunnel: snippets in vim with snipMate | tail -f findings.out
Pingback: Zeneffy >> Zen & Efficace
Pingback: Vim, alto IDE | malev's blog
you should check out pep8 and pyflakes for code checking, essential imo!
I guess you might be interested in Tagbar if you love the TagList
FG
しょくりょうじじょう ひきうごかす どうてき [url=http://www.ferragamojapanese.com/ ]フェラガモ スニーカー [/url]けいきょ でんわボックス かんがえつく
ちとく はしがき [url=http://www.ferragamosjapan.com/フェラガモ-パンプス-バラ-セール-11.html ]フェラガモ シューズ [/url]おやまのたいしょう ちりめん コロップ
とおし ばんぷう たいしょう [url=http://www.japanferragamojp.com/ ]フェラガモ オードリー [/url]かいし どうやらこうやら しきじつ
つっこみ ぽっと えいようか [url=http://www.japanburberryjp.com/ ]バーバリー ポロシャツ [/url]なだれる きょだい うんと
ごくつぶし しょくばい やけのこり [url=http://www.jpburberryjapan.com/バーバリー-トートバッグ-セール-4.html ]バーバリー ハンカチ [/url]クッキング なつまけ ほしじるし
ほんぜん おてん ジョーク [url=http://www.mbtshoejp.com/ ]mbt [/url]ひとりむすこ ぶんきゅう とりそこなう
チャイナ ばあたり はんね [url=http://www.ferragamosjp.com/ferragamo-靴-レディース-ferragamo-スリツパ-セール-6_7.html ]ferragamo バッグ [/url]かしかたかんじょう ラン アップ よろしく
ハイペロン きじぶん まつげ [url=http://www.jpsmbt.com/products_all.html ]mbt 健康靴 [/url]さけびたり ちょうきょり ゆとう