2010-05-27

vim配置文件注解

类归于: vi/vim — 标签:, kthiz2006 @ 16:00

最近,本人把自己的vim的配置文件整理了一下,现今拿出来晒晒与大家分享,附注释如下:

set nocompatible "设定 gvim 运行在增强模式下,不使用vi的键盘模式
set helplang=cn "设置帮助的语言
filetype on "设置文件类型的检测
filetype plugin on "为特定的文件类型允许插件文件的载入
set et "编辑时将所有 Tab 替换为空格
set sw=2 "自动缩进的时候, 缩进尺寸为 2 个空格
set sts=2 "设置 softtabstop 为 2. 输入 tab 后就跳了2格.
set smarttab "行首的 根据 'shiftwidth' 插入空白
let php_folding = 1 "折叠类、函数等
set backspace=indent,eol,start "不设定的话在插入状态无法用退格键和 Delete
set nobackup " 覆盖文件时不备份
set history=50 " 设置在命令行显示多少历史记录
set showcmd " 在状态栏显示目前所执行的指令
set incsearch " 输入搜索内容时就显示搜索结果
set number " 显示行号
"打开自动缩进和 C 语言风格的缩进模式,定制 C 语言缩进风格
set cindent
set autoindent
set mouse=a " 设定在任何模式下鼠标都可用
set scrolloff=5 " 设定光标离窗口上下边界 5 行时窗口自动滚动
set ignorecase " 搜索时忽略大小写
set smartcase "但在有一个或以上大写字母时,仍保持对大小写敏感
set hid " 允许在有未保存的修改时切换缓冲区,此时的修改由切换由 vim 负责保存
set shiftwidth=2 " 设定 << 和 >> 命令移动时的宽度为 4
set showmatch " 在输入括号时光标会短暂地跳到与之相匹配的括号处,不影响输入
"设置 折行
set textwidth=84
set wrap
syn on
set completeopt=menu,longest,preview
set confirm
"解决乱码
set encoding=utf-8
set fileencodings=utf-8,chinese,latin-1
if has("win32")
set fileencoding=chinese
else
set fileencoding=utf-8
endif
"解决菜单乱码
source $VIMRUNTIME/delmenu.vim
source $VIMRUNTIME/menu.vim
"解决consle输出乱码
language messages zh_CN.utf-8
"设置CTAGS
"let Tlist_Ctags_Cmd = 'E:\vim72\ctag\ctags.exe'
let Tlist_Auto_Open =
let Tlist_Exit_OnlyWindow = 1
let Tlist_Use_Right_Window =1
let tlist_actionscript_settings = 'actionscript;c:class;f:method;p:property;v:variable'
set tags=./tags,./../tags,./**/tags
"设置配色方案
"colorscheme vibrantink
"设置字体
set guifont=Lucida_Console:h11:b
"设置 bufexplorer
map :FirstExplorerWindow
map :BottomExplorerWindow
map :NERDTreeToggle
"去掉烦死我的错误声音
set vb t_vb=
"工作目录随文件变
autocmd BufEnter * cd %:p:h
"设置状态栏
set statusline+=%f
"不显示工具条
set guioptions-=T
"显示横向滚动条
set guioptions+=b
"设置自动补全
setl omnifunc=nullcomplete#Complete
autocmd FileType * setl omnifunc=nullcomplete#Complete
autocmd FileType python setl omnifunc=pythoncomplete#Complete
autocmd FileType javascript setl omnifunc=javascriptcomplete#CompleteJS
autocmd FileType html setl omnifunc=htmlcomplete#CompleteTags noci
autocmd FileType css setl omnifunc=csscomplete#CompleteCSS noci
autocmd FileType xml setl omnifunc=xmlcomplete#CompleteTags
autocmd FileType php setl omnifunc=phpcomplete#CompletePHP
autocmd FileType c setl omnifunc=ccomplete#Complete
"设定状态条
set cmdheight=1 " 设定命令行的行数为 1
set laststatus=2 " 显示状态栏 (默认值为 1, 无法显示状态栏)
set statusline=%F%m%r,%Y,%{&fileformat}\ \ \ ASCII=\%b,HEX=\%B\ \ \ %l,%c%V\ %p%%\ \ \ [\ %L\ lines\ in\ all\ ]
"设置智能大小写查找
set ignorecase smartcase
"设置标尺
set ruler "打开状态栏标尺
"设置制表符
set cindent shiftwidth=2
set softtabstop=2
set smarttab
set list "显示制表符
set listchars=tab:>-
"设置标签
function GuiTabLabel()
let label = ''
let bufnrlist = tabpagebuflist(v:lnum)
" 如果标签页里有修改过的缓冲区,加上 '+'
for bufnr in bufnrlist
if getbufvar(bufnr, "&modified")
let label = '+'
break
endif
endfor
" 如果标签页里的窗口数目多于一,加上窗口数目
let wincount = tabpagewinnr(v:lnum, '$')
if wincount > 1
let label .= wincount
endif
if label != ''
let label .= ' '
endif
" 附加缓冲区名
label . bufname(bufnrlist[tabpagewinnr(v:lnum) - 1])
endfunction
set guitablabel=%{GuiTabLabel()}
set guitablabel=%N\ %f
"设置php注释
"source $vimruntime\vimfiles\plugin\php-doc.vim
inoremap :call PhpDocSingle()i
nnoremap :call PhpDocSingle()
vnoremap :call PhpDocRange()
"设置putty
let g:netrw_sftp_cmd = "PSFTP"
"设置quickfix
"php语法检查
setlocal makeprg=\"d:\wamp\bin\php\php5.2.6\php.exe\"\ -l\ -n\ -d\ html_errors=off\ %
setlocal errorformat=%m\ in\ %f\ on\ line\ %l
"autocmd BufWritePost *.php,*.phps :make
map :w:noh:make

下面是我的vim的配置文件的下载链接:
vimrc

2010-05-26

使用SYMFONY Filter 过滤URL

类归于: symfony — 标签:, , szn000 @ 09:30

这是一个过滤URL中 多个’/'的例子
修改 apps/myapp/config/filters.yml
添加代码
myFilter:
class: myFilter

(myFilter可自定义)
创建文件myFilter.class.php
创建位置apps/myapp/lib/
文件内容如下
使用preg_match 是因为 ereg 过时
注意preg_match 匹配正则时以’/'开始和结束
class myFilter extends sfFilter
{
public function execute ($filterChain)
{
//提取uri
$str = $_SERVER['REQUEST_URI'];
//判断是否匹配正则
if(preg_match("/(.*\/{2,}.*)((\.html)*)/",$str)){
//要跳转的页面
header("Location:/error.html");
//跳转后代码不执行
exit();
}
//执行下一个filter
$filterChain->execute();
}
}

修改apps/myapp/config/routing.yml
添加代码 此代码用来定义URL /error.html 要执行的模块和动作
Error:
url: /error.html
param: { module: index, action: error404}

2010-05-25

仿symfony机制实现不用require或者include来实例化类

类归于: PHP, symfony — 标签:, kthiz2006 @ 10:35


class MyClass {
public static function register() {
//如果解串行器发现有未定义类要被实例化,则设置spl_autoload_call函数加载请求类.
ini_set('unserialize_callback_func', 'spl_autoload_call');
//自动载入类,类似于__autoload
if (false === spl_autoload_register(array("MyClass","autoLoad"))) {
throw new Exception('wrong');
}
}
//处理自动载入类
public static function autoload($className) {
require realpath(dirname(__FILE__)).'/lib/'.strtolower($className).'.class.php';
}
}

只要在项目目录中创建一个类的目录,这里我创建的叫lib,往这里放入你要放的类的文件。
注:
1.类的文件名要统一规则,这样方便系统快速地进行查找,避免消耗更多的内存。
2.autoload的函数的载入类的规则可以依据需要进行设置,我这里规定要载入我的lib目录里文件的扩展名是class.php的类的文件。

以下是使用的例子:
在创建的lib目录里方了一个叫cache的类的文件。
接着在你要编写的程序文件里载入上边的说的自动载入类的文件,使用MyClass::register(),就可以不用载入要实例化的类的文件直接实例化刚才的cache的类的文件。

以上说的实现的效果必须是php的版本>=5.1.2

2010-05-24

在Emacs里使用 PHPCS

类归于: symfonybobhero @ 13:36

前文书说的好 MAKER 可以在VIM 里使用PHPCS ,VIM是很牛B 的编辑器,但是还有一个更牛B 的编辑器就是 EMACS。

具体有多牛,去GOOGLE 百度一下

PHPCS是什么 ,请参考前文

这里贴上 EMACS 下使用的代码


(defun php-lint ()
(interactive)
(let ((compilation-error-regexp-alist '(php))
(compilation-error-regexp-alist-alist ()))
(pushnew '(php "\\(syntax error.*\\) in \\(.*\\) on line \\([0-9]+\\)$" 2 3 nil nil 1)
compilation-error-regexp-alist-alist)
(compile (concat "php -l -f \"" (buffer-file-name) "\""))))
(define-key php-mode-map (kbd " ") 'php-lint)
(defun phpcs ()
"Performs a PHP code sniffer check on the current file."
(interactive)
(let ((compilation-error-regexp-alist '(gnu)))
;; (compile (format "phpcs --standard=PEAR --report=emacs \"%s\""
(compile (format "phpcs --standard=Zend --report=emacs \"%s\""
(buffer-file-name)))))
;; Check code style
(define-key php-mode-map (kbd " ") 'phpcs)

完成以后可以用 快键调用也可以用 命令行调用。
什么 你看不懂? 没有关系 ,看不懂就看不懂吧,早晚能看懂的

vim配合phpcs实现自动语法检查

类归于: PHP, vi/vim — 标签:, , , , maker @ 10:40

在开发过程中, 我们需要保证语法正确, 当然这是基础, 也是必须做到的. 很多时候, 我们还要去遵守一些编码规范, 比如PEAR规则, Zend规则等等, 这样可以让我们写出清晰并且容易阅读的代码, 但是通常来说, 自觉遵守规范是一个很难办到的事情, 所以我们可能需要借助一些辅助方法.

Vim是一款很强大的编辑器, 拥有着可谓无所不能的扩展机制, 这里我简单介绍一下Vim配合phpcs实现自动和手动的语法检查.

什么是phpcs?

phpcs全名PHP_CodeSniffer, 是PEAR中一个用来检测PHP代码规范的包, 他是确保你代码简介一致的必不可少的工具, 并且可以更快的帮你找到语法错误. 关于phpcs的具体安装和使用方法这里就不做说明了, 请各位读者自行查找.

注: phpcs中默认提供了一些编码规范, 在开发过程中我们可能需要一些自己的规范, 可以在phpcs的目录中添加新规范, 规范的编写方法请参考一些内置规范, 规则都是phpcs已经有的, 只需修改声明就可以了.

我们需要扩展vim的功能, 这样需要编写一些脚本, 脚本可以直接加到你的vim配置文件中, 也可以保存成脚本保存在$VIMHOME/plugin/目录下.

下面是脚本内容:

function! Phpcs()
" phpcs 命令的路径和参数, 请根据环境自行修改
! /home/bobhero/Dropbox/project/libphp/bin/phpcs --standard=Zend "%"
cwindow
endfunction
" :w 自动验证语法
autocmd BufWritePost *.php call Phpcs()
" :Phpcs 验证语法
command! Phpcs execute Phpcs()

以上代码添加完毕后重启vim, 然后你就可以在保存文件(:w)的同时自动检查语法了, 还可以在vim中调用Phpcs命令(:Phpcs 注意区分大小写), 我在网上还看见了一些更强大的脚本, 这里就不详细介绍了.

早前文章 »

WordPress 所驱动