MacVim是一款高級(jí)文本編輯器,集成了Vim編輯器的各種功能,同時(shí)也支持Mac系統(tǒng)的界面,非常適合用來(lái)編寫代碼。
對(duì)于需要用MacVim編寫PHP代碼的用戶來(lái)說(shuō),下面介紹一些常用的配置。首先,需要安裝一個(gè)插件管理器——Vundle,它可以方便地管理各種插件。
git clone https://github.com/VundleVim/Vundle.vim.git ~/.vim/bundle/Vundle.vim
然后,在MacVim的配置文件中添加以下配置:
set nocompatible " be iMproved, required
filetype off " required
" set the runtime path
set rtp+=~/.vim/bundle/Vundle.vim
call vundle#begin()
" let Vundle manage Vundle, required
Plugin 'VundleVim/Vundle.vim'
Plugin 'scrooloose/syntastic'
" add all your plugins here (note older versions of Vundle used Bundle instead of Plugin)
Plugin 'tpope/vim-fugitive'
" ...
" All of your Plugins must be added before the following line
call vundle#end() " required
filetype plugin indent on " required
" Enable syntax highlighting
syntax on
上述配置代碼中,Plugin 'VundleVim/Vundle.vim'表示安裝Vundle插件管理器,Plugin 'scrooloose/syntastic'表示安裝Syntastic插件,該插件可以在保存代碼時(shí)自動(dòng)檢查語(yǔ)法錯(cuò)誤。
接下來(lái)是針對(duì)PHP的配置。在MacVim的配置文件中添加以下代碼:
au BufNewFile,BufRead *.php set ft=php
autocmd FileType php imap=php_check()autocmd FileType php nnoremap:call Php_preview()autocmd FileType php nnoremapp :call Php_complete()function! Php_preview()
let script = tempname() . '.php'
let html = tempname() . '.html'
let save_cursor = getpos(".")
silent exe 'write!' script
silent execute "!php -f " . script . " >" . html
silent execute '!open ' . html
silent call setpos('.', save_cursor)
" autocmd BufLeave!rm '$script' '$html'
endfunction
function! Php_complete()
let line = getline('.')
let pos = col('.') - 1
let tagname = line[0 : pos-1] . line[pos : ].match('^[_a-zA-Z][_a-zA-Z0-9]*')[0]
if !tagname || len(tagname) == 0 | return | endif
let matches = system('ctags --php-kinds=m --fields=name --sort=no -R .')
let list = []
for match in split(matches, '\n')
let fields = split(match, '\t')
if len(fields) == 3
let name = fields[0]
let filename = fields[1]
let kind = fields[2]
if kind == 'm' && name =~# '^[a-zA-Z_]*'
call add(list, name)
endif
endif
endfor
if len(list) == 0 | return | endif
call complete(pos, list)
endfunction
function! php_check()
silent! execute "!php -l % >& /tmp/php_syntax_check"
let errors = matchlist(readfile('/tmp/php_syntax_check'), '\v^PHP [0-9]+ errors?:.*\zs[[:alnum:][:punct:] ]*\ze\.$')
if len(errors) >0
echohl ErrorMsg
echom "Syntax error:"
for error in errors
if len(error) >0
echom error
endif
endfor
echohl None
else
echom "Syntax check OK"
endif
endfunction
上述代碼中,au BufNewFile,BufRead *.php set ft=php表示在打開(kāi)擴(kuò)展名為.php的文件時(shí),自動(dòng)設(shè)置文件類型為PHP。另外三個(gè)函數(shù)分別用于預(yù)覽PHP代碼、自動(dòng)補(bǔ)全函數(shù)名稱和檢查語(yǔ)法錯(cuò)誤。
在MacVim中配置好以上內(nèi)容后,即可愉快地使用它來(lái)編寫PHP代碼啦!