linux中文乱码解决之道

当使用linux时 打开gbk编码的文件时通常是乱码 因为linux默认使用的是utf-8

那么如果让编辑器支持gbk编码了 那就是今天要讨论的事情了

思路:

首先用locale命令来查看当前系统locale配置环境

locale显示默认字符集 locale -a显示支持的字符集

修改local文件 用local-gen生成需要要的local文件

通过修改的local文件重置配置

demo:

查看当前拥有的中文字符集

nailsoul@nailsoul-Inspiron-3847:~$ locale -a|grep zh_CN
zh_CN
zh_CN.gb18030
zh_CN.gb2312
zh_CN.gbk
zh_CN.utf8

修改locale文件(/var/lib/locales/supported.d/local)

由于使用的是english版 var/lib/locales/supported.d 下面只有en文件没有local文件

推荐的方法是拷贝所有ubuntu支持的字符集到自己的配置文件里,然后编辑。

cp /usr/share/i18n/SUPPORTED /var/lib/locales/supported.d/local

vim /var/lib/locales/supported.d/local

删除不需要的字符集 留下需要的字符集 也可以留下en_和zh_系列的字符集

删除除了local以外的字符集文件 如果其他字符集文件中有需要的字符 拷贝到local文件中

rm /var/lib/locales/supported.d/en

重新生成locale支持文件

nailsoul@nailsoul-Inspiron-3847:/var/lib/locales/supported.d$ sudo locale-gen --purge
Generating locales (this might take a while)...
  en_AG.UTF-8... done
  en_AU.ISO-8859-1... done
  en_AU.UTF-8... done
  en_BW.ISO-8859-1... done
  en_BW.UTF-8... done
  en_CA.ISO-8859-1... done
  en_CA.UTF-8... done
  en_DK.ISO-8859-1... done
  en_DK.ISO-8859-15... done
  en_DK.UTF-8... done
  en_GB.ISO-8859-1... done
  en_GB.ISO-8859-15... done
  en_GB.UTF-8... done
  en_HK.ISO-8859-1... done
  en_HK.UTF-8... done
  en_IE.ISO-8859-1... done
  en_IE.UTF-8... done
  en_IE.ISO-8859-15@euro... done
  en_IN.UTF-8... done
  en_NG.UTF-8... done
  en_NZ.ISO-8859-1... done
  en_NZ.UTF-8... done
  en_PH.ISO-8859-1... done
  en_PH.UTF-8... done
  en_SG.ISO-8859-1... done
  en_SG.UTF-8... done
  en_US.ISO-8859-1... done
  en_US.ISO-8859-15... done
  en_US.UTF-8... done
  en_ZA.ISO-8859-1... done
  en_ZA.UTF-8... done
  en_ZM.UTF-8... done
  en_ZW.ISO-8859-1... done
  en_ZW.UTF-8... done
  zh_CN.GB2312... done
  zh_CN.GB18030... done
  zh_CN.GBK... done
  zh_CN.UTF-8... done
  zh_HK.BIG5-HKSCS... done
  zh_HK.UTF-8... done
  zh_SG.GB2312... done
  zh_SG.GBK... done
  zh_SG.UTF-8... done
  zh_TW.BIG5... done
  zh_TW.EUC-TW... done
  zh_TW.UTF-8... done
Generation complete.


这一步会将/usr/lib/locale/里面的locale支持文件删掉,重新生成。如果设置的locale没有生成过,或者设置的时候拼写错误,在这个目录找不到同名的支持文件就会提示No such file了。

重置配置

nailsoul@nailsoul-Inspiron-3847:/var/lib/locales/supported.d$ sudo dpkg-reconfigure locales 
Generating locales (this might take a while)...
  en_AG.UTF-8... done
  en_AU.ISO-8859-1... done
  en_AU.UTF-8... done
  en_BW.ISO-8859-1... done
  en_BW.UTF-8... done
  en_CA.ISO-8859-1... done
  en_CA.UTF-8... done
  en_DK.ISO-8859-1... done
  en_DK.ISO-8859-15... done
  en_DK.UTF-8... done
  en_GB.ISO-8859-1... done
  en_GB.ISO-8859-15... done
  en_GB.UTF-8... done
  en_HK.ISO-8859-1... done
  en_HK.UTF-8... done
  en_IE.ISO-8859-1... done
  en_IE.UTF-8... done
  en_IE.ISO-8859-15@euro... done
  en_IN.UTF-8... done
  en_NG.UTF-8... done
  en_NZ.ISO-8859-1... done
  en_NZ.UTF-8... done
  en_PH.ISO-8859-1... done
  en_PH.UTF-8... done
  en_SG.ISO-8859-1... done
  en_SG.UTF-8... done
  en_US.ISO-8859-1... done
  en_US.ISO-8859-15... done
  en_US.UTF-8... done
  en_ZA.ISO-8859-1... done
  en_ZA.UTF-8... done
  en_ZM.UTF-8... done
  en_ZW.ISO-8859-1... done
  en_ZW.UTF-8... done
  zh_CN.GB2312... done
  zh_CN.GB18030... done
  zh_CN.GBK... done
  zh_CN.UTF-8... done
  zh_HK.BIG5-HKSCS... done
  zh_HK.UTF-8... done
  zh_SG.GB2312... done
  zh_SG.GBK... done
  zh_SG.UTF-8... done
  zh_TW.BIG5... done
  zh_TW.EUC-TW... done
  zh_TW.UTF-8... done
Generation complete.



设置 Vim RunTime Environment

$ vim ~/.vimrc //如果不存在新建

将以下文件保存到该文件中
set fileencodings=utf-8,ucs-bom,gb18030,gbk,gb2312,cp936  
set termencoding=utf-8  
set encoding=utf-8  
wq退出保存

当下次打开gbk的文件是就不会是乱码了

Ubuntu终端乱码问题解决方案

linux下vim中文乱码的解决方法


通过gconf-editor解决gedit乱码问题

如何解决gedit乱码问题

猜你喜欢

转载自blog.csdn.net/nailsoul/article/details/52124067