Skip to content

Instantly share code, notes, and snippets.

@eagleon
Created January 20, 2013 17:17
Show Gist options
  • Select an option

  • Save eagleon/4579969 to your computer and use it in GitHub Desktop.

Select an option

Save eagleon/4579969 to your computer and use it in GitHub Desktop.
我安装了GBK Encoding Support这款插件可以解决打开GBK编码的文件中文乱码的问题,但是随之而来的确是tab显示的标题乱码了,不怎么好看.于是自己动手丰衣足食,我就修改了一下原作者的代码,修改代码文件的路径如下:C:\Users\你的用户名\AppData\Roaming\Sublime Text 2\Packages\GBK Encoding Support\sublime_gbk.py,达到我的目的,当然这样我就把原来文件的gbk给转换成了utf-8了。大家看代码吧,注释部分原作者的代码,newline注释下面的是我修改的代码,替换gbk2utf8即可。
#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')
#newline
f = file(view.file_name(),'w')
f.write(text.encode('utf8'))
f.close()
window = sublime.active_window()
#newline
v = window.find_open_file(file_name)
#v = window.find_open_file(tmp_file)
if(not v):
#newline
#window.open_file(tmp_file)
window.open_file(file_name)
window.focus_view(view)
window.run_command('close')
window.focus_view(v)
sublime.status_message('gbk encoding detected, open with utf8.')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment