代码: 全选
function GetEncoding(f)
let e = system('enca -Pe "' . a:f . '"')
let e = substitute(e, '/.*', '', '')
if e =~ 'unknown'
return 'ascii'
endif
return e
endfunc
au BufReadPre * exec "set fencs=" . GetEncoding(expand('<afile>'))
代码: 全选
function GetEncoding(f)
let e = system('enca -Pe "' . a:f . '"')
let e = substitute(e, '/.*', '', '')
if e =~ 'unknown'
return 'ascii'
endif
return e
endfunc
au BufReadPre * exec "set fencs=" . GetEncoding(expand('<afile>'))
用enca的好处就是日文韩文阿拉伯文什么的都没有问题,只要字体是全的。madoldman 写了:直接在fencs里面把常用的编码都放进去,用逗号间隔就可以自动识别了吧?我一直这样用,好像没问题,如下
set enc=utf-8
set fenc=utf-8
set fencs=utf-8,gb18030,gbk,gb2312,cp936
终极做不到, 发现如果是日文的 sjis 会被错误判断为 gbk。。 亚洲编码就是复杂阿。roylez 写了:这也许是终级解决方案了,不光简体繁体,其他任何编码应该都没问题。本来就知道enca可以判断文件编码,突然心血来潮想要让它帮vim选择编码。为了避免自己浪费力气,先google了一下,已经有人做好了。http://trific.ath.cx/software/enca/misc/代码: 全选
function GetEncoding(f) let e = system('enca -Pe "' . a:f . '"') let e = substitute(e, '/.*', '', '') if e =~ 'unknown' return 'ascii' endif return e endfunc au BufReadPre * exec "set fencs=" . GetEncoding(expand('<afile>'))