个人遇到的Vim问题汇总:中文、乱码、字体、打印、ghostscript、xelatex

Vim、Emacs配置和使用
回复
neil.sun
帖子: 110
注册时间: 2009-08-28 15:00

个人遇到的Vim问题汇总:中文、乱码、字体、打印、ghostscript、xelatex

#1

帖子 neil.sun » 2012-06-29 14:36

先把以前发过的VIM贴子也一起放进来:

Vim常用命令速记表
这个贴子里的最后一条命令最有用,说明我记性越来越差了。

Ubuntu 10.04 Lucid Lynx中GVim无法正常显示中文菜单
这个贴子里的问题出现的频率小了,解决方案仍旧是有用的。我检查了最新的版本Vim7.3,貌似从7.2升上来之后就已经没这问题了。

如何在Ubuntu 10.04 Lucid Lynx下用Vim打印中文UTF-8文档
这个贴子里Vim的部分仍旧是对的,但是Ghostscript升级了,从8升到9之后CIDFnmap的位置变了。9里面怎么配置,我会在后面提到。
参见:4、linux下ghostscript 9字体配置

以下所有问题适用于Vim7.3,操作系统Archlinux和Windows7。
本文不包括如何在linux和windows中安装字体。
但包括如何在linux下的ghostscript 9中配置字体。
参见:4、linux下ghostscript 9字体配置

话说管理员能不能开发个功能,贴代码的时候自动加行号啊~~
这里贴出的代码都是有行号的,方便后面解释。
要复制粘贴的同学,文后有完整的.vimrc文件。
本文所列出的字体文件,在文后有下载链接。

1、中文显示乱码

代码: 全选

" encoding
1 set encoding=utf-8
2 set fileencoding=utf-8
3 if has('win32')
4     set termencoding=chinese
5     language message zh_CN.UTF-8
6 endif
解释下:
1 显示时采用utf-8
2 文件保存时采用utf-8
4 在windows命令行中调用vim命令时,中文文件内容可以正常显示
5 在windows中vim/gvim的状态栏中,中文提示可以正常显示

2、设置中文字体

代码: 全选

" font
1 if has('unix')
2     set guifont=Microsoft\ Yahei\ Mono\ 12
3 elseif has('win32')
4     set guifont=Microsoft_Yahei_Mono:h12:cGB2312
5 endif
解释下:
2 要先在linux中安装相应的字体
4 要先在windows中安装相应的字体
文后有字体文件的下载链接

windows版的vim无法列出所有的字体,只有很有限的几种,重新编译vim是个解决方案,安装合适的字体也可以解决这个问题。
这里的Microsoft_YaHei_Mono就是一个微软雅黑Mono的特殊字体,安装后可以被windows版的vim正确选择和使用。

3、中文打印乱码

代码: 全选

" print
1 if has('unix')
2     set printencoding=utf-8
3     set printmbcharset=iso10646
4     set printfont=Microsoft_YaHei_Mono
5     set printmbfont=r:Microsoft_YaHei_Mono
6 elseif has('win32')
7     " very dirty, but works
8     map <Leader>p :let @m=&mod<CR>ggvG$"+yggvG$x:set enc=cp936<CR>:lang mes zh_CN<CR>"+P:ha<CR>ggvG$"+yggvG$x:set enc=utf-8<CR>:lang mes zh_CN.UTF-8<CR>"+P:if !@m<CR>w<CR>en<CR><CR>
9 endif
linux中vim打印乱码:很多时候是ghostscript没有配置好字体。

2 打印时采用utf-8
3 打印时采用中文字符集
4 设置打印时使用的ghostscript字体
5 设置打印多字节字符时使用的ghostscript字体
4 5 都要先在linux中安装相应的字体,并在ghostscript中配置好,如何配置见后文
参见:4、linux下ghostscript 9字体配置

windows中vim打印乱码:很多时候是vim和windows采用不同的编码和语言造成的。
windows中的编码是cp936,语言是zh_CN。编码可以改,但会带来很多问题。语言很难改。
vim中配置的编码是utf-8,语言是zh_CN.UTF-8。编码和语言都能改,但是文件格式转来转去也很容易出问题。

于是,我们采用了肮脏的手段,dirty work。第8行拆解如下:

代码: 全选

map <Leader>p
使用 <Leader>p来调用打印命令
不知道什么是Leader的同学,请用\p来调用命令,并参考:help Leader
知道什么是Leader的同学,请在.vimrc里面查找你所设置的mapleader

代码: 全选

:let @m=&mod<CR>
用寄存器m来记录当前文件是否已保存,mod是modified,参考:help modified

代码: 全选

ggvG$"+y
ggvG$全选所有文件内容
"+y复制到windows剪贴板
"+是全局剪贴板,参考:help "+

代码: 全选

ggvG$x
ggvG$全选所有文件内容
x删除

代码: 全选

:set enc=cp936<CR>
把编码设为cp936,enc是encoding,<CR>是回车
cp936全称是:Chinese (Simplified)_People's Republic of China.936,语言首字母C,国家首字母P,编号936
这是为了正确打印文件。

代码: 全选

:lang mes zh_CN<CR>
把语言设置为zh_CN,lang mes是language messages,参考:help language
这是为了正确显示vim的打印对话框和打印进度提示。

代码: 全选

"+P
从windows剪贴板里把内容复制回来,这时候会自动转码为cp936

代码: 全选

:ha<CR>
打印,ha是hardcopy,参考:help hardcopy

代码: 全选

ggvG$"+y
ggvG$全选所有文件内容
"+y复制到windows剪贴板。

代码: 全选

ggvG$x
ggvG$全选所有文件内容
x删除

代码: 全选

:set enc=utf-8<CR>
恢复utf-8编码设置

代码: 全选

:lang mes zh_CN.UTF-8<CR>
恢复zh_CN.UTF-8语言设置

代码: 全选

"+P
从windows剪贴板里把内容复制回来,这时候会自动从cp936转码为utf-8

代码: 全选

:if !@m<CR>
w<CR>
en<CR>
<CR>
完整版是:

代码: 全选

:if !@m<CR>
write<CR>
endif<CR>
<CR>
这么折腾了一番之后,现在的文件状态是:已编辑,未保存。
如果打印之前已经保存,那么现在的状态和打印前是不一致的,需要再保存下。
如果打印之前尚未保存,那么现在的状态和打印前是一致的,就不需要保存了。
打印之前文件是什么状态,我们之前已经记录在寄存器m中了,m是modified的首字母,方便记忆。

正如注释所说,第8行代码很dirty,但是能work。

缺点:使用了windows全局剪贴板和vim寄存器m。
所以:不要在windows全局剪贴板中放重要的东西。

打印完之后如果想清除windows全局剪贴板和寄存器m中的内容:

代码: 全选

:let @+=''
:let @m=''
4、linux下ghostscript 9字体配置

以Archlinux为例,字体配置文件的位置在:

代码: 全选

/usr/share/ghostscript/9.05/Resource/Init/cidfmap
字体配置范例:

代码: 全选

/SimSun   << /FileType /TrueType /Path (/usr/share/fonts/TTF/simsun.ttc)  /SubfontID 0 /CSI [(GB1) 2] >> ;
/NSimSun  << /FileType /TrueType /Path (/usr/share/fonts/TTF/simsun.ttc)  /SubfontID 1 /CSI [(GB1) 2] >> ;
/SimHei   << /FileType /TrueType /Path (/usr/share/fonts/TTF/simhei.ttf)  /SubfontID 0 /CSI [(GB1) 2] >> ;
/KaiTi    << /FileType /TrueType /Path (/usr/share/fonts/TTF/simkai.ttf)  /SubfontID 0 /CSI [(GB1) 2] >> ;
/FangSong << /FileType /TrueType /Path (/usr/share/fonts/TTF/simfang.ttf) /SubfontID 0 /CSI [(GB1) 2] >> ;
/LiSu     << /FileType /TrueType /Path (/usr/share/fonts/TTF/simli.ttf)   /SubfontID 0 /CSI [(GB1) 2] >> ;
/YouYuan  << /FileType /TrueType /Path (/usr/share/fonts/TTF/simyou.ttf)  /SubfontID 0 /CSI [(GB1) 2] >> ;
/Microsoft_YaHei      << /FileType /TrueType /Path (/usr/share/fonts/TTF/msyh.ttf)     /SubfontID 0 /CSI [(GB1) 2] >> ;
/Microsoft_YaHei_Bold << /FileType /TrueType /Path (/usr/share/fonts/TTF/msyhbd.ttf)   /SubfontID 0 /CSI [(GB1) 2] >> ;
/Microsoft_YaHei_Mono << /FileType /TrueType /Path (/usr/share/fonts/TTF/msyhmono.ttf) /SubfontID 0 /CSI [(GB1) 2] >> ;
解释下:

代码: 全选

/SimSun   << /FileType /TrueType /Path (/usr/share/fonts/TTF/simsun.ttc)  /SubfontID 0 /CSI [(GB1) 2] >> ;
拆解如下:

代码: 全选

/SimSun
这个字体名叫SimSun

代码: 全选

/FileType /TrueType
它是一个TureType字体文件

代码: 全选

/Path (/usr/share/fonts/TTF/simsun.ttc)
文件路径在/usr/share/fonts/TTF/simsun.ttc

代码: 全选

/SubfontID 0
它是这个文件中的第一个字体
通常的文件名是*.ttf,意思是truetype file,这种文件一般只含一种字体,可以放心使用序号0
有时候文件名是*.ttc,意思是truetype collection,这种文件一般含几个字体,要注意用/SubfontID后面的序号来选择你想要的那个字体。

代码: 全选

/CSI [(GB1) 2]
/CSI是CID System Info
GB1是指字体编码,这里是中文
如果数值为2,表示包含Ordering和Supplement两种信息。
如果数值为3,表示包含Registry,Ordering和Supplement三种信息。
对于TrueType字体来说,目前只能取2。

更多信息,请参考:CIDFontSubstitution

5、如何在vim-latex插件中使用xelatex

代码: 全选

 1 " for xelatex in vim-latex, clean code, but can not compile twice
 2 "let g:Tex_DefaultTargetFormat='pdf'
 3 "let g:Tex_CompileRule_pdf='xelatex -interaction=nonstopmode $*'

 4 " for xelatex in vim-latex, dirty work, but able to compile twice
 5 let g:Tex_CompileRule_dvi='xelatex -interaction=nonstopmode $*'
 6 if has('unix')
 7     let g:Tex_ViewRuleComplete_dvi = 'evince $*.pdf &'
 8 elseif has('win32')
 9     let g:Tex_ViewRuleComplete_dvi = 'start AcroRd32 $*.pdf'
10 endif
第一种:干净的解法
2 设置为默认生成pdf文件
3 用xelatex来生成pdf文件,其中的$*会被展开为当前文件的文件名

这是干净明白的解法。但是,vim-latex有一个我很喜欢的功能,就是:
默认情况下,vim-latex会用latex生成dvi,此时它会一边跑一边监控.aux文件。
如果发现.aux文件改变了,vim-latex会自动跑第二遍,来生成目录和索引等内容。
如果用干净的解法,vim-latex就不能自动跑两遍,必须由我手动跑两遍。我很懒。

于是再次采用了肮脏的手段。

第二种:肮脏但有效的解法
5 保持默认生成dvi文件的设置,但却运行xelatex命令。
这样子当然不会生成dvi了,最后生成的一定是pdf,但是却可以自动跑两遍。
但是预览的时候就傻了,默认的dvi预览器是:unix下xdvi,windows下yap。这两位都不能看pdf,况且文件后缀名也不对。
7 设置unix下的dvi预览器为evince,后台预览pdf文件
9 设置windows下的dvi预览器为acrobat,后台预览pdf文件
7 9中的$*会被展开为当前文件的文件名,不含后缀名。

参考:

代码: 全选

:help Tex_DefaultTargetFormat
:help Tex_CompileRule_format
:help Tex_ViewRule_format
:help Tex_ViewRuleComplete_format
附上完整的.vimrc

代码: 全选

" basic
set nocompatible
set backspace=indent,eol,start
if has('unix')
    set viminfo+=n$HOME/.vim/.viminfo
elseif has('win32')
    set viminfo+=n$HOME/vimfiles/_viminfo
endif

" appearance
set ruler
set nowrap
set number
set showcmd

" search
set hlsearch
set incsearch

" tab space indent
set smarttab
set expandtab
set autoindent
set smartindent

" tabstop
set tabstop=4
set shiftwidth=4
set softtabstop=4

" miscellaneous
set mouse=a
set history=50
let mapleader=','
colorscheme torte

" encoding
set encoding=utf-8
set fileencoding=utf-8
if has('win32')
    set termencoding=chinese
    language message zh_CN.UTF-8
endif

" font
if has('unix')
    set guifont=Microsoft\ Yahei\ Mono\ 12
elseif has('win32')
    set guifont=Microsoft_Yahei_Mono:h12:cGB2312
endif

" print
if has('unix')
    set printencoding=utf-8
    set printmbcharset=iso10646
    set printfont=Microsoft_YaHei_Mono
    set printmbfont=r:Microsoft_YaHei_Mono
elseif has('win32')
    " very dirty, but works
    map <Leader>p :let @m=&mod<CR>ggvG$"+yggvG$x:set enc=cp936<CR>:lang mes zh_CN<CR>"+P:ha<CR>ggvG$"+yggvG$x:set enc=utf-8<CR>:lang mes zh_CN.UTF-8<CR>"+P:if !@m<CR>w<CR>en<CR><CR>
endif

" syntax
syntax on
filetype plugin on
filetype indent on

" dictionary
if has('unix')
    set dict=/usr/share/dict/words
endif

" highlight insert mode
autocmd InsertEnter,InsertLeave * set cursorline!

" for mutt
autocmd BufRead /tmp/mutt-* set spell textwidth=72

" for vim-latex
set grepprg=grep\ -nH\ $*
autocmd BufRead *.tex set shiftwidth=2 iskeyword+=:

    " for xelatex in vim-latex, clean code, but can not compile twice
    "let g:Tex_DefaultTargetFormat='pdf'
    "let g:Tex_CompileRule_pdf='xelatex -interaction=nonstopmode $*'

    " for xelatex in vim-latex, dirty work, but able to compile twice
    let g:Tex_CompileRule_dvi='xelatex -interaction=nonstopmode $*'
    if has('unix')
        let g:Tex_ViewRuleComplete_dvi = 'evince $*.pdf &'
    elseif has('win32')
        let g:Tex_ViewRuleComplete_dvi = 'start AcroRd32 $*.pdf'
    endif

" for vimim
:let g:vimim_cloud=-1
PS:忽然想到ggvG$貌似可以简化为ggVG

字体文件下载链接:
https://bit.ly/neilpub -> fonts

------------------------------------------------

4月11日更新:更新了字体和下载链接
上次由 neil.sun 在 2013-04-11 3:08,总共编辑 5 次。
头像
reverland
帖子: 1317
注册时间: 2011-11-26 15:57
系统: windows xp
联系:

Re: 个人遇到的Vim问题汇总:中文、乱码、字体、打印、ghostscript、xelatex

#2

帖子 reverland » 2012-07-04 11:34

:em11
托在github上的jekyll博客
Always Look on the Bright Side of Life
头像
code vampire
帖子: 193
注册时间: 2010-06-19 12:50
系统: AIX、UBUNTU
联系:

Re: 个人遇到的Vim问题汇总:中文、乱码、字体、打印、ghostscript、xelatex

#3

帖子 code vampire » 2012-09-09 11:54

ghostscript9 配置的真及时
回复