PydictionでVimにPython補完機能をつけよう!!(&Djangoも!)

Pydictionって?

VimPythonのコーディングをする時に、コード補完が可能になるため、楽にコーディング出来るようになります。

インストール

配布元からパッケージを取得してきます。
現在の最新版は、1.2のようです。

$> wget http://www.vim.org/scripts/download_script.php?src_id=11062
$> mv download_script.php\?src_id\=11062 pydiction-1.2.zip
$> unzip pydiction-1.2.zip
$> mkdir -p ~/.vim/after/ftplugin/
$> cp pydiction-1.2/python_pydiction.vim ~/.vim/after/ftplugin/
$> mkdir -p ~/.vim/pydiction/
$> cp complete-dict pydiction.py ~/.vim/pydiction/

後は、~/.vim.rcにPydiction用の設定を追加します。

" python
filetype plugin on
autocmd FileType python let g:pydiction_location = '~/.vim/pydiction/complete-dict'
autocmd FileType python setl autoindent
autocmd FileType python setl smartindent cinwords=if,elif,else,for,while,try,except,finally,def,class
autocmd FileType python setl expandtab tabstop=4 shiftwidth=4 softtabstop=4

これで完了です。
これでVim上でPythonのコーディングを行う際に、TABで補完機能が動作します。

Django補完アドオンの作成

一部のモジュールがsettings.pyに依存しているようで、ひと手間必要とのこと。

  1. 適当にDjangoプロジェクトを作成します。

(後で削除するので、ホントに適当でいーです。)

$> cd ~/temp/
$> django-admin.py startproject tekito
$> cd tekito
プロジェクトルートに、pydiction.pyとcomplete-dictをコピー
$> cp ~/.vim/pydiction/* .

pydiction.pyの2行目あたりに、以下のコードを追加。

import settings
from django.core.management import setup_environ
setup_environ(settings)

pydiction.pyを使って、使いたいモジュールをcomplete-dictに取り込む

$> python pydiction.py django django.contrib django.contrib.admin django.db django.db.models django.db.models.fields django.forms django.forms.extras django.http django.shortcuts django.utils
Backing up current complete-dict to complete-dict.last
Trying module: django
Trying module: django.contrib
Trying module: django.contrib.admin
Trying module: django.db
Trying module: django.db.models
Trying module: django.db.models.fields
Trying module: django.forms
Trying module: django.forms.extras
Trying module: django.http
Trying module: django.shortcuts
Trying module: django.utils
Removing duplicates...
Done.

complete-dictを元の場所に戻す。

$> cp complete-dict ~/.vim/pydiction/

[参考]
http://wiki.deathmarch.jp/Python/Django/pydiction
Pythonの開発環境をvimに整える - やさしいデスマーチ