准备知识
以7.2版为例,VIM安装完整后的目录结构一般为
vim/vim72
vim/vimfiles
在VIM启动的时候,会准备两个环境变量,$VIM和$VIMRUNTIME,分别指向vim和vim/vim72
一般gVim会在$VIM生成一个_vimrc,这个就是VIM的配置文件了.对VIM的配置都通过修改这个文件来安成.
借助Cygwin将_vimrc重命名为.vimrc,Windows是不允许这样的命名规则的.
我们也可以把.vimrc放置于其他地方,VIM启动时会先搜索$HOME这个系统变量指定的目录,
因此我们只需要添加一个$HOME环境变量,并把.vimrc放到这里即可实现Cygwin和gVim共用一个.vimrc
启动后最大化
if has("win32")
au GUIEnter * simalt ~x
endif
实际上是调用一个自动命令,在GUIEnter之后(进入图形界面后),控制Alt+Space键和X键,即完成窗口最大化.
simalt说明,在VIM中输入":h simalt",回车查看帮助.
全屏幕运行
gvimfullscreen_win32
解压到$VIMRUNTIME,调用这个动态连接库:
call libcallnr("gvimfullscreen.dll", "ToggleFullScreen", 0)<CR>
或者映射快捷键:
map <F11> <Esc>:call libcallnr("gvimfullscreen.dll", "ToggleFullScreen", 0)<CR>
按F11就可以切换全屏和非全屏了.
需要注意的是,VIM默认有菜单栏和工具栏,需要去掉这两栏,
以及Tab的显示才能做到真正全屏幕模式:
set guioptions-=m "Menu Bar
set guioptions-=T "Tool Bar
map <silent> <F2> :if &guioptions =~# 'T' <Bar>
/set guioptions-=T <Bar>
/set guioptions-=m <bar>
/else <Bar>
/set guioptions =T <Bar>
/set guioptions =m <Bar>
/endif<CR>
按F2即可切换显示或者关闭显示菜单栏和工具栏.
关闭显示Tabs:
set showtabline=0
showtabline的用法参考:h showtabline
背景透明
VimTweak
解压到$VIMRUNTIME,仍然是调用动态链接库。比如映射为F12:
if executable("vimtweak.dll")
autocmd guienter* call libcallnr("vimtweak","SetAlpha",222)
endif
关闭警告声音
警告声音会很烦人,以下配置彻底关闭警告声,有时候仅关闭其中一种是不够的。
"Make no bells,and no blink.
au VimEnter * set vb t_vb=
setnoerrorbells
setnovisualbell
具体说明查看帮助.
不兼容VI模式,除非你需要用到:
setnocompatible
具体说明参考h: nocompatible
引入4个文件,myfunc.vim是一些需要用到的自定义函数.
source $VIMRUNTIME/myfunc.vim
if has("win32")
source $VIMRUNTIME/delmenu.vim
source $VIMRUNTIME/menu.vim
source $VIMRUNTIME/mswin.vim
behave mswin
language messages en_US.utf-8
endif
具体说明查看帮助.
VIM打开文件编辑的时候会创建swap文件以及备份文件,如果不需要可以关闭:
setnoswapfile
setnobackup
编码设置
setencoding=utf-8
setfileencodings=utf-8,chinese,latin-1
VIM有fileencodings,fileencoding,encoding,termencoding几个编码选项, 具体说明.
VIM打开文件后都会创建一个缓冲区,首先探测文件编码格式,
然后转为内部编码,保存文件则是个逆向过程.
Leader设置
letmapleader = ","
很多时候我们需要用到快捷方式调用某个功能或完成某项操作,
则会用到<leader>键作为前缀,默认用"/"。
说明参考:h leader
VIM帮助
VIM是我见过帮助文档最完善的软件了,善用帮助文档.
如果你有中文帮助文档,可以设置为中文。
sethelplang=cn
中文帮助文档 下载
菜单和提示消息语言设置
language messages en_US.utf-8
或者
language messages cn_ZH.utf-8
其他
has("win32")是VIM脚本,如果返回真,表示当前VIM支持WIN32,具体参考:h win32.
由于配置的时候要兼容Windows和Unix两种环境下的模式,需要判断当前系统,
我写到了myfunc.vim里面.这样我们就可以只需一个.vimrc文件,就可以应用于两种环境下.
如果有自定义的函数也可以写到单独的文件中,然后在.vimrc里调用,myfunc.vim全部内容:
"Functions
"-------------------------------------------------------------------------------
"MyDiff
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 =~ '/<cmd'
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
"MySys
"-------------------------------------------------------------------------------
function! MySys()
if has("win32")
return "windows"
else
return "Unix"
endif
endfunction
"SwitchToBuf
function! SwitchToBuf(filename)
"let fullfn = substitute(a:filename, "^//~/", $VIM . "/", "")
" find in current tab
let bufwinnr = bufwinnr(a:filename)
if bufwinnr != -1
exec bufwinnr . "wincmd w"
return
else
" find in each tab
tabfirst
let tab = 1
while tab <= tabpagenr("$")
let bufwinnr = bufwinnr(a:filename)
if bufwinnr != -1
exec "normal " . tab . "gt"
exec bufwinnr . "wincmd w"
return
endif
tabnext
let tab = tab + 1
endwhile
" not exist, new tab
exec "tabnew " . a:filename
Sexplore!
wincmd l
endif
endfunction
"ModLog
function! ModLog()
let baseline = line('.') + 1
call append (baseline," /*" . strftime("%Y/%m/%d") . " NAME?????????? */")
call append (baseline+1," /* " . strftime("XXX%m%d") . " DESCRIPTION*/")
call append (baseline+2," /********************************************************************/")
call cursor (baseline+1,1)
endfunction
"PliCommentLine
function! PliCommentLine(parm)
let lnum = line('.')
let line = getline('.')
let len= len(line)
if len < 72
let trimline = strpart(line, 0, len )
let snum = 72 - len
call setline(lnum, trimline . repeat(" ", snum))
let line = getline('.')
endif
if a:parm == "f"
" let column = 71
" let end = -1
" while column > 58
" if line[column] =~ '/S'
" let end=column
" break
" else
" let end=column
" endif
" let column -= 1
" endwhile
" let trimline = strpart(line, 1, end )
let trimline = strpart(line, 0, 62 )
call setline(lnum,trimline . "XXX" . strftime("%m%d") . "/*//")
endif
if a:parm == "l"
" let column = 71
" let end = -1
" while column > 65
" if line[column] =~ '/S'
" let end=column
" break
" else
" let end=column
" endif
" let column -= 1
" endwhile
" let trimline = strpart(line, 1, end )
let trimline = strpart(line, 1, 66 )
call setline(lnum," ///*" . trimline . "/*//")
endif
if a:parm == "t"
let trimline = strpart(line, 0, 60 )
call setline(lnum, trimline . "///*XXX" . strftime("%m%d") . "/*//")
endif
endfunction <!--v:3.2-->
以7.2版为例,VIM安装完整后的目录结构一般为
vim/vim72
vim/vimfiles
在VIM启动的时候,会准备两个环境变量,$VIM和$VIMRUNTIME,分别指向vim和vim/vim72
一般gVim会在$VIM生成一个_vimrc,这个就是VIM的配置文件了.对VIM的配置都通过修改这个文件来安成.
借助Cygwin将_vimrc重命名为.vimrc,Windows是不允许这样的命名规则的.
我们也可以把.vimrc放置于其他地方,VIM启动时会先搜索$HOME这个系统变量指定的目录,
因此我们只需要添加一个$HOME环境变量,并把.vimrc放到这里即可实现Cygwin和gVim共用一个.vimrc
启动后最大化
if has("win32")
au GUIEnter * simalt ~x
endif
实际上是调用一个自动命令,在GUIEnter之后(进入图形界面后),控制Alt+Space键和X键,即完成窗口最大化.
simalt说明,在VIM中输入":h simalt",回车查看帮助.
全屏幕运行
gvimfullscreen_win32
解压到$VIMRUNTIME,调用这个动态连接库:
call libcallnr("gvimfullscreen.dll", "ToggleFullScreen", 0)<CR>
或者映射快捷键:
map <F11> <Esc>:call libcallnr("gvimfullscreen.dll", "ToggleFullScreen", 0)<CR>
按F11就可以切换全屏和非全屏了.
需要注意的是,VIM默认有菜单栏和工具栏,需要去掉这两栏,
以及Tab的显示才能做到真正全屏幕模式:
set guioptions-=m "Menu Bar
set guioptions-=T "Tool Bar
map <silent> <F2> :if &guioptions =~# 'T' <Bar>
/set guioptions-=T <Bar>
/set guioptions-=m <bar>
/else <Bar>
/set guioptions =T <Bar>
/set guioptions =m <Bar>
/endif<CR>
按F2即可切换显示或者关闭显示菜单栏和工具栏.
关闭显示Tabs:
set showtabline=0
showtabline的用法参考:h showtabline
背景透明
VimTweak
解压到$VIMRUNTIME,仍然是调用动态链接库。比如映射为F12:
if executable("vimtweak.dll")
autocmd guienter* call libcallnr("vimtweak","SetAlpha",222)
endif
关闭警告声音
警告声音会很烦人,以下配置彻底关闭警告声,有时候仅关闭其中一种是不够的。
"Make no bells,and no blink.
au VimEnter * set vb t_vb=
setnoerrorbells
setnovisualbell
具体说明查看帮助.
不兼容VI模式,除非你需要用到:
setnocompatible
具体说明参考h: nocompatible
引入4个文件,myfunc.vim是一些需要用到的自定义函数.
source $VIMRUNTIME/myfunc.vim
if has("win32")
source $VIMRUNTIME/delmenu.vim
source $VIMRUNTIME/menu.vim
source $VIMRUNTIME/mswin.vim
behave mswin
language messages en_US.utf-8
endif
具体说明查看帮助.
VIM打开文件编辑的时候会创建swap文件以及备份文件,如果不需要可以关闭:
setnoswapfile
setnobackup
编码设置
setencoding=utf-8
setfileencodings=utf-8,chinese,latin-1
VIM有fileencodings,fileencoding,encoding,termencoding几个编码选项, 具体说明.
VIM打开文件后都会创建一个缓冲区,首先探测文件编码格式,
然后转为内部编码,保存文件则是个逆向过程.
Leader设置
letmapleader = ","
很多时候我们需要用到快捷方式调用某个功能或完成某项操作,
则会用到<leader>键作为前缀,默认用"/"。
说明参考:h leader
VIM帮助
VIM是我见过帮助文档最完善的软件了,善用帮助文档.
如果你有中文帮助文档,可以设置为中文。
sethelplang=cn
中文帮助文档 下载
菜单和提示消息语言设置
language messages en_US.utf-8
或者
language messages cn_ZH.utf-8
其他
has("win32")是VIM脚本,如果返回真,表示当前VIM支持WIN32,具体参考:h win32.
由于配置的时候要兼容Windows和Unix两种环境下的模式,需要判断当前系统,
我写到了myfunc.vim里面.这样我们就可以只需一个.vimrc文件,就可以应用于两种环境下.
如果有自定义的函数也可以写到单独的文件中,然后在.vimrc里调用,myfunc.vim全部内容:
"Functions
"-------------------------------------------------------------------------------
"MyDiff
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 =~ '/<cmd'
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
"MySys
"-------------------------------------------------------------------------------
function! MySys()
if has("win32")
return "windows"
else
return "Unix"
endif
endfunction
"SwitchToBuf
function! SwitchToBuf(filename)
"let fullfn = substitute(a:filename, "^//~/", $VIM . "/", "")
" find in current tab
let bufwinnr = bufwinnr(a:filename)
if bufwinnr != -1
exec bufwinnr . "wincmd w"
return
else
" find in each tab
tabfirst
let tab = 1
while tab <= tabpagenr("$")
let bufwinnr = bufwinnr(a:filename)
if bufwinnr != -1
exec "normal " . tab . "gt"
exec bufwinnr . "wincmd w"
return
endif
tabnext
let tab = tab + 1
endwhile
" not exist, new tab
exec "tabnew " . a:filename
Sexplore!
wincmd l
endif
endfunction
"ModLog
function! ModLog()
let baseline = line('.') + 1
call append (baseline," /*" . strftime("%Y/%m/%d") . " NAME?????????? */")
call append (baseline+1," /* " . strftime("XXX%m%d") . " DESCRIPTION*/")
call append (baseline+2," /********************************************************************/")
call cursor (baseline+1,1)
endfunction
"PliCommentLine
function! PliCommentLine(parm)
let lnum = line('.')
let line = getline('.')
let len= len(line)
if len < 72
let trimline = strpart(line, 0, len )
let snum = 72 - len
call setline(lnum, trimline . repeat(" ", snum))
let line = getline('.')
endif
if a:parm == "f"
" let column = 71
" let end = -1
" while column > 58
" if line[column] =~ '/S'
" let end=column
" break
" else
" let end=column
" endif
" let column -= 1
" endwhile
" let trimline = strpart(line, 1, end )
let trimline = strpart(line, 0, 62 )
call setline(lnum,trimline . "XXX" . strftime("%m%d") . "/*//")
endif
if a:parm == "l"
" let column = 71
" let end = -1
" while column > 65
" if line[column] =~ '/S'
" let end=column
" break
" else
" let end=column
" endif
" let column -= 1
" endwhile
" let trimline = strpart(line, 1, end )
let trimline = strpart(line, 1, 66 )
call setline(lnum," ///*" . trimline . "/*//")
endif
if a:parm == "t"
let trimline = strpart(line, 0, 60 )
call setline(lnum, trimline . "///*XXX" . strftime("%m%d") . "/*//")
endif
endfunction <!--v:3.2-->