使用indent格式化代码

本文介绍了Linux indent工具的使用方法,重点讲解了-npro, -kr, -i8, -ts8, -sob, -l80, -ss, -ncs, -cp1, -nut, -br, -nce的区别,并提到了如何自定义参数和保存配置。
摘要由CSDN通过智能技术生成

这是一款 Linux 上的工具。下载 indent 省略。

例如要把 xxx.c 格式化,我习惯用
indent -npro -kr -i4 -ts4 -sob -l80 -ss -ncs -cp1 -br -nce -nut xxx.c -o xxx1.c

查看
/usr/src/linux-headers-<版本>/scripts/Lindent
文件 ,可以看到一行代码:

indent -npro -kr -i8 -ts8 -sob -l80 -ss -ncs -cp1

这一行就是 linux 内核使用 indent 工具整理代码的格式。

我习惯再加上 -br -nce -nut

这些参数都是什么意思呢?

选项说明
-npro不要读取 indent 的配置文件:.indent.pro
-kr指定使用 Kernighan&Ritchie 的格式。可以换为 -orig,BSD风格
-i8设置缩排的格数为 8,可以修改
-ts8设置 tab 为 8 个空格,可以修改
-sob删除多余的空白行
-l80代码每行长度超过 80 换行(对于非注释行)
-ss若 for 或者 while 区段只有一行时,在分号前加上空格
-ncsno-space-after-casts,不要在 cast 之后空一格
-cp1#else、#endif 后面的注释开始于列 1(前面空一个格)
-nut不使用 tab 缩进,即 tab 用空格替换
-brif、while 等后面的括号和 if、while 在同一行。Put braces on line with if, etc.

-nce 和 -ce 有啥不一样呢?

-ce, --cuddle-else, Cuddle(拥抱的意思) else and preceding ‘}’.

举个例子,如果用 -br -ce,得到的格式是

  	if (x > 0) {
   	 	x--;
  	} else {
    	fprintf (stderr, "...something wrong?\n");
  	}

看到了吗? else 被 } 和 { 包围

如果用 -br -nce,得到的格式是

      if (x > 0) {
        x--;
      } 
      else {
        fprintf (stderr, "...something wrong?\n");
      }

其实就是 else 另起一行。

The Kernighan & Ritchie style is used throughout their well-known book “The C Programming Language”. It is enabled with the ‘-kr’option. The Kernighan & Ritchie style corresponds to the following set of options:

-nbad -bap -bbo -nbc -br -brs -c33 -cd33 -ncdb -ce -ci4 -cli0
-cp33 -cs -d0 -di1 -nfc1 -nfca -hnl -i4 -ip0 -l75 -lp -npcs
-nprs -npsl -saf -sai -saw -nsc -nsob -nss

具体就不一一解释了。用 man 命令查看吧。

如果你觉得每次都输入那么一大串太麻烦了,那么可以把你的定制化参数保存到 ~/.indent.pro
里面写上
-kr -i4 -ts4 -sob -l80 -ss -ncs -cp1 -br -nce -nut
这样,直接使用

indent xxx.c -o xxx1.c

就可以了

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值