vimのPHP開発環境

提供: neovim/vim入門
移動: 案内検索
スポンサーリンク

このページは、vimを最速でコーディングするためのPHPの統合開発環境(IDE)にしてしまうためのドキュメントです。vimのプラグイン管理としてNeoBundleを使用します。マイクロソフトのVisual Studio系のインテリセンスのようなvimにおけるオムニ補完を強力にするneocompleteや補完する対称の説明を補完するneocomplete-phpを取り上げます。同じようなコーディングを簡略化するためのスニペット機能neosnippetを紹介します。また、シンタックスチェックの自動化のため Syntastic をとりあげ、コーディングしたものを速攻で実行するための QuickRun を紹介します。

概要

vimは、選択肢が無限大です。答えはひとつではありません。これは、あくまでも1例です。 ここで紹介するの一部は、PHP用ですが、多くは、PHP以外でも利用できます。

  • プラグイン管理
  • 補完
  • タグリスト
  • シンタックスチェック
  • 実行
  • ドキュメント参照

さぁ、補完をはじめましょう。

プラグイン管理

PHPには、直接関係がありませんが、「PHPを補完する機能」を補完するために使用します。

mkdir -p ~/.vim/bundle
git clone git://github.com/Shougo/neobundle.vim ~/.vim/bundle/neobundle.vim
set nocompatible
filetype off
 
if has('vim_starting')
        set rtp+=$HOME/.vim/bundle/neobundle.vim/
endif
call neobundle#rc(expand('~/.vim/bundle'))
 
NeoBundleFetch 'Shougo/neobundle.vim'
filetype plugin indent on       " restore filetype

補完

  • オムニ補完
  • neocomplete
  • neosnippet
  • emmet(zencoding)

neocomplete

neocompleteは、キーワード補完システムを提供します。

  • ファイル名の補完
    • パス
    • インクルードファイル
  • オムニ補完
NeoBundle 'Shougo/neocomplete.vim'
 
"Note: This option must set it in .vimrc(_vimrc).  NOT IN .gvimrc(_gvimrc)!
" Disable AutoComplPop.
let g:acp_enableAtStartup = 0
" Use neocomplete.
let g:neocomplete#enable_at_startup = 1
" Use smartcase.
let g:neocomplete#enable_smart_case = 1
" Set minimum syntax keyword length.
let g:neocomplete#sources#syntax#min_keyword_length = 3
let g:neocomplete#lock_buffer_name_pattern = '\*ku\*'
 
" Define dictionary.
let g:neocomplete#sources#dictionary#dictionaries = {
			\ 'default' : '',
			\ 'vimshell' : $HOME.'/.vimshell_hist',
			\ 'scheme' : $HOME.'/.gosh_completions'
			\ }
 
" Define keyword.
if !exists('g:neocomplete#keyword_patterns')
	let g:neocomplete#keyword_patterns = {}
endif
let g:neocomplete#keyword_patterns['default'] = '\h\w*'
 
" Plugin key-mappings.
inoremap <expr><C-g>     neocomplete#undo_completion()
inoremap <expr><C-l>     neocomplete#complete_common_string()
 
" Recommended key-mappings.
" <CR>: close popup and save indent.
inoremap <silent> <CR> <C-r>=<SID>my_cr_function()<CR>
function! s:my_cr_function()
	return neocomplete#close_popup() . "\<CR>"
	" For no inserting <CR> key.
	"return pumvisible() ? neocomplete#close_popup() : "\<CR>"
endfunction
" <TAB>: completion.
inoremap <expr><TAB>  pumvisible() ? "\<C-n>" : "\<TAB>"
" <C-h>, <BS>: close popup and delete backword char.
inoremap <expr><C-h> neocomplete#smart_close_popup()."\<C-h>"
inoremap <expr><BS> neocomplete#smart_close_popup()."\<C-h>"
inoremap <expr><C-y>  neocomplete#close_popup()
inoremap <expr><C-e>  neocomplete#cancel_popup()
" Close popup by <Space>.
"inoremap <expr><Space> pumvisible() ? neocomplete#close_popup() : "\<Space>"
 
" For cursor moving in insert mode(Not recommended)
"inoremap <expr><Left>  neocomplete#close_popup() . "\<Left>"
"inoremap <expr><Right> neocomplete#close_popup() . "\<Right>"
"inoremap <expr><Up>    neocomplete#close_popup() . "\<Up>"
"inoremap <expr><Down>  neocomplete#close_popup() . "\<Down>"
" Or set this.
"let g:neocomplete#enable_cursor_hold_i = 1
" Or set this.
"let g:neocomplete#enable_insert_char_pre = 1
 
" AutoComplPop like behavior.
"let g:neocomplete#enable_auto_select = 1
 
" Shell like behavior(not recommended).
"set completeopt+=longest
"let g:neocomplete#enable_auto_select = 1
"let g:neocomplete#disable_auto_complete = 1
"inoremap <expr><TAB>  pumvisible() ? "\<Down>" : "\<C-x>\<C-u>"
 
" Enable omni completion.
autocmd FileType css setlocal omnifunc=csscomplete#CompleteCSS
autocmd FileType html,markdown setlocal omnifunc=htmlcomplete#CompleteTags
autocmd FileType javascript setlocal omnifunc=javascriptcomplete#CompleteJS
autocmd FileType python setlocal omnifunc=pythoncomplete#Complete
autocmd FileType xml setlocal omnifunc=xmlcomplete#CompleteTags
let g:neocomplete#sources#omni#input_patterns.perl = '\h\w*->\h\w*\|\h\w*::'

neocomplete-php

PHPの関数名の説明を表示できます。

NeoBundle 'violetyk/neocomplete-php.vim'
let g:neocomplete_php_locale = 'ja'

日本語の辞書を作成するなら、一度だけ

:PhpMakeDict ja

を実行します。

neocompleteとneocomplete-phpによる超補完術の例

neocompleteとneocomplete-phpによる超補完術の例です。

neosnippet

NeoBundle 'Shougo/neosnippet'
NeoBundle 'Shougo/neosnippet-snippets'
imap <C-k>     <Plug>(neosnippet_expand_or_jump)
smap <C-k>     <Plug>(neosnippet_expand_or_jump)

emmet-vim

PHP自体の補完ではありません。HTMLの補完です。

NeoBundle 'mattn/emmet-vim'
  • html:5
  • div
  • div>ul>li*5
  • a:mail
  • form:post
  • div>p>em{hoge}+s{foo}

タグリスト

taglist.vim

NeoBundle 'taglist.vim'
:Tlist
ctags -R -n --languages=PHP --PHP-types=c+f+d
 
# FreeBSD
exctags -R -n --languages=PHP --PHP-types=c+f+d

tagbar + phpctags

NeoBundle 'vim-scripts/tagbar-phpctags', {
  \   'build' : {
  \     'others' : 'chmod +x bin/phpctags',
  \   },
  \ }
NeoBundle 'vim-scripts/tagbar'

Unite outline

Vim unite-outlineでソースコード探索がラクチン

NeoBundle 'Shougo/unite.vim'
NeoBundle 'Shougo/unite-outline'
"NeoBundle 'h1mesuke/unite-outline'

2014-04-30時点では、unite-outlineのメンテナーが不在とのことらしく、C++とかでunite-outlineが動かないバグがあるため、 Shougo/unite-outline を使用したほうが良いです。

https://github.com/h1mesuke/unite-outline/network

:Unite outline

シンタックスチェック

syntastic

NeoBundle 'scrooloose/syntastic'
let g:syntastic_enable_signs=1
let g:syntastic_auto_loc_list=2

実行

quickrun

vimprocを利用することで非同期で QuickRun が実行できます。

NeoBundle 'Shougo/vimproc', {
      \ 'build' : {
      \     'windows' : 'make -f make_mingw32.mak',
      \     'cygwin' : 'make -f make_cygwin.mak',
      \     'mac' : 'make -f make_mac.mak',
      \     'unix' : 'make -f make_unix.mak',
      \    },
      \ }
 
NeoBundle 'thinca/vim-quickrun'
let g:quickrun_config={'*': {'split': ''}}
let g:quickrun_config._={ 'runner':'vimproc',
\       "runner/vimproc/updatetime" : 10,
\       "outputter/buffer/close_on_empty" : 1,
\ }

PHPUnit

cd /usr/ports/devel/pear-PHPUnit3/
sudo make
:QuickRun phpunit

ドキュメント

vim-ref

PHPのドキュメントをvimの中で参照できます。 http://www.php.net/download-docs.php

NeoBundle 'thinca/vim-ref'
let g:ref_phpmanual_path=$HOME . '/tmp/vim/php/php-chunked-xhtml'
mkdir -p $HOME/tmp/vim/php
cd $HOME/tmp/vim/php
wget http://jp1.php.net/distributions/manual/php_manual_ja.tar.gz
tar zxfp php_manual_ja.tar.gz
:Ref phpmanual echo

関連項目




スポンサーリンク