默认情况下, vim 支持代码格式化的, 使用命令
gg=G
会完成一个整体缩进的功能, 但是这样对于代码美化来说是完成不够的, 那么我就需要一些外置工具来实现这个功能了.
 幸运的是, 强大的vim提供了这样一个格式化代码的接口, 这个接口就是 equalprg, 这里我们就以美化PHP代码为例来完成这个配置的操作.
 1. 安装 PHP 的美化工具 PHP_Beautifier:
pear install PHP_Beautifier-0.1.15
2. 屏蔽 PHP_Beautifier 的报错:
 打开 /usr/local/php/bin/ (我PHP的安装目录) 下的 php_beautifier
将 error_reporting(E_ALL) 改为 error_reporting(0)
3. 配置符合自己美化方式的定制脚本(linux下的, windows下请自行编写bat文件):
touch /usr/local/bin/xw.phpb chmod +x /usr/local/bin/xw.phpb
编辑 /usr/local/bin/xw.phpb, 添加以下内容
#!/bin/bash EXEC=/usr/local/php/bin/php_beautifier [ -x "$EXEC" ] || exit 1 $EXEC -l "ArrayNested() NewLines(before=T_COMMENT:if,after=T_PUBLIC:T_PROTECTED:T_PRIVATE)" $*
上述脚本里 /usr/local/php/bin/php_beautifier 是我的 php_beautifier 的安装目录.
 这里的 -l 是 php_beautifier 的选项可以实现对格式化 PHP 代码的控制.
4. 让 vim 支持美化工具:
:set equalprg=/usr/local/bin/xw.phpb
这个时候打开一个PHP文件, 输入以下内容
<?php
class Person {
    /**
     * 姓名
     * @var string
     */
    public $name = '';
    /**
     * 地区代码
     * @var string
     */
    public $areaCode = '';
    /**
     * 性别
     * @var integer
     */
    protected $gender=1;
    /**
     * 构造
     */
    public function __construct($conf) {
        $this->name = $conf['name'];
        $this->areaCode = $conf['areaCode'];
        $this->gender = $conf['gender'];
    }
}
/**
 * Hello Moto
 */
function demo() {
    return true;
}
if(demo()) {
    echo 123,456,789;
}
$arr = array('name' => 'xwsoul','areaCode' => 'cn','gender' => 0,);
$p = new Person($arr);再使用:
gg=G
最终会得到以下效果
<?php
class Person {
    /**
     * 姓名
     * @var string
     */
    public $name = '';
    /**
     * 地区代码
     * @var string
     */
    public $areaCode = '';
    /**
     * 性别
     * @var integer
     */
    protected $gender = 1;
    /**
     * 构造
     */
    public function __construct($conf) {
        $this->name = $conf['name'];
        $this->areaCode = $conf['areaCode'];
        $this->gender = $conf['gender'];
    }
}
/**
 * Hello Moto
 */
function demo() {
    return true;
}
if (demo()) {
    echo 123, 456, 789;
}
$arr = array(
    'name' => 'xwsoul',
    'areaCode' => 'cn',
    'gender' => 0,
);
$p = new Person($arr);至此, vim 下配置 PHP 的代码格式化工具就算是完成了.
参考资料:
http://linuxfan-linuxtips.blogspot.com/2008/12/vim.html
http://beautifyphp.sourceforge.net/docs/
