How to format XML in Smultron
In my quest to find a great text/code editor for Mac OS-X, so far, I've chosen Smultron. It has a nice set of features for what I need: It supports Unicode, can display East Asian characters just fine (shame on you TextMate), has project support, syntax highlighting, auto-complete and more, and it's free. Most importantly, it also has support to configure and run shell commands, which means you can use other apps to modify the text. One thing I found missing right off the bat was the ability to format, tabify, and indent XML, so it's time to find a command to format XML and integrate it with Smultron.
There is a fantastic command "tidy", which can format and indent XML elements among other things. The tidy command came with my Mac Mini (running Leopard), but if you need to install it, you can find tidy at http://tidy.sourceforge.net.
The command can be run like so:
This will indent the UTF-8 formatted XML within filename.xml.
If the command doesn't work for you, the command options might be different for your version of tidy. You can check the tidy help by running
Now that we know how to use tidy, we can create a command within Smultron. While in Smultron, press Command-B to open the Commands window.
Choose a Collection where you want to create the new command. I created a new collection called "Format". To do this, click the "New Collection" button, give it a the name "Format". Make sure the "Format" collection is selected, then click the "New Command" button (a plus symbol). Name the command something like "XML UTF-8" and in the bottom pane, type the following:
The %%p is a variable that will be the full path to the document that is currently selected within Smultron. You can also provide a shortcut key to run the command without having to use the menus.
That's it! Now you can try it out. Write a chunk of XML that is not yet indented. The XML must be well-formed, or tidy will notify you of errors. Once you have some XML to try it out with, and you have the XML file saved and loaded into Smultron, go the Tools menu -> Commands -> Format -> XML UTF-8. You should see a output dialog window pop-up to display the output from tidy. This is where tidy will display any errors if it has found any.
One thing you'll see is that your XML still doesn't appear to have been formatted. You simply need to reload the XML file in Smultron. The command to reload the file is actually the "Revert to Saved" command. In this case, reverting to the saved version will load the more current version of the file. So to reload the file, press Shift-Command-U, or select "Revert" from the File menu.
If you have a better way of handling this, I'd love to hear it. Sound off in the comments.
There is a fantastic command "tidy", which can format and indent XML elements among other things. The tidy command came with my Mac Mini (running Leopard), but if you need to install it, you can find tidy at http://tidy.sourceforge.net.
The command can be run like so:
/usr/bin/tidy -xml -utf8 -indent -modify filename.xmlThis will indent the UTF-8 formatted XML within filename.xml.
If the command doesn't work for you, the command options might be different for your version of tidy. You can check the tidy help by running
tidy -?Now that we know how to use tidy, we can create a command within Smultron. While in Smultron, press Command-B to open the Commands window.
Choose a Collection where you want to create the new command. I created a new collection called "Format". To do this, click the "New Collection" button, give it a the name "Format". Make sure the "Format" collection is selected, then click the "New Command" button (a plus symbol). Name the command something like "XML UTF-8" and in the bottom pane, type the following:
/usr/bin/tidy -xml -utf8 -indent -modify %%pThe %%p is a variable that will be the full path to the document that is currently selected within Smultron. You can also provide a shortcut key to run the command without having to use the menus.
That's it! Now you can try it out. Write a chunk of XML that is not yet indented. The XML must be well-formed, or tidy will notify you of errors. Once you have some XML to try it out with, and you have the XML file saved and loaded into Smultron, go the Tools menu -> Commands -> Format -> XML UTF-8. You should see a output dialog window pop-up to display the output from tidy. This is where tidy will display any errors if it has found any.
One thing you'll see is that your XML still doesn't appear to have been formatted. You simply need to reload the XML file in Smultron. The command to reload the file is actually the "Revert to Saved" command. In this case, reverting to the saved version will load the more current version of the file. So to reload the file, press Shift-Command-U, or select "Revert" from the File menu.
If you have a better way of handling this, I'd love to hear it. Sound off in the comments.
Comments: (0)
alphatrak
Bringing you the coding smackdown since '95
Bringing you the coding smackdown since '95
Now Playing
- StarCraft - PC
Now Reading
- Death Note
- Ikigami: The Ultimate Limit
- Infinite Game Universe: Mathematical Techniques
- Microserfs by Douglas Coupland
- Pro Android 2