在vim中使用perltidy美化perl代码

格式优美的perl代码不但让人赏心悦目,而且可以方便阅读.

perltidy的是sourceforge的一个小项目,在我们写完乱七八糟的代码后,他能像变魔术一样把代码整理得漂漂亮亮,快来体验一下吧!!!

perltidy 主页: http://perltidy.sourceforge.net/perltidy.html

安装方法:

进入解压后的目录,然后执行一下命令

perl Makefile.PL

make

make test

make install

使用方法:

配置一下vim,使得我们在写代码的时候,不离开vim就可以美化我们代码。

在/etc/vimrc最后一行加入:(意思是快捷键pt来调用perltidy)

map ,pt :%! perltidy

写完perl代码后,在vim命令模式下,输入命令“,pt”就可以直接整理代码格式了。

注意:

1)如果需要安装到自己的home目录下,如下:

perl Makefile.PL PREFIX=~/perl

setenv PERL5LIB ~/perl/lib/site_perl/5.8.3

2)调用时使用config,默认为home目录下的.perltidyrc:

默认地,perltidy yourscript 将生成yourscript.tdy文件, 需要手动用tdy文件覆盖原来的文件。

如果只用指定的配置文件 perltidy -pro=tidyconfigfile yourscript > yourscript.tdy,然后在用tdy文件覆盖原来的文件。

默认的配置文件实例 .perltidyrc file:

# This is a simple of a .perltidyrc configuration file

# This implements a highly spaced style

-bl # braces on new lines

-pt=0 # parens not tight at all

-bt=0 # braces not tight

-sbt=0 # square brackets not tight

我的配置文件:

-gnu # GNU Coding Standards

-pbp # --perl-best-practices

-i=2 # Use 2 columns per indentation level

-ci=2 # Continuation indentation is extra indentation spaces applied when a long line is broken.

-ce # else and elsif are follow immediately after the curly brace closing the previous block

-nbl # places an opening brace on the same line as the keyword introducing it.

-bt=2 # braces not tight

-sbt=2 # square brackets not tight

-pt=2 # Tightness of curly braces, parentheses, and square brackets.

-nsfs # --space-for-semicolon

-nsak=s # --nospace-after-keyword=s removes keywords.

-dws # --delete-old-whitespace

完!