linux下代碼格式化工具:indent
ubuntu 下:$ sudo apt-get install indent
常用的設定:
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
注意:
<1> -i4 -ts4同時設定
-i4 設定縮排多少字元,如果為tab的整數倍,用tab來縮排,否則用空格填充。
-i4 -ts4並不能就按照4個空格縮排,而是按一個tab。
當使用vim查看代碼時,
只有在.vimrc中設定 set ts=4
代碼才會按4個空格縮排。
<2> fileformats=unix, dos
在vim命令模式下,輸入“:set ffs”,得到: fileformats=unix,dos
所以vim同時支援 unix和dos格式(不同的行結束符)
輸入“:set ff”,可以得到當前檔案格式。
如果需要轉換的檔案的格式是dos的,在使用indent轉換後會多出很多^M。
解決辦法:
:set ff=unix
:wq
$ indent ...
這樣就可以了。
參考:
http://hi.baidu.com/houhou1999/blog/item/4983d41b680825c5ac6e75cd.html