Re: VimのPython向け設定 - climpyの日記

~/.vimrc に

autocmd BufRead,BufNewFile *.py syntax on
autocmd BufRead,BufNewFile *.py set ai
autocmd BufRead *.py set smartindent cinwords=if,elif,else,for,while,with,try,except,finally,def,class

を追記、これで構文強調表示と自動字下げが設定される。

VimのPython向け設定 - climpyの日記
  • syntax on はグローバルなので autocmd で実行する必要はない。直接書けばOK。
  • バッファローカルな設定を行う際は :setlocal を使用する。
  • 特定のファイルタイプに関する設定をする場合は FileType イベントを使うか、ftplugin を使う。

すなわち、

" .vimrc
syntax on
autocmd FileType python setlocal ai
autocmd FileType python setlocal smartindent cinwords=if,elif,else,for,while,with,try,except,finally,def,class

もしくは下の2行は ~/.vim/after/ftplugin/python.vim*1

setlocal ai
setlocal smartindent cinwords=if,elif,else,for,while,with,try,except,finally,def,class

後者の方がおすすめ。

つづいて

# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4

ソースコード中に書くことにする。

VimのPython向け設定 - climpyの日記

これをいちいち書くくらいなら、何かよほど特殊な事情でもない限り上記と一緒に設定されるようにしてしまった方が良いと思います。

" ~/.vim/after/ftplugin/python.vim に追記
setlocal tabstop=8 expandtab shiftwidth=4 softtabstop=4

*1:正確には b:undo_ftplugin とか必要だけどここでは省略。詳細は :help undo_ftplugin