DEV Community

KOGA Mitsuhiro
KOGA Mitsuhiro

Posted on • Originally published at qiita.com

vimでお手軽にXMLを整形する

開発などで改行がないXMLをvimでお手軽に整形するコマンドです。

:%s/></>\r</g | filetype indent on | setf xml | normal gg=G

もしくは

:%s/></>^M</g | filetype indent on | setf xml | normal gg=G

^M^Mの2文字ではなく、Ctrl-Vの後にEnterを入力した1文字です。

インデント不要だったりXMLが巨大で時間がかかる場合は次のように置換だけにすると高速です。

:%s/></>\r</g
:%s/></>^M</g

整形例

<foo><bar><qiita>example</qiita></bar></foo>

こんなテキストが

<foo>
    <bar>
        <qiita>example</qiita>
    </bar>
</foo>

このように整形されます。

Top comments (0)

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay