使用indent格式化代码

Posted 车子 chezi

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了使用indent格式化代码相关的知识,希望对你有一定的参考价值。

这是一款 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格式化代码的主要内容,如果未能解决你的问题,请参考以下文章

代码格式化工具---indent工具

linux 使用indent格式化代码

xlsx怎么改800米的格式

Linux 命令(243)—— indent 命令

Linux 命令(243)—— indent 命令

linux下使用indent整理代码