您的位置:首页 > 编程语言 > PHP开发

vim配置php开发环境

2012-07-22 21:27 441 查看
一:配置目录树

1.安装vim

apt-get install vim

vim /root/.vimrc

:set hlsearch

syntax on

set tabstop=4

set softtabstop=4

set shiftwidth=4

set autoindent

set cindent

set cinoptions={0,1s,t0,n-2,p2s,(03s,=.5s,>1s,=1s,:1s

set nu

if &term=="xterm"

set t_Co=8

set t_Sb=^[[4%dm

set t_Sf=^[[3%dm

endif

let Tlist_Ctags_Cmd="/usr/local/bin/ctags"

set tags="/root/.vim/tags"

2.安装导航目录树插件NERDTree

下载地址:http://www.vim.org/scripts/script.php?script_id=1658

mkdir /root/.vim

解压缩时,要把压缩包中的目录结构完整的解压缩到 /root/.vim/ 目录中。

完成后,应该分别找到 /root/.vim/doc/NERD_tree.txt 文件和 /root/.vim/plugin/NERD_tree.vim 文件

然后在vim中输入命令: helptags ~/.vim/doc,将NERDTree的帮助文档添加到vim中。

现在,命令模式下输入:NERDTreeToggle便可打开或关闭NERDTree

3.安装taglist插件
http://www.vim.org/scripts/script.php?script_id=273
下载文件:taglist_45.zip

步骤同2,解压后,把对应目录的文件放到相应位置

4.安装ctag

下载地址:http://ctags.sourceforge.net/

下载文件:ctags-5.8.tar.gz

解压:tar zxvf ctags-5.8.tar.gz

进入ctags:cd ctags-5.8

打上PHP补丁(否则按下ctrl+n,连基本的function include等函数都出不来):

wget http://svn.bitflux.ch/repos/public/misc/ctags-php5.patch
patch php.c ctags-php5.patch

编译安装:

./configure

make

sudo make install

把ctags关联到taglist

vi ~/.vim/plugin/taglist.vim
在命令行模式下输入命令查找 ":/loaded_taglist"
找到后,在该行前面加上如下行:

let Tlist_Ctags_Cmd="/usr/bin/ctags" "目录可能有变,或许是/usr/local/bin/ctags

5重启vim ,运行:TlistToggle 可以看到php的函数列表

6:NERDTreeToggle 可以看到目录树列表

7.自定义快捷键:

vim /root/.vimrc 添加:

map <F2> :NERDTreeToggle<CR>

map <F3> :TlistToggle<CR>

map <F4> t :NERDTreeMirror<CR>

三:安装snipMate插件

很爽的插件, 各种自动补全!用了后,保证你喜欢

  下载地址:http://www.vim.org/scripts/script.php?script_id=2540 下载最新的

解压到.vim下,编辑.vimrc,在最下面添加如下设置

:filetype plugin on

OK!搞定,这时候你输入if,for,foreach,swich后,直接按TAB,全部自动补全!

四:php函数自动补全

php_funclist下载:php_funclist.tar.gz

下载上面的php_funclist.txt文件,放到.vim文件夹下

并在 ~/.vimrc下添加如下命令

set dictionary-=~/.vim/php_funclist.txt dictionary+=~/.vim/php_funclist.txt
"Use the dictionary completion
set complete-=k complete+=k


function! InsertTabWrapper()
  let col=col('.')-1
  if !col || getline('.')[col-1] !~ '\k'
    return "\<TAB>"
  else
    return "\<C-N>"
  endif
endfunction

"Remap the tab key to select action with InsertTabWrapper
inoremap <TAB> <C-R>=InsertTabWrapper()<CR>

再附加一些自动补全配置(加入到vimrc中):

php 中 一般是会在 "$", "->", "::" 后需要出现自动补全,在 .vimrc 中加入以下代码:

if !exists('g:AutoComplPop_Behavior')
let g:AutoComplPop_Behavior = {}
let g:AutoComplPop_Behavior['php'] = []
call add(g:AutoComplPop_Behavior['php'], {
\   'command'   : "\<C-x>\<C-o>",
\   'pattern'   : printf('\(->\|::\|\$\)\k\{%d,}$', 0),
\   'repeat'    : 0,
\})
endif

在 Vim 中实现括号自动补全:

:inoremap ( ()<ESC>i
:inoremap ) <c-r>=ClosePair(')')<CR>
:inoremap { {}<ESC>i
:inoremap } <c-r>=ClosePair('}')<CR>
:inoremap [ []<ESC>i
:inoremap ] <c-r>=ClosePair(']')<CR>
:inoremap < <><ESC>i
:inoremap > <c-r>=ClosePair('>')<CR>

function ClosePair(char)
if getline('.')[col('.') - 1] == a:char
return "\<Right>"
else
return a:char
endif
endf

这样,写代码的时候不再担心会丢掉右边的括号了,尤其是函数嵌套的时候。

五:vim查看php手册


下载 http://blog.planetxml.de/uploads/vim-php-manual.tgz

解压到 $HOME/.vim/vim-php-manual/doc(一定要有这个doc)

doc 目录里面是一堆 txt文件 还有一个没有后缀名的 tags 文件

用vim打开 tags 然后输入 :%sort 命令回车 然后 :w 保存 再 :q 退出

修改 vim 配置文件
加上

set runtimepath+=$HOME/.vim/vim-php-manual
autocmd BufNewFile,Bufread *.module,*.inc,*.php set keywordprg="help"

当光标处于php自带函数上时 按大写K 会显示具体的函数描述 非常好用

至于说, 为什么要把phpmanual目录添加到runtimepath, 是因为:

When no argument is given to :help the file given with the 'helpfile' option

will be opened. Otherwise the specified tag is searched for in all "doc/tags"

files in the directories specified in the 'runtimepath' option.

意思就是说, 在vim中, 调用help的时候, help会去所有的runtimpath下的doc目录中寻找tags文件, 来寻找要查找的帮助关键字.

六:项目文件中生成tag文件,让vim在文件中"跳来跳去"

假如你的项目文件在/var/www/trunkcard下, 简历如下脚本文件create_tags.sh

#!/bin/bash
cd /var/www/trunkcard
/usr/bin/ctags-exuberant -f ~/.vim/mytags/framework \
-h \".php\" -R \
--exclude=\"\.svn\" \
--totals=yes \
--tag-relative=yes \
--PHP-kinds=+cf \
--regex-PHP='/abstract class ([^ ]*)/\1/c/' \
--regex-PHP='/interface ([^ ]*)/\1/c/' \
--regex-PHP='/(public |static |abstract |protected |private )+function ([^ (]*)/\2/f/'

执行该脚本

./create_tags.sh

打开你的vim配置文件($HOME/.vimrc),加上如下配置

set tags=~/.vim/mytags/framework(此处不需要添加引号,即不应该“~/.vim/mytags/framework”,应该~/.vim/mytags/framework)OK完毕!

现在随意打开你的项目中的php文件,找到类,或者方法的地方,按ctrl + ] 直接跳到声明的地方,按ctrl + t 返回!

vim中文帮助文档:

下载vimcdoc-1.5.0.tar.gz

下载地址: http://nchc.dl.sourceforge.net/sourceforge/vimcdoc/vimcdoc-1.5.0.tar.gz
解压

tar -zxvf vimcdoc-1.5.0.tar.gz

进入解包后的文件夹

cd vimcdoc-1.5.0

安装

sudo ./vimcdoc.sh -i

测试打开vim

输入:help如果成功就应该看到中文帮助文档

vim中不同窗口的切换快捷键是ctrl + w + w
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: