VimだけでXML Reformat

Vim「だけ」でなんちゃってXML Reformat

まっつんさんの記事のパクリです。

たとえばこういう XML があったとして…

<status><created_at>Mon Feb 06 21:07:52 +0000 2012</created_at><id>166629198054690816</id><text>Post-Bowl Twitter analysis http://t.co/OYYSRSew http://t.co/M0AtLQVd</text><source>web</source><truncated>false</truncated><favorited>false</favorited><in_reply_to_status_id></in_reply_to_status_id><in_reply_to_user_id></in_reply_to_user_id><in_reply_to_screen_name></in_reply_to_screen_name><retweet_count>454</retweet_count><retweeted>false</retweeted><user><id>783214</id><name>Twitter</name><screen_name>twitter</screen_name><location>San Francisco, CA</location><description>Always wondering what's happening. </description><profile_image_url>http://a0.twimg.com/profile_images/1124040897/at-twitter_normal.png</profile_image_url><profile_image_url_https>https://si0.twimg.com/profile_images/1124040897/at-twitter_normal.png</profile_image_url_https><url>http://blog.twitter.com/</url><protected>false</protected><followers_count>7625563</followers_count><profile_background_color>ACDED6</profile_background_color><profile_text_color>333333</profile_text_color><profile_link_color>038543</profile_link_color><profile_sidebar_fill_color>F6F6F6</profile_sidebar_fill_color><profile_sidebar_border_color>EEEEEE</profile_sidebar_border_color><friends_count>822</friends_count><created_at>Tue Feb 20 14:35:54 +0000 2007</created_at><favourites_count>16</favourites_count><utc_offset>-28800</utc_offset><time_zone>Pacific Time (US &amp; Canada)</time_zone><profile_background_image_url>http://a1.twimg.com/profile_background_images/378245879/Twitter_1544x2000.png</profile_background_image_url><profile_background_image_url_https>https://si0.twimg.com/profile_background_images/378245879/Twitter_1544x2000.png</profile_background_image_url_https><profile_background_tile>true</profile_background_tile><profile_use_background_image>true</profile_use_background_image><notifications>false</notifications><geo_enabled>true</geo_enabled><verified>true</verified><following>true</following><statuses_count>1266</statuses_count><lang>en</lang><contributors_enabled>true</contributors_enabled><follow_request_sent>false</follow_request_sent><listed_count>68708</listed_count><show_all_inline_media>true</show_all_inline_media><default_profile>false</default_profile><default_profile_image>false</default_profile_image><is_translator>false</is_translator></user><geo/><coordinates/><place/><possibly_sensitive>false</possibly_sensitive><contributors><user_id>7694352</user_id></contributors></status>

Vim のバッファにコピペで貼りつけた上で次のようにコマンドを叩くと…

:set ft=xml
:%s/></>^M</g
:%s/>\(\S\)/>^M\1/g
:%s/\(\S\)</\1^M</g
gg=G

こんなんなります。^M<CTRL-V><ENTER> で入力しましょう。めんどくさければマクロ化や関数化しても良いですが、まぁその場で組み立てれば十分です。

<status>
  <created_at>
    Mon Feb 06 21:07:52 +0000 2012
  </created_at>
  <id>
    166629198054690816
  </id>
  <text>
    Post-Bowl Twitter analysis http://t.co/OYYSRSew http://t.co/M0AtLQVd
  </text>
  <source>
    web
  </source>
  <truncated>
    false
  </truncated>
  <favorited>
    false
  </favorited>
  <in_reply_to_status_id>
  </in_reply_to_status_id>
  <in_reply_to_user_id>
  </in_reply_to_user_id>
  <in_reply_to_screen_name>
  </in_reply_to_screen_name>
  <retweet_count>
    454
  </retweet_count>
  <retweeted>
    false
  </retweeted>
  <user>
    <id>
      783214
    </id>
    <name>
      Twitter
    </name>
    <screen_name>
      twitter
    </screen_name>
    <location>
      San Francisco, CA
    </location>
    <description>
      Always wondering what's happening. </description>
    <profile_image_url>
      http://a0.twimg.com/profile_images/1124040897/at-twitter_normal.png
    </profile_image_url>
    <profile_image_url_https>
      https://si0.twimg.com/profile_images/1124040897/at-twitter_normal.png
    </profile_image_url_https>
    <url>
      http://blog.twitter.com/
    </url>
    <protected>
      false
    </protected>
    <followers_count>
      7625563
    </followers_count>
    <profile_background_color>
      ACDED6
    </profile_background_color>
    <profile_text_color>
      333333
    </profile_text_color>
    <profile_link_color>
      038543
    </profile_link_color>
    <profile_sidebar_fill_color>
      F6F6F6
    </profile_sidebar_fill_color>
    <profile_sidebar_border_color>
      EEEEEE
    </profile_sidebar_border_color>
    <friends_count>
      822
    </friends_count>
    <created_at>
      Tue Feb 20 14:35:54 +0000 2007
    </created_at>
    <favourites_count>
      16
    </favourites_count>
    <utc_offset>
      -28800
    </utc_offset>
    <time_zone>
      Pacific Time (US &amp; Canada)
    </time_zone>
    <profile_background_image_url>
      http://a1.twimg.com/profile_background_images/378245879/Twitter_1544x2000.png
    </profile_background_image_url>
    <profile_background_image_url_https>
      https://si0.twimg.com/profile_background_images/378245879/Twitter_1544x2000.png
    </profile_background_image_url_https>
    <profile_background_tile>
      true
    </profile_background_tile>
    <profile_use_background_image>
      true
    </profile_use_background_image>
    <notifications>
      false
    </notifications>
    <geo_enabled>
      true
    </geo_enabled>
    <verified>
      true
    </verified>
    <following>
      true
    </following>
    <statuses_count>
      1266
    </statuses_count>
    <lang>
      en
    </lang>
    <contributors_enabled>
      true
    </contributors_enabled>
    <follow_request_sent>
      false
    </follow_request_sent>
    <listed_count>
      68708
    </listed_count>
    <show_all_inline_media>
      true
    </show_all_inline_media>
    <default_profile>
      false
    </default_profile>
    <default_profile_image>
      false
    </default_profile_image>
    <is_translator>
      false
    </is_translator>
  </user>
  <geo/>
  <coordinates/>
  <place/>
  <possibly_sensitive>
    false
  </possibly_sensitive>
  <contributors>
    <user_id>
      7694352
    </user_id>
  </contributors>
</status>

手元の環境にPythonが入ってなくても安心です。そうスパルタンVimならね。