使用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 內(nèi)核使用 indent 工具整理代碼的格式。
我習慣再加上 -br -nce -nut
這些參數(shù)都是什么意思呢?
| -npro | 不要讀取 indent 的配置文件:.indent.pro |
| -kr | 指定使用 Kernighan&Ritchie 的格式??梢該Q為 -orig,BSD風格 |
| -i8 | 設置縮排的格數(shù)為 8,可以修改 |
| -ts8 | 設置 tab 為 8 個空格,可以修改 |
| -sob | 刪除多余的空白行 |
| -l80 | 代碼每行長度超過 80 換行(對于非注釋行) |
| -ss | 若 for 或者 while 區(qū)段只有一行時,在分號前加上空格 |
| -ncs | no-space-after-casts,不要在 cast 之后空一格 |
| -cp1 | #else、#endif 后面的注釋開始于列 1(前面空一個格) |
| -nut | 不使用 tab 縮進,即 tab 用空格替換 |
| -br | if、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 命令查看吧。
如果你覺得每次都輸入那么一大串太麻煩了,那么可以把你的定制化參數(shù)保存到 ~/.indent.pro
里面寫上
-kr -i4 -ts4 -sob -l80 -ss -ncs -cp1 -br -nce -nut
這樣,直接使用
indent xxx.c -o xxx1.c
就可以了
總結
以上是生活随笔為你收集整理的使用indent格式化代码的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: atoi(s)函数用法
- 下一篇: python生成pdf文档_使用Pyth