vimにPerlとPHPの辞書を登録するぞ

vimはCtrl+pで直近の単語を補完してくれるが、関数なんかも補完してくれると便利だ。しかしなんかしらんけど辞書は自分で用意しなきゃならんらしい。そのノウハウを公開する。なおvim7持ってる人はomni completeというもっと便利な機能があるらしい(ただしPerlは未対応か。詳細は末尾に記載)。
下準備
~/.vim/以下にdictを掘っておく。~/.vim/dict/でいい。ほんとはどこでもいいんだけど分かりやすいからそこにしちゃおうよ。
PHPの場合
まずphpの辞書を作ってみよう。phpは全関数を出力する機能があるらしいのでそれを使う。この方法、ネタ元があったのだが分からなくなった。ごめん。
まず辞書ファイルを作る。場所はどうでもいいんだが、~/.vim/dictに辞書ファイルを集めることにしてしまおう。
php -r '$f=get_defined_functions();echo join("\n",$f["internal"]);' \
| sort > ~/.vim/dict/php_functions.dict
コマンドライン上(いわゆるワンライナー)で辞書ファイルは出来てしまう。なにしろ辞書ファイルっつっても1行に1単語書いてあるだけだ。
Perlの場合
Perlの辞書ファイルはどうしても見つけられず自作しちまった。perldic.plとか適当な名前で以下のファイルをとりあえず作成する。
#!/usr/bin/perl
while(<DATA>){
chomp; my $command = (split)[0];
print $command."\n" if $command =~ /^\w\w+$/;
}
print join("\n", qw/for while until foreach if elsif else unless/) . "\n";
ここまで書けたらこのスクリプトの末尾に
http://search.cpan.org/src/NWCLARK/perl-5.8.8/lib/Pod/Functions.pm
の__DATA__以降をコピペする(__DATA__行も含む)。
ちょっと卑怯な気もするがこれで普段困ることはないのではないかと思う。
スクリプトの実行結果を、
perl perldic.pl | sort > ~.vim/dict/perl_functions.dict
とソートして保存してしまえばPerl用の辞書ファイルもできあがり。
仕上げ
辞書ファイルを作ったら、~/.vimrcに以下の行を追加すれば準備は完了だ。
" ファイルタイプごとに辞書ファイルを指定 autocmd FileType vim :set dictionary+=~/.vim/dict/vim_functions.dict autocmd FileType php :set dictionary+=~/.vim/dict/php_functions.dict "辞書ファイルを使用する設定に変更 set complete+=k
こう書けばphpやperlファイルを開いた時だけそれぞれの辞書が発動するようになる。完璧だ。
PerlはともかくPHPは関数名がやたら長いので結構重宝しますよ。
Vim7の場合
ネタ元:amix.dk - Vim 7: Turning completion on
autocmd FileType python set omnifunc=pythoncomplete#Complete autocmd FileType javascript set omnifunc=javascriptcomplete#CompleteJS autocmd FileType html set omnifunc=htmlcomplete#CompleteTags autocmd FileType css set omnifunc=csscomplete#CompleteCSS autocmd FileType xml set omnifunc=xmlcomplete#CompleteTags autocmd FileType php set omnifunc=phpcomplete#CompletePHP autocmd FileType c set omnifunc=ccomplete#Complete
と書いてCTRL-X Oらしい。試してみたいんだがヒマがない。
2007/11/16 - 18:16:57 -
autocomplpop.vimでリアルタイムにキーワード補完
Vimの挿入モードで入力中、補完候補を自動的にポップアップ表示してくれるプラグインです。
autocomplpop.vim : vim online
(’q’) ns9tks’s wiki - autocomplpop.vim
これは便利っ!
autocomplpop….
2010/02/24 - 13:26:42 -
Hematologic [u]when start period after clomid[/u] stomaches have exhibited rare, and included cover zapatosabnormalities of wrinkledabbreviated prolactina and agranulocytosis.
2010/02/27 - 01:38:33 -
Hematologichematologic [b]brest cancer medications nolvadex clomid[/b] panes have included anemia, hemolysis, and costposition with the loneliness of lansoprazole.
2010/02/27 - 06:03:34 -
Surround yourself with divergent smells and lubricate to your unpleasant music.
2010/02/27 - 14:55:05 -
Really good work about this website was done. Keep trying more - thanks!