Wednesday, February 8, 2012

Fixing Sublime Text 2 for a Japanese Windows system

Sublime Text 2 is a programmer’s text editor with build-in Python scripting. Usually I use Programmer’s Notepad as a lightweight text editor, but I decided to give Sublime a try.

Unfortunately, it didn’t run my Python script when pressing build. Outputting the following line instead:

[Decode error - output not utf-8][Finished]

A quick look at the source revealed that they expect the command prompt output to be utf-8:

try:
str = data.decode(self.encoding)
except:
str = "[Decode error - output not " + self.encoding + "]"
proc = None

While this may be true on linux, it isn’t most of the time the case on Windows. To see the encoding of the command prompt, the command chcp can be used. On a Japanese Windows 7 system this is code page 932, a Shift-JIS dialect made by Microsoft:

C:\>chcp
現在のコード ページ: 932

Knowing this, it is easy to fix the Sublime build command. Just open Python.sublime-build for example and add the encoding:

{
"cmd": ["D:\\Python26\\python", "-u", "$file"],
"file_regex": "^[ ]*File \"(...*?)\", line ([0-9]*)",
"selector": "source.python",
"encoding": "cp932"
}

Once saved, the build command will work perfectly. More information on build command files for Sublime can be found here.

1 comment:

  1. Thanks! It worked not only for Japanese writing system, but also for other non-ASCII characters, like cyrillic.

    ReplyDelete