Pythonの開発環境をvimに整える

vimPythonの開発環境を作ってみました。

インデントの設定

自動 インデントはPythonでコーディングをする場合には必須な機能です。定番の設定があるので、.vimrcに追記します。

filetype plugin on
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

インデントは4文字でタブ文字ではなく半角スペースで設定するようにしています。また、if等のキーワードに対してスマートインデントが設定されています。

スクリプトの実行

学習時は特にエディタで編集しているスクリプトを実行できると便利です。こちらも各所で紹介されているので流用して.vimrcに追加します。

" Execute python script C-P 
function! s:ExecPy()
    exe "!" . &ft . " %"
:endfunction
command! Exec call <SID>ExecPy()
autocmd FileType python map <silent> <C-P> :call <SID>ExecPy()<CR>

FileTypeがPythonの場合、Ctrl+PでPythonスクリプトとして実行されます。

コード補完

Eclipseに慣れている自分としてはコード補完がない環境での開発は考えられません。高度な入力補完はできなくとも最低限の入力補完はないかと探してみた所、pydictionというプラグインが良さそうです。
http://www.vim.org/scripts/script.php?script_id=850
※2009/09/08時点ではバージョン1.2

インストール

配布サイト下部よりzipをダウンロードし、適当なディレクトリで展開します。展開すると4つのファイルが

$ wget http://www.vim.org/scripts/download_script.php?src_id=11062
$ unzip pydiction-1.2.zip 
Archive:  pydiction-1.2.zip
   creating: pydiction-1.2/
  inflating: pydiction-1.2/python_pydiction.vim  
  inflating: pydiction-1.2/pydiction.py  
  inflating: pydiction-1.2/complete-dict  
  inflating: pydiction-1.2/README.txt 

python_pydiction.vimプラグインの本体です。~/.vim/after/ftplugin/ の下にコピーします。
complete-dictは辞書ファイルになります。このファイルはどこに設置しても構いませんが、~/.vim/pydiction/の下にコピーしました。pydiction.pyは辞書ファイルに定義を追加するスクリプトなので、同じディレクトリに置いておきます。

$ mkdir -p ~/.vim/after/ftplugin/
$ cp python_pydiction.vim ~/.vim/after/ftplugin/
$ mkdir -p ~/.vim/pydiction/
$ cp complete-dict ~/.vim/pydiction/
$ cp pydiction.py ~/.vim/pydiction/

最後に.vimrcに以下のコードを追加して辞書ファイルの場所を示します。

autocmd FileType python let g:pydiction_location = '~/.vim/pydiction/complete-dict'
使い方

コードの補完はvimの補完機能を利用しているようですが、pydictionではタブキーで補完が可能になっています。ただし、最初にタブを押した時、一番上の候補に勝手に補完されてしまうのは使いにくい所です*1
使い方としてはある程度しぼれるまで入力してからタブ、候補が2−3出現し、タブで選択する、という感じですね。尚、Ctrl+Eで補完をキャンセルできます。

辞書ファイルに独自モジュールを追加する

辞書ファイルには基本モジュールは含まれている状態ですが、必要に応じて辞書ファイルをカスタマイズするスクリプトが同梱されています。使い方は、以下のようにします。

$ python pydiction.py <ModuleName>

モジュールは半角スペースで区切って複数指定する事ができます。当然ですが、モジュールにはパスが通っている必要があります。

Djangoのモジュールを追加する

上記方法でDjangoのモジュールを追加する事はできますが、django.db.modelsなど一部のモジュールを取り込もうとすると、エラーとなってしまいます。これはsettings.pyが読み込まれていないため、初期化に失敗しているからです。このエラーは次の手順で回避できます。
1. 適当にDjangoプロジェクトを作成する
2.プロジェクトのルートディレクトリにpydiction.pyとcomplete-dictをコピーする。
3. pydiction.pyを開き、以下のコードを2行目あたりに挿入する

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

4. 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

まとめ

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
" Execute python script C-P 
function! s:ExecPy()
    exe "!" . &ft . " %"
:endfunction
command! Exec call <SID>ExecPy()
autocmd FileType python map <silent> <C-P> :call <SID>ExecPy()<CR>

*1:ちょっとスクリプトをいじればできそうな気がしますが、わかりませんでした