Home Download Buy Blog Forum Support

ConvertToUTF8 plugin

ConvertToUTF8 plugin

Postby wofeiwo on Tue Sep 06, 2011 7:09 pm


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: Select all
# 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:
                    theFile = codecs.open(view.file_name(),"r",coder)
                    text = theFile.read()
                    if len(text) != 0:
        # Replace all file content with new encoding data.
        view.replace(edit, view.sel()[0], text)

        # Reset cursor position.

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

And here is User key binding:
Code: Select all
    { "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.
Posts: 2
Joined: Tue Sep 06, 2011 6:17 pm

Re: ConvertToUTF8 plugin

Postby yuzebin on Mon Jan 30, 2012 7:41 am

Thanks very much.
Posts: 2
Joined: Mon Jan 30, 2012 7:33 am

Re: ConvertToUTF8 plugin

Postby lucifr on Tue Feb 21, 2012 12:19 pm

Have you considered wrap it up as a package and commit to github?
Posts: 2
Joined: Sun Dec 18, 2011 6:15 am

Re: ConvertToUTF8 plugin

Postby wofeiwo on Thu Feb 23, 2012 3:58 am

Just try this plugin:EncodingHelper
My plugin will not be updated again. Thanks for your guys' support.
Posts: 2
Joined: Tue Sep 06, 2011 6:17 pm

Re: ConvertToUTF8 plugin

Postby iKythm on Thu Mar 01, 2012 11:06 pm

It's working...
Good job,Thank you
Posts: 1
Joined: Thu Mar 01, 2012 11:03 pm

Re: ConvertToUTF8 plugin

Postby Booger12 on Fri Apr 13, 2012 9:10 am

It probably will not work on a vb4.

Online Pharmacy
Posts: 2
Joined: Fri Apr 13, 2012 9:06 am

Return to Plugin Announcements

Who is online

Users browsing this forum: Yahoo [Bot] and 14 guests