Sublime Forum

ConvertToUTF8 plugin

#1

Hi,all

I’m from china, and opening gbk encoding file always be a problem in sublime 2.
So I write this plugin, Convert gbk file to utf8.

Save following code in ~/Library/Application Support/Sublime Text 2/Packages/User/ConvertToUTF8.py

[code]# Created by wofeiwo <wofeiwo#gmail.com> on 2011-09-07.
import sublime, sublime_plugin
import codecs

Add “convert_coder_list” list in user settings,

Or you can just modify your own charsets here.

DEFAULT_CODER = “gb18030”, “gbk”, “gb2312”, “big5”, “utf-8”, “utf-16le”]

class ConvertToUtf8Command(sublime_plugin.TextCommand):
def run(self, edit):
view = self.view

    if view.is_loading():
        sublime.status_message("Waiting to loading.")
        return False
    
    coders = view.settings().get("convert_coder_list", DEFAULT_CODER)

    text = ""

    # Try to convert every coder in the list.
    for coder in coders:
            try:
                theFile = codecs.open(view.file_name(),"r",coder)
                text = theFile.read()
                if len(text) != 0:
                    break
            except:
                continue        
    
    # Replace all file content with new encoding data.
    view.run_command("select_all")
    view.replace(edit, view.sel()[0], text)

    # Reset cursor position.
    view.sel().clear()
    view.sel().add(sublime.Region(0))

def is_enabled(self):
    return self.view.file_name() and len(self.view.file_name()) > 0

[/code]

And here is User key binding:

    { "keys": "ctrl+shift+c"], "command": "convert_to_utf8" }  

Then you can open the gbk file, press “ctrl+shift+c”,all content will replace by new encoding content.

0 Likes

How to set fallback_encoding to gb2312?
#2

Thanks very much.

0 Likes

#3

Have you considered wrap it up as a package and commit to github?

0 Likes

#4

Just try this plugin:EncodingHelper
My plugin will not be updated again. Thanks for your guys’ support.

0 Likes

#5

It’s working…
Good job,Thank you

0 Likes

#6

It probably will not work on a vb4.


Online Pharmacy

0 Likes

#7

extremely useful, thx a lot

0 Likes