利用 Vim 開發 Android app 時常會需要將之前的xml 檔案內容貼上到目前檔案中
由於在 .vimrc 中設定自動縮排 set autoindent 的關係,貼上後就變成這樣:
幹你娘咧這三小,於是馬上想起了 Vim 內建排版的指令: gg + V + G + =
結果變成這樣:
這個排版根本就不適用於 xml 檔案!
好佳在發現了 xmllint 這個指令就可以幫 xml 做好完美排版
xmllint --format source.xml > target.xml
但是這樣還是很難用,因為每次都要導到另一個檔案去,無法修改原檔案
於是哥寫了一支 shell script 放到 ~/bin 底下
~/bin/xmlformat.sh
#!/bin/sh
if [ "$1" != "" ]; then
if [ -f $1 ]; then
xmllint --format $1 > temp.xml
mv temp.xml $1
else
echo error! $1 does not exist
fi
else
echo error! use: xmlformat file_name
fi
再修改 ~/.bashrc ,加入
alias xmlf='. xmlformat.sh'
然後就大功告成啦
以後貼完 xml 內容後,只需要存檔,然後輸入:
xmlf res/values/strings.xml
精美的排版就完成啦!
全站熱搜