Handling Japanese Encodings in TextMate

As we know, TextMate is fairly understandably biased to UTF. I often encounter garbage text when opening files someone else made. Most of them are encoded in Shift-JIS.

After playing with Terminal, I could use the following command to convert the file to be encoded in UTF-8.

iconv -f shift-jis -t utf-8 text1.txt>text2.txt

I tried to convert the command above to a command in TextMate. I made a new command called “Encode Shift-JIS to UTF-8”, and I typed in the following in the text field:

iconv -f shift-jis -t utf-8 "$TM_FILEPATH"

Also other settings are the following:

Save: Nothing
Input: None
Output: Replace Document

However, the command didn’t work appropriately. I saw a diamond-like character at the end of each line. (Update: The diamond characters can be removed with “Remove Unprintable Characters in Document / Selection” in Text Bundle.) So, I went and asked why this doesn’t work in TextMate mailing list. According to Hans, this problem is due to the window/buffer using UTF-8. He suggested an alternative way to this. You type the following command in a TextMate document window.

iconv -f shift-jis -t utf-8 /PATH/TO/file.txt | cat

Then, you type Control-R, which is “Execute Line and Insert the Result”. He also shared his own bundle to open encoded file, “OpenEncodedFile”, which is available from his website. Another bundle he shared in the list is a bundle to facilitate inserting file path names, called FileName Completion.tmCommand.

I think his way is better than use of nkf, which you need to install. iconv and textutil come with every Mac.

Note: I will put a link to the mailing list archive as soon as the thread is available in the archive.