vim中解决中文乱码完美解决方案

  • 六月 1st, 2007

很多朋友都遇到vim中中文乱码这个问题,修改vimrc文件时,菜单栏乱码,弹出菜单栏乱码,显示主题乱码等等,都没有得到完美的解决。我遇到也是如此,但下面这4行代码在我的电脑上完美无瑕的解决了这个问题:

“此四行解决所有乱码问题
set langmenu=zh_CN.utf8
set fileencodings=utf-8,cp936,big5,latin1
set ambiwidth=double
let $LANG=’en’

new:关于此问题最新终极解决方案请查阅

http://www.liuhuadong.com/archives/68/index.html 

我的vimrc:

set nocompatible

source $VIMRUNTIME/plugin/delspc.vim “删除文件行尾空格

source $VIMRUNTIME/mswin.vim
behave mswin

set diffexpr=MyDiff()
function MyDiff()
let opt = ‘-a –binary ‘
if &diffopt =~ ‘icase’ | let opt = opt . ‘-i ‘ | endif
if &diffopt =~ ‘iwhite’ | let opt = opt . ‘-b ‘ | endif
let arg1 = v:fname_in
if arg1 =~ ‘ ‘ | let arg1 = ‘”‘ . arg1 . ‘”‘ | endif
let arg2 = v:fname_new
if arg2 =~ ‘ ‘ | let arg2 = ‘”‘ . arg2 . ‘”‘ | endif
let arg3 = v:fname_out
if arg3 =~ ‘ ‘ | let arg3 = ‘”‘ . arg3 . ‘”‘ | endif
let eq = ”
if $VIMRUNTIME =~ ‘ ‘
if &sh =~ ‘\
let cmd = ‘”"‘ . $VIMRUNTIME . ‘\diff”‘
let eq = ‘”‘
else
let cmd = substitute($VIMRUNTIME, ‘ ‘, ‘” ‘, ”) . ‘\diff”‘
endif
else
let cmd = $VIMRUNTIME . ‘\diff’
endif
silent execute ‘!’ . cmd . ‘ ‘ . opt . arg1 . ‘ ‘ . arg2 . ‘ > ‘ . arg3 . eq
endfunction

” When started as “evim”, evim.vim will already have done these settings.
if v:progname =~? “evim”
finish
endif

” Use Vim settings, rather then Vi settings (much better!).
” This must be first, because it changes other options as a side effect.
set nocompatible

” allow backspacing over everything in insert mode
set backspace=indent,eol,start

if has(“vms”)
set nobackup ” do not keep a backup file, use versions instead
else
set backup ” keep a backup file
endif
set history=50 ” keep 50 lines of command line history
set ruler ” show the cursor position all the time
set showcmd ” display incomplete commands
set incsearch ” do incremental searching

” For Win32 GUI: remove ‘t’ flag from ‘guioptions’: no tearoff menu entries
” let &guioptions = substitute(&guioptions, “t”, “”, “g”)

” Don’t use Ex mode, use Q for formatting
map Q gq

” This is an alternative that also works in block mode, but the deleted
” text is lost and it only works for putting the current register.
“vnoremap p “_dp

” Switch syntax highlighting on, when the terminal has colors
” Also switch on highlighting the last used search pattern.
if &t_Co > 2 || has(“gui_running”)
syntax on
set hlsearch
endif

” Only do this part when compiled with support for autocommands.
if has(“autocmd”)

” Enable file type detection.
” Use the default filetype settings, so that mail gets ‘tw’ set to 72,
” ‘cindent’ is on in C files, etc.
” Also load indent files, to automatically do language-dependent indenting.
filetype plugin indent on

” Put these in an autocmd group, so that we can delete them easily.
augroup vimrcEx
au!

” For all text files set ‘textwidth’ to 78 characters.
autocmd FileType text setlocal textwidth=78

” When editing a file, always jump to the last known cursor position.
” Don’t do it when the position is invalid or when inside an event handler
” (happens when dropping a file on gvim).
autocmd BufReadPost *
\ if line(“‘\”") > 0 && line(“‘\”") <= line(“$”) |
\ exe “normal g`\”" |
\ endif

augroup END

else

set autoindent ” always set autoindenting on

endif ” has(“autocmd”)

“以下内容为自己定义
colorscheme oceandeep “配色方案
set nu “显示行号
“set guifont=monaco:h9:cANSI “字体
set hls “搜索高亮显示
set sm “括号配对
set nobackup

” 设定缩进参数以及 Tab
set autoindent
set expandtab
set shiftwidth=4
set softtabstop=4
set tabstop=4
” end设定缩进参数以及 Tab

au GUIEnter * simalt ~x “启动窗口最大

” 关闭工具栏
set guioptions=m

“You can obtain the completion dictionary file from:
” http://cvs.php.net/viewvc.cgi/phpdoc/funclist.txt
set dictionary-=$VIM/php.txt dictionary+=$VIM/php.txt

“Use the dictionary completion
set complete-=k complete+=k

“Auto completion using the TAB key
“This function determines, wether we are on the start of the line text(then tab indents) or
“if we want to try auto completion
function! InsertTabWrapper()
let col=col(‘.’)-1
if !col || getline(‘.’)[col-1] !~ ‘\k’
return “\
else
return “\
endif
endfunction

“Remap the tab key to select action with InsertTabWrapper
inoremap =InsertTabWrapper()

“此四行解决所有乱码问题
set langmenu=zh_CN.utf8
set fileencodings=utf-8,cp936,big5,latin1
set ambiwidth=double
let $LANG=’en’

set gfs=monaco
set guifontset=monaco
“set guifont=Consolas:h9:cANSI
“set guifont=Consolas:h9:cANSI
set guifont=Consolas:h9:cANSI
“set guifont=Bitstream_Vera_Sans_Mono:h9:cANSI

“set guifontwide=新宋体:w5:h9:cGB2312

” 进入插入模式时改变状态栏颜色(仅限于Vim 7)
set laststatus=2
if version >= 700
au InsertEnter * hi StatusLine guibg=#818D29 guifg=#FCFCFC gui=none
au InsertLeave * hi StatusLine guibg=#EEEEEE guifg=#363636 gui=none
endif

ab f function () {

2 引用 to “vim中解决中文乱码完美解决方案”

  1. Gravatar Icon kiss sky 回复说:

    请教一下有没有在vim里格式化代码的plugin和方法。实现像eclipse里ctrl+shift+F的功能:

    自动调整缩进
    自动调整if { begin end的缩进对齐等
    自动换行

  2. Gravatar Icon admin 回复说:

    这个需要写缩进代码,可以去vim.org寻找下,应该有很多脚本

给我回复