<转>Sublime2 gbk编码乱码与gbk中文文件名乱码解决方案

简介: 让GBK Encoding Support内容支持中文,标签栏显示也为中文. 安装GBK Encoding Support ,然后修改SublimeText2\Data\Packages\GBK Encoding Support\sublime_gbk.
+关注继续查看

让GBK Encoding Support内容支持中文,标签栏显示也为中文.

安装GBK Encoding Support ,然后修改SublimeText2\Data\Packages\GBK Encoding Support\sublime_gbk.py 

内容如下:

 

#coding: utf8

import sublime, sublime_plugin
import os, re
import urllib

TEMP_PATH = os.path.join(os.getcwd(), 'tmp')
SEPERATOR = '                '

def gbk2utf8(view):
    try:
        reg_all = sublime.Region(0, view.size())
        gbk = view.substr(reg_all).encode('gbk')
    except:
        gbk = file(view.file_name()).read()
        text = gbk.decode('gbk')
        
        file_name = view.file_name().encode('utf-8')

        tmp_file_name = urllib.quote_plus(os.path.basename(file_name))  + SEPERATOR + urllib.quote_plus(file_name)
        tmp_file = os.path.join(TEMP_PATH, tmp_file_name)

        # f = file(tmp_file, 'w')
        f = file(view.file_name(), 'w')
        f.write(text.encode('utf8'))
        f.close()

        window = sublime.active_window()
        
        # v = window.find_open_file(tmp_file)
        v = window.find_open_file(file_name)

        if(not v):
            window.open_file(file_name)
            # window.open_file(tmp_file)

        window.focus_view(view)
        window.run_command('close')
        window.focus_view(v)

        sublime.status_message('gbk encoding detected, open with utf8.')

def saveWithEncoding(view, file_name = None, encoding = 'gbk'):
    if(not file_name):
        file_name = view.file_name()
    reg_all = sublime.Region(0, view.size())
    text = view.substr(reg_all).encode(encoding)
    gbk = file(file_name, 'w')
    gbk.write(text)
    gbk.close()    

class EventListener(sublime_plugin.EventListener):
    def on_load(self, view):
        gbk2utf8(view)
    def on_post_save(self, view):
        parts = view.file_name().split(SEPERATOR)
        if(view.file_name().startswith(TEMP_PATH) and len(parts) > 1):
            file_name = urllib.unquote_plus(parts[1].encode('utf-8')).decode('utf-8')
            saveWithEncoding(view, file_name)

class SaveWithGbkCommand(sublime_plugin.TextCommand):
    def __init__(self, view):
        self.view = view
    def run(self, edit):
        file_name = self.view.file_name()

        if(not file_name):
            return

        parts = file_name.split(SEPERATOR)
        if(not file_name.startswith(TEMP_PATH) and len(parts) <= 1):
            saveWithEncoding(self.view)
            sublime.active_window().run_command('close')
            sublime.active_window().open_file(self.view.file_name())
        else:
            sublime.active_window().run_command('save')

class SaveWithUtf8Command(sublime_plugin.TextCommand):
    def __init__(self, view):
        self.view = view
    def run(self, edit):
        file_name = self.view.file_name()

        if(not file_name):
            return

        parts = file_name.split(SEPERATOR)

        if(file_name.startswith(TEMP_PATH) and len(parts) > 1):
            file_name = urllib.unquote_plus(parts[1].encode('utf-8')).decode('utf-8')
            saveWithEncoding(self.view, file_name, 'utf-8')
            sublime.active_window().run_command('close')
            sublime.active_window().open_file(file_name)
        else:
            sublime.active_window().run_command('save')

 

 

目录
相关文章
|
5天前
|
存储 网络协议 安全
字符编码技术专题(一):快速理解ASCII、Unicode、GBK和UTF-8
今天中午,我突然想搞清楚 Unicode 和 UTF-8 之间的关系,就开始查资料。 这个问题比我想象的复杂,午饭后一直看到晚上9点,才算初步搞清楚。 下面就是我的总结,主要用来整理自己的思路。我尽量写得通俗易懂,希望能对其他朋友有用。毕竟,字符编码是计算机技术的*石,对于程序员来说尤其重要,字符编码的知识是必须要懂的。
5 0
|
3月前
|
存储 Windows
“浅入深处“编码历史,字符串编码(ASCII, GBK, ANSI, Unicode, UTF-8编码),为什么记事本默认ANSI编码,Unicode和UTF8有什么区别
“浅入深处“编码历史,字符串编码(ASCII, GBK, ANSI, Unicode, UTF-8编码),为什么记事本默认ANSI编码,Unicode和UTF8有什么区别
|
3月前
|
存储 Java 关系型数据库
【精炼易懂】字符集、编码、乱码问题、ASCII、GBK、Unicode、UTF-8详解+实例说明
【精炼易懂】字符集、编码、乱码问题、ASCII、GBK、Unicode、UTF-8详解+实例说明
4335 4
|
3月前
|
存储 Windows
编码 GBK 的不可映射字符 (0x80),sublime设置中文,sublime没有GBK编码选项的问题详解(二)
编码 GBK 的不可映射字符 (0x80),sublime设置中文,sublime没有GBK编码选项的问题详解
64 1
|
3月前
|
Java Linux 开发工具
编码 GBK 的不可映射字符 (0x80),sublime设置中文,sublime没有GBK编码选项的问题详解(一)
编码 GBK 的不可映射字符 (0x80),sublime设置中文,sublime没有GBK编码选项的问题详解
64 1
|
5月前
|
存储 编解码 前端开发
正确的认识乱码与编码
编码将《内存字节》作用于《磁盘文件或者网络文件》的过程,也是将《磁盘文件/网络文件》反解析成《内存字节》的过程. 这个过程中如果 内存字符串到 “字节数组”的编码 与 网络/磁盘文件的之间转化的编解码方式不一致或者不兼容就 会产生乱码. 在一次网络数据访问的过程中,可能有多次这两个步骤的转化.
87 0
|
编解码 JavaScript IDE
彻底解决Qt中文乱码以及汉字编码的问题(UTF-8/GBK)(二)
彻底解决Qt中文乱码以及汉字编码的问题(UTF-8/GBK)
1750 0
彻底解决Qt中文乱码以及汉字编码的问题(UTF-8/GBK)(二)
|
编解码 JavaScript Ubuntu
彻底解决Qt中文乱码以及汉字编码的问题(UTF-8/GBK)(一)
彻底解决Qt中文乱码以及汉字编码的问题(UTF-8/GBK)
2146 0
|
存储 安全 关系型数据库
为什么编码不同会出现乱码?
本章节通过情景模拟,和大家一起探讨为什么编码不同会出现乱码
|
Web App开发 JavaScript Java
编码问题 UTF-8 & GBK
记一次奇怪的编码问题。 * Meta Element vs Response Header * Meta 的作用? * 一个细节 * 细节对编码的影响 ## Meta Element vs Response Header 一个 GBK 编码页面,使用 meta 指定页面编码和使用 response header 指定页面编码。哪个优先级比较高? ###
2279 0
相关产品
云迁移中心
推荐文章
更多