格式化C語言命令indent

indent是linux下一個能力極強的代碼整理軟件,使用他,可以輕鬆的寫出代碼風格十分精良的代碼。
但是indent的參數太多,使用起來不是很容易,怎麼辦呢?

查看
/usr/src/linux-headers-<版本>/scripts/Lindent
文件 ,可以看到一行代碼:

indent -npro -kr -i8 -ts8 -sob -l80 -ss -ncs-cp1


這一行就是linux內核使用indent整理代碼的格式,使用這條命令就可以實現風格十分良好的C或C++代碼

其中-l80是每一行最多80個字母,超出會拆行,如果不喜歡可以使用更長的行字數

使用的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 tobreak 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來縮進 
【轉貼】用indent 進行代碼格式化
2009-03-27 12:01

indent 常用的參數設置

 

以前的用法:indent-kr -cli4 -i4 -nut<filename>

 

改進的用法:indent-kr -cli4 -nut -bl4 -bli0<filename>

 

註釋:

 

-kr, UseKernighan & Ritchie coding style.

 

-cli4,specifies the number of spaces that case labels should be indentedto the right of the containing switch statement.

 

-nut, 不使用空格。

 

-bl4, 大括號內的縮進,這裏爲4個空格。

 

-bli0,“if”語句後大括號的縮進,一般設爲“0”

 

indent工具參數

 

indent是一個常用的LinuxC代碼格式化工具,但是參數太多,很難記憶,目前發現一組參數,可以格式化爲我常用的代碼格式


-bad -bap-bbb -bbo -nbc -bl -bli0 -bls -c33 -cd33 -ncdb -ncdw -nce -cli0-cp33 -cs -d0 -nbfda -nfc1-nfca -hnl -ip5 -l75 -lp -pcs -nprs -saf -sai -saw -nsc -nsob -nss-i4 -ts4 -ut

 

保存在~/.indent.pro,就是默認的indent參數了

3.5. 用 indent進行代碼格式化

一個範例,勝過前言萬語。用 indent 格式化代碼,再從格式化前後的格式變化, 來學習格式化規範。

可以用不同的參數調用 indent,

3.5.1. introduce indent

  1. What is Indent

    The `indent' program can be used to make code easier to read. Itcan also convert from one style. of writing C to another.

  2. Download

    Current Version: GNU indent 2.2.7。 Download: ftp.gnu.org/gnu/indent/indent-2.2.7.tar.gz

3.5.2. Indent參數規範

我們參照GNU,Kernighan & Ritchie,Berkeley風格,制定了自己風格:

  1. indent命令參數:

    -bad -bap -bbb -bbo -nbc -bl -bli0 -bls -c33 -cd33 -ncdb -ncdw -nce-cli0 -cp33 -cs -d0 -nbfda -di2 -nfc1 -nfca -hnl -ip5 -l75 -lp -pcs-nprs -psl -saf -sai -saw -nsc -nsob -nss -i4 -ts4 -ut

  2. indent配置文件

    如上參數可寫入用戶目錄下的文件:".indent.pro",作爲運行indent的確 省參數。

  3. indent配置說明

    表 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 theinput.
    --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來縮進
發佈了62 篇原創文章 · 獲贊 3 · 訪問量 24萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章