使用VSCode做爲IDE開發工具已經有一段時間,期間一直有一個很困擾個人問題,就是關於行尾空格的自動刪除。
通常狀況下,都須要對源碼中的行尾的多餘空格進行刪除,因此我有設置自動刪除行尾空格。可是當我編輯markdown文件時,行尾空格也會被刪除。
WTF!!
markdown文件不是經過行尾三個空格來實現換行的嗎,爲何編輯器要自動去除它空格。網上查了好久也沒找到能解決這個問題的方法。
直到最近,看到一個叫.editorconfig
文件,一會兒就想到了可使用這個來解決。下面就介紹下.editorconfig
。json
顧名思義,EditorConfig就是編輯器配置,就是指統一不一樣編輯器的代碼風格的配置。舉個例子:好比咱們要控制一個多人維護的項目縮進統一用2個空格。那麼每一個人的IDE都是不一樣的,一種方法是幾我的約定好各自配置本身的IDE,可是若是每一個人又維護着多個項目,幾個項目間就會相互影響。因此更好的辦法是由項目自己來控制代碼風格。也就是使用EditorConfig來控制。 markdown
EditorConfig包含一個用於定義代碼格式的文件和一批編輯器插件,這些插件是讓編輯器讀取配置文件並以此來格式化代碼。
EditorConfig的官網: http://editorconfig.org/編輯器
先來看下EditorConfig長什麼樣子:ide
# EditorConfig is awesome: http://EditorConfig.org # top-most EditorConfig file root = true # Unix-style newlines with a newline ending every file [*] end_of_line = lf insert_final_newline = true # Matches multiple files with brace expansion notation # Set default charset [*.{js,py}] charset = utf-8 # 4 space indentation [*.py] indent_style = space indent_size = 4 # Tab indentation (no size specified) [Makefile] indent_style = tab # Indentation override for all JS under lib directory [lib/**.js] indent_style = space indent_size = 2 # Matches the exact files either package.json or .travis.yml [{package.json,.travis.yml}] indent_style = space indent_size = 2
上面的代碼就是EditorConfig的配置demo。
當用IDE打開一個文件時,EditorConfig插件會在打開文件的目錄和其每一級父節點查找.editorconfig
文件,直到找到一個配置了root = true
的配置文件。工具
EditorConfig文件使用INI格式。斜槓(/
)做爲路徑分隔符,#
或者;
做爲註釋。路徑支持通配符:開發工具
通配符 | 說明 |
---|---|
* | 匹配除/以外的任意字符 |
** | 匹配任意字符串 |
? | 匹配任意單個字符 |
[name] | 匹配name字符 |
[!name] | 不匹配name字符 |
[s1,s2,s3] | 匹配給定的字符串 |
[num1..num2] | 匹配num1到mun2直接的整數 |
EditorConfig支持如下屬性:spa
屬性 | 說明 |
---|---|
indent_style | 縮進使用tab 或者space |
indent_size | 縮進爲space 時,縮進的字符數 |
tab_width | 縮進爲tab 時,縮進的寬度 |
end_of_line | 換行符的類型。lf , cr , crlf 三種 |
charset | 文件的charset。有如下幾種類型:latin1 , utf-8 , utf-8-bom , utf-16be , utf-16le |
trim_trailing_whitespace | 是否將行尾空格自動刪除 |
insert_final_newline | 是否使文件以一個空白行結尾 |
root | 代表是最頂層的配置文件,發現設爲true時,纔會中止查找.editorconfig文件 |
最後,回到上面遇到的問題,怎麼解決呢?
在項目根目錄添加文件.editorconfig
,內容以下:插件
# http://editorconfig.org root = true [*] trim_trailing_whitespace = true [*.md] trim_trailing_whitespace = false
這樣就解決了個人問題。固然,每一個項目都要添加。。。。。。code