Linux indent 命令 | C / C++ 源代码格式化工具
• 最后修改:2025-06-25 • 阅读量:20
Linux indent ( indent ) 命令用于格式化 C / C++ 源代码。
indent 命令的定义解释
indent ( indent ) 命令是一个强大的工具,它可以根据一组特定的规则缩进代码,从而帮助您生成格式一致的代码。您将首先了解缩进命令的目的和语法,然后学习如何使用它来格式化 C/ C++ 源代码。最后,您需要自定义缩进格式可以查看选项的解释说明。
安装 indent 命令
在默认情况下,很多系统是没有安装 indent 命令的,要使用 indent 首先要安装 indent:
1、Debian、Ubuntu 安装 indent:
sudo apt update
sudo apt install indent
2、RHEL、AlmaLinux、Rocky Linux 安装 indent:
sudo dnf update
sudo dnf install indent
3、FreeBSD 安装 indent:
sudo pkg update
sudo pkg install indent
4、Arch Linux 安装 indent:
sudo pacman -Sy
sudo pacman -S indent
indent 命令语法
indent [options] [input-files]
indent [options] [single-input-file] [-o output-file]
indent --version
[options]
:可选,可用于自定义代码格式的各种选项[input-files]
:可选,需要格式化的 C / C++ 多个文件[single-input-file]
:可选,需要格式化的 C / C++ 单个文件[-o output-file]
:可选,格式化单个文件时,用于指定输出文件- 当没有指定格式化文件时,从标准输入读取内容,按
Ctrl + D
结束输入内容
常用格式输出选项
设置大括号位置的选项
-br, --braces-on-if-line
:将大括号放在 if 行上.-bl, --braces-after-if-line
:在if
后面加上大括号-blf, --braces-after-func-def-line
:在函数定义行后面加大括号-bls, --braces-after-struct-decl-line
:在结构体声明行后面的行加上大括号-brf, --braces-on-func-def-line
:在函数定义行加上大括号-brs, --braces-on-struct-decl-line
:在结构体声明行上加大括号
设置缩进的选项
- -bli
n
, --brace-indentn
:设置大括号缩进 n 空格 - -cbi
n
, --case-brace-indentationn
:在case
标签后面缩进大括号 n 个空格 - -ci
n
, --continuation-indentationn
:连续缩进 n 个空格 - -cli
n
, --case-indentationn
:case
标签缩进n个空格 - -d
n
, --line-comments-indentationn
:将代码右侧以外的注释缩进设置为 n 个空格 - -i
n
, --indent-leveln
:将缩进级别设置为 n 个空格 - -nut, --no-tabs:使用空格代替制表符
- -ts
n
, --tab-sizen
:设置制表符大小为 n 个空格 - -ut, --use-tabs:使用 tab,这是默认值
设置换行的选项
-bfda, --break-function-decl-args
:声明中所有参数前换行-bc, --blank-lines-after-commas
:声明中逗号后换行-bfde, --break-function-decl-args-end
:在声明的最后一个参数之后换行-hnl, --honour-newlines
:倾向于在输入换行符的位置换行-nbc, --no-blank-lines-after-commas
:不要强制在声明中的逗号后面加换行符-nhnl, --ignore-newlines
:不要在输入换行符的位置换行-nbap, --no-blank-lines-after-procedures
:不要在过程(函数)体之后强制设置空行-nbad, --no-blank-lines-after-declarations
:不要在声明后强制使用空行-ss, --space-special-semicolon
:当for
和while
语句为一行时,强制分号前换行
设置函数类型及其名称的选项
-nbap, --no-blank-lines-after-procedures
:不要在过程(函数)体之后强制设置空行-npsl, --dont-break-procedure-type
:将过程(函数)的类型与其名称放在同一行-pcs, --space-after-procedure-calls
:在被调用过程(函数)的名称和(
之间插入一个空格-psl, --procnames-start-lines
:将过程(函数)的类型放在其名称之前的行中
indent 命令使用示例
本教程的所有示例都是依据如下的 source1.c
讲解的,文件内容如下:
#include <stdio.h>
int main() {
printf("Hello, World!\n");
return 0;
}
使用默认选项格式化文件
不使用任何参数格式化 source1.c
文件:
indent source1.c
输出文件有些系统为本文件,即:source1.c;有些系统为source1.c.indent,自己查看一下文件即可。
输出内容为:
#include <stdio.h>
int
main ()
{
printf ("Hello, World!\n");
return 0;
}
默认情况下函数(过程)的类型与其名称是不在同一行的。
指定输出文件
格式化文件 source1.c
时,指定输出文件为 formatted_source1.c
:
indent source1.c -o formatted_source1.c
输出内容与上一个示例相同,只是输出到了指定的 formatted_source1.c
文件中。
将函数的类型与函数名放在同一行
使用选项 -npsl
或长格式形式 --dont-break-procedure-type
将函数的类型与函数名放在同一行
indent -npsl source1.c -o formatted_source1.c
#include <stdio.h>
int main ()
{
printf ("Hello, World!\n");
return 0;
}
indent 命令选项解释
如下给出了 Linux indent 命令中所有选项的解释说明:
选项 | 解释说明 |
---|---|
-as, --align-with-spaces | 如果使用 Tab 缩进,则使用空格对齐 |
-bad, --blank-lines-after-declarations | 在声明后添加空行 |
-bap, --blank-lines-after-procedures | 在过程(函数)体后添加空行 |
-bbb, --blank-lines-before-block-comments | 在块注释后添加空行 |
-bbo, --break-before-boolean-operator | 倾向于在布尔运算符之前断行 |
-bc, --blank-lines-after-commas | 声明中逗号后换行 |
-bl, --braces-after-if-line | 在 if 后面加上大括号 |
-blf, --braces-after-func-def-line | 在函数定义行后面加大括号 |
-blin , --brace-indentn |
设置大括号缩进 n 空格 |
-bls, --braces-after-struct-decl-line | 在结构体声明行后面的行加上大括号 |
-br, --braces-on-if-line | 将大括号放在 if 行上 |
-brf, --braces-on-func-def-line | 在函数定义行加上大括号 |
-brs, --braces-on-struct-decl-line | 在结构体声明行上加大括号 |
-bs, --Bill-Shannon, --blank-before-sizeof | 在 sizeof 和它的参数之间添加一个空格 |
-cn , --comment-indentationn |
将注释放在第 n 列代码的右侧 |
-cbin , --case-brace-indentationn |
在 case 标签后面缩进大括号 n 个空格 |
-cdn , --declaration-comment-columnn |
在第 n 列声明的右侧加上注释 |
-cdb, --comment-delimiters-on-blank-lines | 注释分隔符单独成行 |
-cdw, --cuddle-do-while | 使 do {} while; 的 while 与前面的 } 紧靠着 |
-ce, --cuddle-else | else 与前面的 } 紧靠着 |
-cin , --continuation-indentationn |
连续缩进 n 个空格 |
-clin , --case-indentationn |
case 标签缩进n个空格 |
-cpn , --else-endif-columnn |
在第 n 列 #else 和 #endif 语句的右侧放置注释 |
-cs, --space-after-cast | 在强制类型转换操作符后加空格 |
-dn , --line-comments-indentationn |
将代码右侧以外的注释缩进设置为 n 个空格 |
-bfda, --break-function-decl-args | 声明中所有参数前换行 |
-bfde, --break-function-decl-args-end | 在声明的最后一个参数之后换行 |
-dj, --left-justify-declarations | 如果使用 -cd 0 ,则声明后的注释在声明后面保持左对齐 |
-din , --declaration-indentationn |
把变量放在第 n 列 |
-fc1, --format-first-column-comments | 在第一列中格式化注释 |
-fca, --format-all-comments | 不要禁用所有注释的格式化 |
-fnc, --fix-nested-comments | 修复嵌套注释 |
-gnu, --gnu-style | 使用GNU编码风格(默认值) |
-gts, --gettext-strings | 将 gettext _("...") 和 N_("...") 视为字符串而不是函数 |
-hnl, --honour-newlines | 倾向于在输入换行符的位置换行 |
-in , --indent-leveln |
将缩进级别设置为 n 个空格 |
-iln , --indent-labeln |
将标签偏移量设置为第 n 列 |
-ipn , --parameter-indentationn |
按 n 个空格缩进旧式函数定义中的参数类型 |
-kr, --k-and-r-style | 使用 Kernighan & Ritchie 编码风格 |
-ln , --line-lengthn |
将非注释行的最大行长设置为 n |
-lcn , --comment-line-lengthn |
将注释格式化的最大行长设置为 n |
-linux, --linux-style | 使用 Linux 编码风格 |
-lp, --continue-at-parentheses | 在括号中排列连续的行 |
-lps, --leave-preprocessor-space | 在‘ # ’和预处理器指令之间留空格 |
-nbad, --no-blank-lines-after-declarations | 不要在声明后强制使用空行 |
-nbap, --no-blank-lines-after-procedures | 不要在过程(函数)体之后强制设置空行 |
-nbbo, --break-after-boolean-operator | 不希望在布尔运算符之前中断长行 |
-nbc, --no-blank-lines-after-commas | 不要强制在声明中的逗号后面加换行符 |
-nbfda, --dont-break-function-decl-args | 不要将函数声明中的每个参数放在单独的行上 |
-ncdb, --no-comment-delimiters-on-blank-lines | 不要将注释分隔符单独成行 |
-ncdw, --dont-cuddle-do-while | 不使 do {} while; 的 while 和前面的 } 紧靠着 |
-nce, --dont-cuddle-else | 不使 else 和前面的 } 紧靠着 |
-ncs, --no-space-after-casts | 不要在强制类型转换操作符后面加空格 |
-ndjn, --dont-left-justify-declarations | 声明后的注释与其他语句后的注释一样 |
-nfc1, --dont-format-first-column-comments | 不要将第一列中的注释格式化为正常格式 |
-nfca, --dont-format-comments | 不要格式化任何注释 |
-ngts, --no-gettext-strings | 将 gettext _("...") 和 N_("...") 视为普通函数,这是默认值 |
-nhnl, --ignore-newlines | 不要在输入换行符的位置换行 |
-nip, --no-parameter-indentation | 参数零宽度缩进 |
-nlp, --dont-line-up-parentheses | 不要把括号排成一行 |
-npcs, --no-space-after-function-call-names | 在函数调用中,不要在函数后面加空格 |
-nprs, --no-space-after-parentheses | 不要在每个 ( 之后和 ) 之前加空格 |
-npsl, --dont-break-procedure-type | 将过程(函数)的类型与其名称放在同一行 |
-nsaf, --no-space-after-for | 不要在每个 for 后面加空格 |
-nsai, --no-space-after-if | 不要在每个 if 后面加空格 |
-nsaw, --no-space-after-while | 不要在每个 while 后面加空格 |
-nsc, --dont-star-comments | 不要在注释的左边放 * 字符 |
-nsob, --leave-optional-blank-lines | 不要折叠可选的空行 |
-nss, --dont-space-special-semicolon | 不要强制在某些语句后面的分号前加空格,禁用 -ss |
-ntac, --dont-tab-align-comments | 不要将注释填充到最近的制表符 |
-nut, --no-tabs | 使用空格代替制表符 |
-nv, --no-verbosity | 禁用详细模式 |
-orig, --original | 使用原始的 Berkeley 编码风格 |
-npro, --ignore-profile | 不要读取 .indent.pro 文件 |
-pal, --pointer-align-left | 在指针声明中,在 char* p 类型旁边的空格左侧加上星号 |
-par, --pointer-align-right | 在变量名旁边的空格右边的指针声明中加上星号:char *p 。这是默认行为。 |
-pcs, --space-after-procedure-calls | 在被调用过程(函数)的名称和 ( 之间插入一个空格 |
-pin , --paren-indentationn |
指定在语句中断时每个开括号 ( 添加额外 n 个空格缩进 |
-pmt, --preserve-mtime | 保留对输出文件的访问和修改时间 |
-ppin , --preprocessor-indentationn |
指定预处理器条件语句的缩进 |
-prs, --space-after-parentheses | 在每个 ( 之后和 ) 之前加一个空格 |
-psl, --procnames-start-lines | 将过程(函数)的类型放在其名称之前的行中 |
-saf, --space-after-for | 在每个 for 后面加空格 |
-sai, --space-after-if | 在每个 if 后面加一个空格 |
-sar, --spaces-around-initializers | 在初始化中的 { 之后和 } 之前加一个空格 |
-saw, --space-after-while | 在每个 while 后面加一个空格 |
-sbin , --struct-brace-indentationn |
结构体、联合或枚举的大括号缩进 n 个空格 |
-sc, --start-left-side-of-comments | 在注释的左边加上 * 字符 |
-slc, --single-line-conditionals | 允许不带括号的条件语句(if, else等)将其内部语句放在同一行 |
-sob, --swallow-optional-blank-lines | 折叠可选的空行 |
-ss, --space-special-semicolon | 当 for 和while 语句为一行时,强制分号前换行 |
-st, --standard-output | 写入标准输出 |
-T | 告诉缩进 typennames 的名称 |
-tsn , --tab-sizen |
设置制表符大小为 n 个空格 |
-ut, --use-tabs | 使用 tab,这是默认值 |
-v, --verbose | 启用详细模式 |
-version | 输出 indent 的版本号 |
结语
在本教程中我们介绍了如何使用 indent ( indent ) 命令格式化 C / C++ 源代码。indent ( indent ) 命令是一个强大的工具,它可以根据一组特定的规则缩进代码,从而帮助您生成格式一致的代码。本教程首先介绍了 indent 的基本语法,然后介绍了使用它来格式化 C/ C++ 源代码。最后,给出了所有选项的解释说明。
该 indent 命令可以在 Linux 系统中使用,也可以在 FreeBSD 系统中使用。