Linux 代码格式化工具 indent
以下是我最喜歡的代碼格式化腳本:
#!/bin/shif [ $# -lt 1 ]; thenecho "Usage: $0 <file>"exit 1 fi# format a source file(*.c, *.h) formatSrcfile() {indent -npro -nip -lp -npsl -npcs -i4 -ts4 -sob -l140 -ss -nsaf -nsai -nsaw -bl -bli 0 $1rm -f *~ }for i in $* doformatSrcfile $1shift done=========================================================================================================
參考以下文章: http://blog.csdn.net/httphttpcn/article/details/5940848
linux kernel有一個(gè)腳本Lindent,可以幫助你將自己的代碼做一個(gè)好的代碼風(fēng)格格式化。
如:/usr/src/linux-headers-2.6.22-14/scripts/Lindent
常用的設(shè)置:
indent -npro -kr -i4 -ts4 -sob -l80 -ss -bl -bli 0
(這個(gè)風(fēng)格我喜歡)
參數(shù)說(shuō)明:
-npro或--ignore-profile 不要讀取indent的配置文件.indent.pro。
-kr 指定使用Kernighan&Ritchie的格式。可以換為 -orig,BSD風(fēng)格
-i4 --indent-level 設(shè)置縮排的格數(shù)為4。
-ts4 設(shè)置tab的長(zhǎng)度
-sob或--swallow-optional-blank-lines 刪除多余的空白行。
-l80 代碼超過(guò)80換行
-ss或--space-special-semicolon 若for區(qū)段只有一行時(shí),在分號(hào)前加上空格。
-ncs或--no-space-after-casts 不要在cast之后空一格。
-bl {分行顯示
-bli 0 括號(hào)縮進(jìn)為0
我較常用的是:
indent -npro -nip -lp -npsl -npcs -i4 -ts4 -sob -l140 -ss -nsaf -nsai -nsaw -bl -bli 0 代碼文件名
功能說(shuō)明:調(diào)整C原始代碼文件的格式。
語(yǔ) 法:indent [參數(shù)][源文件] 或 indent [參數(shù)][源文件][-o 目標(biāo)文件]
補(bǔ)充說(shuō)明:indent可辨識(shí)C的原始代碼文件,并加以格式化,以方便程序設(shè)計(jì)師閱讀。
參 數(shù):
-bad或--blank-lines-after-declarations 在聲明區(qū)段或加上空白行。
-bap或--blank-lines-after-procedures 在程序或加上空白行。
-bbb或--blank-lines-after-block-comments 在注釋區(qū)段后加上空白行。
-bc或--blank-lines-after-commas 在聲明區(qū)段中,若出現(xiàn)逗號(hào)即換行。
-bl或--braces-after-if-line if(或是else,for等等)與后面執(zhí)行區(qū)段的"{"不同行,且"}"自成一行。
-bli<縮排格數(shù)>或--brace-indent<縮排格數(shù)> 設(shè)置{ }縮排的格數(shù)。
-br或--braces-on-if-line if(或是else,for等等)與后面執(zhí)行跛段的"{"不同行,且"}"自成一行。
-bs或--blank-before-sizeof 在sizeof之后空一格。
-c<欄數(shù)>或--comment-indentation<欄數(shù)> 將注釋置于程序碼右側(cè)指定的欄位。
-cd<欄數(shù)>或--declaration-comment-column<欄數(shù)> 將注釋置于聲明右側(cè)指定的欄位。
-cdb或--comment-delimiters-on-blank-lines 注釋符號(hào)自成一行。
-ce或--cuddle-else 將else置于"}"(if執(zhí)行區(qū)段的結(jié)尾)之后。
-ci<縮排格數(shù)>或--continuation-indentation<縮排格數(shù)> 敘述過(guò)長(zhǎng)而換行時(shí),指定換行后縮排的格數(shù)。
-cli<縮排格數(shù)>或--case-indentation-<縮排格數(shù)> 使用case時(shí),switch縮排的格數(shù)。
-cp<欄數(shù)>或-else-endif-column<欄數(shù)> 將注釋置于else與elseif敘述右側(cè)定的欄位。
-cs或--space-after-cast 在cast之后空一格。
-d<縮排格數(shù)>或-line-comments-indentation<縮排格數(shù)> 針對(duì)不是放在程序碼右側(cè)的注釋,設(shè)置其縮排格數(shù)。
-di<欄數(shù)>或--declaration-indentation<欄數(shù)> 將聲明區(qū)段的變量置于指定的欄位。
-fc1或--format-first-column-comments 針對(duì)放在每行最前端的注釋,設(shè)置其格式。
-fca或--format-all-comments 設(shè)置所有注釋的格式。
-gnu或--gnu-style. 指定使用GNU的格式,此為預(yù)設(shè)值。
-i<格數(shù)>或--indent-level<格數(shù)> 設(shè)置縮排的格數(shù)。
-ip<格數(shù)>或--parameter-indentation<格數(shù)> 設(shè)置參數(shù)的縮排格數(shù)。
-kr或--k-and-r-style. 指定使用Kernighan&Ritchie的格式。
-lp或--continue-at-parentheses 敘述過(guò)長(zhǎng)而換行,且敘述中包含了括弧時(shí),將括弧中的每行起始欄位內(nèi)容垂直對(duì)其排列。
-nbad或--no-blank-lines-after-declarations 在聲明區(qū)段后不要加上空白行。
-nbap或--no-blank-lines-after-procedures 在程序后不要加上空白行。
-nbbb或--no-blank-lines-after-block-comments 在注釋區(qū)段后不要加上空白行。
-nbc或--no-blank-lines-after-commas 在聲明區(qū)段中,即使出現(xiàn)逗號(hào),仍舊不要換行。
-ncdb或--no-comment-delimiters-on-blank-lines 注釋符號(hào)不要自成一行。
-nce或--dont-cuddle-else 不要將else置于"}"之后。
-ncs或--no-space-after-casts 不要在cast之后空一格。
-nfc1或--dont-format-first-column-comments 不要格式化放在每行最前端的注釋。
-nfca或--dont-format-comments 不要格式化任何的注釋。
-nip或--no-parameter-indentation 參數(shù)不要縮排。
-nlp或--dont-line-up-parentheses 敘述過(guò)長(zhǎng)而換行,且敘述中包含了括弧時(shí),不用將括弧中的每行起始欄位垂直對(duì)其排列。
-npcs或--no-space-after-function-call-names 在調(diào)用的函數(shù)名稱(chēng)之后,不要加上空格。
-npro或--ignore-profile 不要讀取indent的配置文件.indent.pro。
-npsl或--dont-break-procedure-type 程序類(lèi)型與程序名稱(chēng)放在同一行。
-nsc或--dont-star-comments 注解左側(cè)不要加上星號(hào)(*)。
-nsob或--leave-optional-semicolon 不用處理多余的空白行。
-nss或--dont-space-special-semicolon 若for或while區(qū)段僅有一行時(shí),在分號(hào)前不加上空格。
-nv或--no-verbosity 不顯示詳細(xì)的信息。
-orig或--original 使用Berkeley的格式。
-pcs或--space-after-procedure-calls 在調(diào)用的函數(shù)名稱(chēng)與"{"之間加上空格。
-psl或--procnames-start-lines 程序類(lèi)型置于程序名稱(chēng)的前一行。
-sc或--start-left-side-of-comments 在每行注釋左側(cè)加上星號(hào)(*)。
-sob或--swallow-optional-blank-lines 刪除多余的空白行。
-ss或--space-special-semicolon 若for或swile區(qū)段今有一行時(shí),在分號(hào)前加上空格。
-st或--standard-output 將結(jié)果顯示在標(biāo)準(zhǔn)輸出設(shè)備。
-T 數(shù)據(jù)類(lèi)型名稱(chēng)縮排。
-ts<格數(shù)>或--tab-size<格數(shù)> 設(shè)置tab的長(zhǎng)度。
-v或--verbose 執(zhí)行時(shí)顯示詳細(xì)的信息。
-version 顯示版本信息。
alias myindent='indent -npro -kr -bl -nce -bli0 -i2 -ts2 -sob -l80 -nfc1 -ss -ncs'
Common styles
There are several common styles of C code, including the GNU style, the Kernighan & Ritchie style, and the original Berkeley style. A style may be selected with a singlebackground option, which specifies a set of values for all other options. However, explicitly specified options always override options implied by a background option.
?
As of version 1.2, the default style of GNU indent is the GNU style. Thus, it is no longer necessary to specify the option ‘-gnu’ to obtain this format, although doing so will not cause an error. Option settings which correspond to the GNU style are:
| ? | <strong><em><span style="font-size:12px;">-nbad -bap -nbc -bbo -bl -bli2 -bls -ncdb -nce -cp1 -cs -di2 -ndj -nfc1 -nfca -hnl -i2 -ip5 -lp -pcs -nprs -psl -saf -sai -saw -nsc -nsob</span> </em></strong> |
The GNU coding style is that preferred by the GNU project. It is the style that the GNU Emacs C mode encourages and which is used in the C portions of GNU Emacs. (People interested in writing programs for Project GNU should get a copy ofThe GNU Coding Standards, which also covers semantic and portability issues such as memory usage, the size of integers, etc.)
?
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:
| ? | <strong><em><span style="font-size:12px;">-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</span></em></strong> |
Kernighan & Ritchie style does not put comments to the right of code in the same column at all times (nor does it use only one space to the right of the code), so for this styleindent has arbitrarily chosen column 33.
?
The style of the original Berkeley indent may be obtained by specifying ‘-orig’ (or by specifying ‘--original’, using the long option name). This style is equivalent to the following settings:
| ? | <em><strong><span style="font-size:12px;">-nbad -nbap -bbo -bc -br -brs -c33 -cd33 -cdb -ce -ci4 -cli0 -cp33 -di16 -fc1 -fca -hnl -i4 -ip4 -l75 -lp -npcs -nprs -psl -saf -sai -saw -sc -nsob -nss -ts8</span> </strong></em> |
?
The Linux style is used in the linux kernel code and drivers. Code generally has to follow the Linux coding style to be accepted. This style is equivalent to the following settings:
| ? | <em><strong><span style="font-size:12px;">-nbad -bap -nbc -bbo -hnl -br -brs -c33 -cd33 -ncdb -ce -ci4 -cli0 -d0 -di1 -nfc1 -i8 -ip0 -l80 -lp -npcs -nprs -npsl -sai -saf -saw -ncs -nsc -sob -nfca -cp33 -ss -ts8 -il1</span> </strong></em> |
表 1. Indent代碼格式化說(shuō)明
| --blank-lines-after-declarations | bad | 變量聲明后加空行 |
| --blank-lines-after-procedures | bap | 函數(shù)結(jié)束后加空行 |
| --blank-lines-before-block-comments | bbb | 塊注釋前加空行 |
| --break-before-boolean-operator | bbo | 較長(zhǎng)的行,在邏輯運(yùn)算符前分行 |
| --blank-lines-after-commas | nbc | 變量聲明中,逗號(hào)分隔的變量不分行 |
| --braces-after-if-line | bl | "if"和"{"分做兩行 |
| --brace-indent 0 | bli0 | "{"不繼續(xù)縮進(jìn) |
| --braces-after-struct-decl-line | bls | 定義結(jié)構(gòu),"struct"和"{"分行 |
| --comment-indentationn | c33 | 語(yǔ)句后注釋開(kāi)始于行33 |
| --declaration-comment-columnn | cd33 | 變量聲明后注釋開(kāi)始于行33 |
| --comment-delimiters-on-blank-lines | ncdb | 不將單行注釋變?yōu)閴K注釋 |
| --cuddle-do-while | ncdw | "do --- while"的"while"和其前面的"}"另起一行 |
| --cuddle-else | nce | "else"和其前面的"}"另起一行 |
| --case-indentation 0 | cli0 | switch中的case語(yǔ)句所進(jìn)0個(gè)空格 |
| --else-endif-columnn | cp33 | #else, #endif后面的注釋開(kāi)始于行33 |
| --space-after-cast | cs | 在類(lèi)型轉(zhuǎn)換后面加空格 |
| --line-comments-indentation n | d0 | 單行注釋(不從1列開(kāi)始的),不向左縮進(jìn) |
| --break-function-decl-args | nbfda | 關(guān)閉:函數(shù)的參數(shù)一個(gè)一行 |
| --declaration-indentationn | di2 | 變量聲明,變量開(kāi)始于2行,即不必對(duì)齊 |
| --format-first-column-comments | nfc1 | 不格式化起于第一行的注釋 |
| --format-all-comments | nfca | 不開(kāi)啟全部格式化注釋的開(kāi)關(guān) |
| --honour-newlines | hnl | Prefer to break long lines at the position of newlines in the input. |
| --indent-leveln | i4 | 設(shè)置縮進(jìn)多少字符,如果為tab的整數(shù)倍,用tab來(lái)縮進(jìn),否則用空格填充。 |
| --parameter-indentationn | ip5 | 舊風(fēng)格的函數(shù)定義中參數(shù)說(shuō)明縮進(jìn)5個(gè)空格 |
| --line-length 75 | l75 | 非注釋行最長(zhǎng)75 |
| --continue-at-parentheses | lp | 續(xù)行從上一行出現(xiàn)的括號(hào)開(kāi)始 |
| --space-after-procedure-calls | pcs | 函數(shù)和"("之間插入一個(gè)空格 |
| --space-after-parentheses | nprs | 在"("后")"前不插入空格 |
| --procnames-start-lines | psl | 將函數(shù)名和返回類(lèi)型放在兩行定義 |
| --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語(yǔ)句,在";"前不加空。 |
| --tab-size | ts4 | 一個(gè)tab為4個(gè)空格(要能整除"-in") |
| --use-tabs | ut | 使用tab來(lái)縮進(jìn) |
總結(jié)
以上是生活随笔為你收集整理的Linux 代码格式化工具 indent的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: Linux系统开机自启流程
- 下一篇: Linux——JDK的部署