如何使用 indent 美化你的代码
代码也需要风格(各种 C 代码风格),经常纠结自己的代码排版不美观,作为一名十分“固执”的程序员,自己在写 C 代码的时候甚至连 #include 头文件顺序都要按拼音序排列,有时候花了很多时间手工检查自己的代码排版风格,虽然是件费力讨好的事,但是宝贵的时间浪费掉了。
最近试用了 indent,发现对代码风格的控制还是挺好的,Linux 内核里面就有一个 indent 脚本(script/Lindent),在 3.2 的内核中 Lindent 如下:
#!/bin/sh PARAM="-npro -kr -i8 -ts8 -sob -l80 -ss -ncs -cp1" RES=`indent --version` V1=`echo $RES | cut -d' ' -f3 | cut -d'.' -f1` V2=`echo $RES | cut -d' ' -f3 | cut -d'.' -f2` V3=`echo $RES | cut -d' ' -f3 | cut -d'.' -f3` if [ $V1 -gt 2 ]; thenPARAM="$PARAM -il0" elif [ $V1 -eq 2 ]; thenif [ $V2 -gt 2 ]; thenPARAM="$PARAM -il0";elif [ $V2 -eq 2 ]; thenif [ $V3 -ge 10 ]; thenPARAM="$PARAM -il0"fifi fi indent $PARAM "$@"
下面是 indent 各个参数的说明(参看):
使用的indent参数 | 值 | 含义 |
---|---|---|
--blank-lines-after-declarations | bad | 变量声明后加空行 |
--blank-lines-after-procedures | bap | 函数结束后加空行 |
--blank-lines-before-block-comments | bbb | 块注释前加空行 |
--break-before-boolean-operator | bbo | 较长的行,在逻辑运算符前分行 |
--blank-lines-after-commas | nbc | 变量声明中,逗号分隔的变量不分行 |
--braces-after-if-line | bl | "if"和"{"分做两行 |
--brace-indent 0 | bli0 | "{"不继续缩进 |
--braces-after-struct-decl-line | bls | 定义结构,"struct"和"{"分行 |
--comment-indentationn | c33 | 语句后注释开始于行33 |
--declaration-comment-columnn | cd33 | 变量声明后注释开始于行33 |
--comment-delimiters-on-blank-lines | ncdb | 不将单行注释变为块注释 |
--cuddle-do-while | ncdw | "do --- while"的"while"和其前面的"}"另起一行 |
--cuddle-else | nce | "else"和其前面的"}"另起一行 |
--case-indentation 0 | cli0 | switch中的case语句所进0个空格 |
--else-endif-columnn | cp33 | #else, #endif后面的注释开始于行33 |
--space-after-cast | cs | 在类型转换后面加空格 |
--line-comments-indentation n | d0 | 单行注释(不从1列开始的),不向左缩进 |
--break-function-decl-args | nbfda | 关闭:函数的参数一个一行 |
--declaration-indentationn | di2 | 变量声明,变量开始于2行,即不必对齐 |
--format-first-column-comments | nfc1 | 不格式化起于第一行的注释 |
--format-all-comments | nfca | 不开启全部格式化注释的开关 |
--honour-newlines | hnl | Prefer to break long lines at the position of newlines in the input. |
--indent-leveln | i4 | 设置缩进多少字符,如果为tab的整数倍,用tab来缩进,否则用空格填充。 |
--parameter-indentationn | ip5 | 旧风格的函数定义中参数说明缩进5个空格 |
--line-length 75 | l75 | 非注释行最长75 |
--continue-at-parentheses | lp | 续行从上一行出现的括号开始 |
--space-after-procedure-calls | pcs | 函数和"("之间插入一个空格 |
--space-after-parentheses | nprs | 在"("后")"前不插入空格 |
--procnames-start-lines | psl | 将函数名和返回类型放在两行定义 |
--space-after-for | saf | for后面有空格 |
--space-after-if | sai | if后面有空格 |
--space-after-while | saw | while后面有空格 |
--start-left-side-of-comments | nsc | 不在生成的块注释中加* |
--swallow-optional-blank-lines | nsob | 不去掉可添加的空行 |
--space-special-semicolon | nss | 一行的for或while语句,在";"前不加空。 |
--tab-size | ts4 | 一个tab为4个空格(要能整除"-in") |
--use-tabs | ut | 使用tab来缩进 |
常见风格 indent 参数设置(参看):
参数 | 含义 | 自定义风格 | GNU风格 | KR风格 | BSD风格 |
---|---|---|---|---|---|
-bad | --blank-lines-after-declarations | y | n | n | n |
-bap | --blank-lines-after-procedures | y | y | y | n |
-bbb | --blank-lines-before-block-comments | y | |||
-bbo | --break-before-boolean-operator | y | y | y | y |
-bc | --blank-lines-after-commas | n | n | n | y |
-bl | --braces-after-if-line | y | y | ||
-blin | --brace-indent n | 0 | 2 | ||
-bls | --braces-after-struct-decl-line | y | y | ||
-br | --braces-on-if-line | y | y | ||
-brs | --braces-on-struct-decl-line | y | y | ||
-bs | --blank-before-sizeof | ||||
-cn | --comment-indentationn | 33 | 33 | 33 | |
-cbin | --case-brace-indentationn | ||||
-cdn | --declaration-comment-columnn | 33 | 33 | 33 | |
-cdb | --comment-delimiters-on-blank-lines | n | n | n | y |
-cdw | --cuddle-do-while | ||||
-ce | --cuddle-else | n | n | n | y |
-cin | --continuation-indentationn | 4 | 4 | ||
-clin | --case-indentationn | 0 | 0 | 0 | |
-cpn | --else-endif-columnn | 33 | 1 | 33 | 33 |
-cs | --space-after-cast | y | y | y | |
-dn | --line-comments-indentationn | 0 | 0 | ||
-ndj | indents declarations the same as code | y | |||
-bfda | --break-function-decl-args | n | |||
-din | --declaration-indentationn | 2 | 2 | 1 | 16 |
-fc1 | --format-first-column-comments | n | n | n | y |
-fca | --format-all-comments | n | n | n | y |
-gnu | --gnu-style | ||||
-hnl | --honour-newlines | y | y | y | y |
-in | --indent-leveln | 4 | 2 | 4 | 4 |
-ipn | --parameter-indentationn | 5 | 5 | 0 | 4 |
-kr | --k-and-r-style | ||||
-ln | --line-lengthn | 75 | 75 | 75 | |
-cs | --space-after-cast | ||||
-dn | --line-comments-indentationn | ||||
-bfda | --break-function-decl-args | ||||
-din | --declaration-indentationn | ||||
-fc1 | --format-first-column-comments | ||||
-fca | --format-all-comments | ||||
-gnu | --gnu-style | ||||
-hnl | --honour-newlines | ||||
-in | --indent-leveln | ||||
-ipn | --parameter-indentationn | ||||
-kr | --k-and-r-style | ||||
-ln | --line-lengthn | ||||
-lcn | --comment-line-lengthn | ||||
-lp | --continue-at-parentheses | y | y | y | |
-lps | --leave-preprocessor-space | ||||
-orig | --original | ||||
-npro | --ignore-profile | ||||
-pcs | --space-after-procedure-calls | y | y | n | n |
-pin | --paren-indentationn | ||||
-pmt | --preserve-mtime | ||||
-prs | --space-after-parentheses | n | n | n | n |
-psl | --procnames-start-lines | y | y | n | y |
-saf | --space-after-for | y | y | y | y |
-sai | --space-after-if | y | y | y | y |
-saw | --space-after-while | y | y | y | y |
-sbin | --struct-brace-indentationn | ||||
-sc | --start-left-side-of-comments | n | n | n | y |
-sob | --swallow-optional-blank-lines | n | n | n | n |
-ss | --space-special-semicolon | n | n | n | |
-st | --standard-output | ||||
-T typenames | Tell indent the name of typenames. | ||||
-tsn | --tab-sizen | 4 | 8 | ||
-ut | --use-tabs | y | |||
-v | --verbose | ||||
-version | Output the version number of indent. |
参考:
C Style: Standards and Guidelines:http://syque.com/cstyle/index.htm
GNU Coding Standards:http://www.gnu.org/prep/standards/standards.html
几种常见风格配置:http://www.gnu.org/software/indent/manual/html_section/indent_4.html#SEC4
如何使用 indent 美化你的代码相关推荐
- 批处理获取当前系统日期及时间及星期转换为数字并加以格式美化的bat代码
代码如下: @echo off&setlocal enabledelayedexpansion ::批处理获取当前系统日期及时间并格式美化的bat代码 echo 读取系统日期和时间(普通) e ...
- dw html5怎么美化,DW CS5/CS6代码格式化、美化插件 Dreamweaver代码格式化美化插件
这个<DW CS5/CS6代码格式化.美化插件>应该是现在最好用的代码格式化扩展了. 众所周知Dreamweaver CS5 CS6自带的应用源格式只能独自格式化HTML文件与CSS文件, ...
- Indent Guides插件格式代码
vs开发工具实用性插件 一. 在开发遇到过很多括号缩近的,很是头疼,于是上网收了一下,希望对大家有所帮助. 第一款插件:Indent Guides 这款插件是给代码块增长对齐线,以标识匹配的花括号 ...
- 【WordPress 美化】短代码给文章添加彩色美化框+按钮
看到很多博客都在用这个美化框,感觉不错,也用来试试,这里说的是纯代码版,用插件 G-Shortcode 也是可以实现同样的功能的,只不过本人有个习惯就是能不用插件就不用,因为插件使用太多托累网站速度. ...
- 使用 Prettier 美化你的代码
大家好,我是前端西瓜哥.今天带大家来学习 Prettier. 为什么要用 Prettier? Prettier 是一款流行的代码格式化工具.它支持的语言相当多. 它很纯粹,就一个代码格式化工具,并不会 ...
- linux之使用indent格式化代码总结
1.介绍indent indent是一个整理代码的工具,不管你的代码写得多乱,你用indent工具都会把你的代码风格和linux 内核代码一致,基本上每个linux 发布都可以找到或者自己安装 sud ...
- Prettier 是 javascript 代码灵活美化工具
搭配ESLint一起工作 很多项目都会使用ESLint来提高代码的质量,集成Prettier和ESLint 是代码美化,统一代码风格规范,并减少团队中的相互代码约束力. Prettier Pretti ...
- 【油猴脚本】改变网页代码块的字体样式/美化LeetCode代码文字显示(CSS;设置@font-face和font-family)
美化网页的代码字体 脚本安装地址: 参考:改变网页代码块的字体样式 更新日志 V0.1 更新时间:2021年9月20日23:04:41 更新功能: 1.改变LeetCode.CSDN.博客园嵌入的代码 ...
- windows下使用indent工具格式化代码
在linux下可以用indent格式化c语言代码,本工具是indent的windows移植版本,只需要将含有indent.exe的文件夹放在有环境变量的路径下就可以在命令行下使用. 下载链接: htt ...
最新文章
- 论文解读 | 微信看一看实时Look-alike推荐算法
- 判断一个变量是不是数组
- chrome浏览器,调试详解,调试js、调试php、调试ajax
- 创建DLL、Lib以及使用DLL、Lib
- WEB产品展示Bootstrap4模板
- USB HID学习:MFC检测USB拔插事件
- 转载! 一图读懂 SignalR
- java 同步异步_Java中的同步于异步
- 《我也能做CTO之程序员职业规划》之三:曲线定律
- 记飞机大战小游戏1.0
- java 游戏 异步框架_基于Java的轻量级异步编程框架
- win10易升_记一次因为升级 Win 10 到 2004 版导致的蓝屏问题的解决
- 03虚幻4 场景中的基础光源和视觉效果
- win10睡眠模式 屏幕熄灭主机仍然运行
- 【校招面试】紫光展锐 - 应用软件工程师面经(一面)
- 京东mate10评论分析
- 区块链的共识机制是什么?
- 一维信号卷积与图像卷积的区别
- (11)RabbitMQ的mandatory、immediate和ReturnListener
- JAVA的三大特性(举例说明)