Linux 代碼格式化工具 indent__Linux

來源:互聯網
上載者:User

linux kernel有一個指令碼Lindent,沒有的可以裝一下,

sudo apt-get install indent

可以協助你將自己的代碼做一個好的代碼風格格式化。
如:/usr/src/linux-headers-2.6.22-14/scripts/Lindent

常用的設定:
indent -npro -kr -i4 -ts4 -sob -l80 -ss -bl -bli 0

(這個風格我喜歡)

參數說明:
-npro或--ignore-profile  不要讀取indent的設定檔.indent.pro。
-kr  指定使用Kernighan&Ritchie的格式。可以換為 -orig,BSD風格
-i4  --indent-level 設定縮排的格數為4。
-ts4 設定tab的長度
-sob或--swallow-optional-blank-lines  刪除多餘的空白行。
-l80 代碼超過80換行
-ss或--space-special-semicolon  若for區段只有一行時,在分號前加上空格。 
-ncs或--no-space-after-casts  不要在cast之後空一格。
-bl {分行顯示
-bli 0 括弧縮排為0

我較常用的是:

indent -npro -nip -nlp -npsl -i4 -ts4 -sob -l200 -ss -bl -bli 0 代碼檔案名稱


功能說明:調整C原始代碼檔案的格式。

語  法:indent [參數][源檔案] 或 indent [參數][源檔案][-o 目標檔案]

補充說明:indent可辨識C的原始代碼檔案,並加以格式化,以方便程式設計師閱讀。

參  數:
-bad或--blank-lines-after-declarations  在聲明區段或加上空白行。
-bap或--blank-lines-after-procedures  在程式或加上空白行。
-bbb或--blank-lines-after-block-comments  在注釋區段後加上空白行。
-bc或--blank-lines-after-commas  在聲明區段中,若出現逗號即換行。
-bl或--braces-after-if-line  if(或是else,for等等)與後面執行區段的"{"不同行,且"}"自成一行。
-bli<縮排格數>或--brace-indent<縮排格數>  設定{ }縮排的格數。
-br或--braces-on-if-line  if(或是else,for等等)與後面執行跛段的"{"不同行,且"}"自成一行。
-bs或--blank-before-sizeof  在sizeof之後空一格。
-c<欄數>或--comment-indentation<欄數>  將注釋置於程式碼右側指定的欄位。
-cd<欄數>或--declaration-comment-column<欄數>  將注釋置於聲明右側指定的欄位。
-cdb或--comment-delimiters-on-blank-lines  注釋符號自成一行。
-ce或--cuddle-else  將else置於"}"(if執行區段的結尾)之後。
-ci<縮排格數>或--continuation-indentation<縮排格數>  敘述過長而換行時,指定換行後縮排的格數。
-cli<縮排格數>或--case-indentation-<縮排格數>  使用case時,switch縮排的格數。
-cp<欄數>或-else-endif-column<欄數>  將注釋置於else與elseif敘述右側定的欄位。
-cs或--space-after-cast  在cast之後空一格。
-d<縮排格數>或-line-comments-indentation<縮排格數>  針對不是放在程式碼右側的注釋,設定其縮排格數。
-di<欄數>或--declaration-indentation<欄數>  將聲明區段的變數置於指定的欄位。
-fc1或--format-first-column-comments  針對放在每行最前端的注釋,設定其格式。
-fca或--format-all-comments  設定所有注釋的格式。
-gnu或--gnu-style.  指定使用GNU的格式,此為預設值。
-i<格數>或--indent-level<格數>  設定縮排的格數。
-ip<格數>或--parameter-indentation<格數>  設定參數的縮排格數。
-kr或--k-and-r-style.  指定使用Kernighan&Ritchie的格式。
-lp或--continue-at-parentheses  敘述過長而換行,且敘述中包含了括弧時,將括弧中的每行起始欄位內容垂直對其排列。
-nbad或--no-blank-lines-after-declarations  在聲明區段後不要加上空白行。
-nbap或--no-blank-lines-after-procedures  在程式後不要加上空白行。
-nbbb或--no-blank-lines-after-block-comments  在注釋區段後不要加上空白行。
-nbc或--no-blank-lines-after-commas  在聲明區段中,即使出現逗號,仍舊不要換行。
-ncdb或--no-comment-delimiters-on-blank-lines  注釋符號不要自成一行。
-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  參數不要縮排。
-nlp或--dont-line-up-parentheses  敘述過長而換行,且敘述中包含了括弧時,不用將括弧中的每行起始欄位垂直對其排列。
-npcs或--no-space-after-function-call-names  在調用的函數名稱之後,不要加上空格。
-npro或--ignore-profile  不要讀取indent的設定檔.indent.pro。
-npsl或--dont-break-procedure-type  程式類型與程式名稱放在同一行。
-nsc或--dont-star-comments  註解左側不要加上星號(*)。
-nsob或--leave-optional-semicolon  不用處理多餘的空白行。
-nss或--dont-space-special-semicolon  若for或while區段僅有一行時,在分號前不加上空格。
-nv或--no-verbosity  不顯示詳細的資訊。
-orig或--original  使用Berkeley的格式。
-pcs或--space-after-procedure-calls  在調用的函數名稱與"{"之間加上空格。
-psl或--procnames-start-lines  程式類型置於程式名稱的前一行。
-sc或--start-left-side-of-comments  在每行注釋左側加上星號(*)。
-sob或--swallow-optional-blank-lines  刪除多餘的空白行。
-ss或--space-special-semicolon  若for或swile區段今有一行時,在分號前加上空格。
-st或--standard-output  將結果顯示在標準輸出裝置。
-T  資料類型名稱縮排。
-ts<格數>或--tab-size<格數>  設定tab的長度。
-v或--verbose  執行時顯示詳細的資訊。
-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 single background 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:18px;">-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 of The 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:18px;">-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 style indent 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:18px;">-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:18px;">-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代碼格式化說明

使用的indent參數 含義
--blank-lines-after-declarations bad 變數聲明後加空行
--blank-lines-after-procedures bap 函數結束後加空行
--blank-lines-before-block-comments bbb 塊注釋前加空行
--break-before-boolean-operator bbo 較長的行,在邏輯運算子前分行
--blank-lines-after-commas nbc 變數聲明中,逗號分隔的變數不分行
--braces-after-if-line bl "if"和"{"分做兩行
--brace-indent 0 bli0 "{"不繼續縮排
--braces-after-struct-decl-line bls 定義結構,"struct"和"{"分行
--comment-indentationn c33 語句後注釋開始於行33
--declaration-comment-columnn cd33 變數聲明後注釋開始於行33
--comment-delimiters-on-blank-lines ncdb 不將單行注釋變為塊注釋
--cuddle-do-while ncdw "do --- while"的"while"和其前面的"}"另起一行
--cuddle-else nce "else"和其前面的"}"另起一行
--case-indentation 0 cli0 switch中的case語句所進0個空格
--else-endif-columnn cp33 #else, #endif後面的注釋開始於行33
--space-after-cast cs 在類型轉換後面加空格
--line-comments-indentation n d0 單行注釋(不從1列開始的),不負值縮排
--break-function-decl-args nbfda 關閉:函數的參數一個一行
--declaration-indentationn di2 變數聲明,變數開始於2行,即不必對齊
--format-first-column-comments nfc1 不格式化起於第一行的注釋
--format-all-comments nfca 不開啟全部格式化注釋的開關
--honour-newlines hnl Prefer to break long lines at the position of newlines in the input.
--indent-leveln i4 設定縮排多少字元,如果為tab的整數倍,用tab來縮排,否則用空格填充。
--parameter-indentationn ip5 舊風格的函數定義中參數說明縮排5個空格
--line-length 75 l75 非注釋行最長75
--continue-at-parentheses lp 續行從上一行出現的括弧開始
--space-after-procedure-calls pcs 函數和"("之間插入一個空格
--space-after-parentheses nprs 在"("後")"前不插入空格
--procnames-start-lines psl 將函數名和傳回型別放在兩行定義
--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語句,在";"前不加空。
--tab-size ts4 一個tab為4個空格(要能整除"-in")
--use-tabs ut 使用tab來縮排

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.