Vim 配置实例

"=====================================================================================
" Description: My daily vimrc with dozens of plugins.
" Originally derived from MetaCosm's http://vi-improved.org/vimrc.php
" Last Change: 0 14/05/2006 20:54:07 Leal@RAINT:_vimrc
"
" Author: Leal <linxiao.li AT gmail DOT com>
" http://www.leal.cn
" Version: 1.0065
"
" Usage: 1. Prepare necessary dirs and files
" $VIMDATA X:\Vim\vimdata on Win, ~/vimdata on Linux
" |
" |-- temp to put swap files <DIR>
" |-- backup to put backup files <DIR>
" |-- diary to save calendar.vim's diaries <DIR>
" |-- GetLatest to save GetLatestVimScripts.vim's <DIR>
" | |
" | `-- GetLatestVimScripts.dat to store GLVS's items
" |
" |-- _vim_fav_files to store favmenu.vim's items
" `-- _vim_mru_files to store mru.vim's items
"
" 2. Get all scripts you favor on www.vim.org better with GLVS
"
" 3. Get all needed utilities, especially on Windows, e.g.
" wget -- WGET for Windows (win32) http://users.ugent.be/~bpuype/wget/
" ctags -- Exuberant Ctags http://ctags.sf.net/
"
" 4. If you have no idea of some option, just press K (<S-k>) on it.
"
" 5. HTML file is produced with :TOhtml, with colo default.
"
"=====================================================================================

"-------------------------------------------------------------------------------------
" TODO List
"-------------------------------------------------------------------------------------
" 1. Install Chinese Vim help helplang -> cn

"-------------------------------------------------------------------------------------
" general
"-------------------------------------------------------------------------------------
set nocompatible " use vim as vim, should be put at the very start
set history=1000 " lines of Ex-mode commands, search history
set browsedir=buffer " use directory of the related buffer for file browser
set clipboard+=unnamed " use clipboard register '*' for all y, d, c, p ops
set viminfo+=! " make sure it can save viminfo
set isk+=$,%,#,- " none of these should be word dividers
set confirm " raise a dialog confirm whether to save changed buffer
set ffs=unix,dos,mac " favor unix ff, which behaves good under bot Winz & Linux
set fenc=utf-8 " default fileencoding
set fencs=utf-8,ucs-bom,euc-jp,gb18030,gbk,gb2312,cp936
map Q gq
" don't use Ex-mode, use Q for formatting
filetype on " enable file type detection
filetype plugin on " enable loading the plugin for appropriate file type

"-------------------------------------------------------------------------------------
" platform dependent
"-------------------------------------------------------------------------------------
if has("win32")
let $VIMDATA = $VIM.'/vimdata'
let $VIMFILES = $VIM.'/vimfiles'
let PYTHON_BIN_PATH = 'd:/foo/python/python.exe' " ensure the path right
else
let $VIMDATA = $HOME.'/vimdata'
let $VIMFILES = $HOME.'/.vim'
let PYTHON_BIN_PATH = '/usr/bin/python'
set guifont=courier\ 10
endif

"-------------------------------------------------------------------------------------
" path/backup
"-------------------------------------------------------------------------------------
set backup " make backup file and leave it around
set backupdir=$VIMDATA/backup " where to put backup file
set directory=$VIMDATA/temp " where to put swap file
set runtimepath+=$VIMDATA " add this path to rtp, support GetLatestVimScripts.vim
set path=.,/usr/include/*,, " where gf, ^Wf, :find will search
set tags=./tags,tags,$VIMRUNTIME/doc/tags,$VIMFILES/doc/tags " tags files CTRL-] uses
set makeef=error.err " the errorfile for :make and :grep

"-------------------------------------------------------------------------------------
" colors
"-------------------------------------------------------------------------------------
set background=dark " use a dark background
syntax on " syntax highlighting

"-------------------------------------------------------------------------------------
" gui-only settings
"-------------------------------------------------------------------------------------
if has("gui_running")
colo inkpot " colorscheme, inkpot.vim
set lines=40 " window tall and wide, only if gui_running,
set columns=120 " or vim under console behaves badly
endif

"-------------------------------------------------------------------------------------
" Vim UI
"-------------------------------------------------------------------------------------
set linespace=1 " space it out a little more (easier to read)
set wildmenu " type :h and press <Tab> to look what happens
set ruler " always show current position along the bottom
set cmdheight=2 " use 2 screen lines for command-line
set lazyredraw " do not redraw while executing macros (much faster)
set nonumber " don't print line number
set hid " allow to change buffer without saving
set backspace=2 " make backspace work normal
set whichwrap+=<,>,h,l " allow backspace and cursor keys to wrap
set mouse=a " use mouse in all modes
set shortmess=atI " shorten messages to avoid 'press a key' prompt
set report=0 " tell us when anything is changed via :...
set fillchars=vert:\ ,stl:\ ,stlnc:\
" make the splitters between windows be blank

"-------------------------------------------------------------------------------------
" visual cues
"-------------------------------------------------------------------------------------
set showmatch " show matching paren
set matchtime=5 " 1/10 second to show the matching paren
set nohlsearch " do not highlight searched for phrases
set incsearch " BUT do highlight where the so far typed pattern matches
set scrolloff=10 " minimal number of screen lines to keep above/below the cursor
set novisualbell " use visual bell instead of beeping
set noerrorbells " do not make noise
set laststatus=2 " always show the status line
set listchars=tab:\|\ ,trail:.,extends:>,precedes:<,eol:$ " what to show while :set list
set statusline=%{VimBuddy()}\ %F%m%r%h%w\ [%{&ff}]\ [%Y]\ [\%03.3b\ \%02.2B]\ [%02v\ %03l\ %L\ %p%%]

"-------------------------------------------------------------------------------------
" text formatting/layout
"-------------------------------------------------------------------------------------
set ai " autoindent
set si " smartindent
set cindent " do C-style indenting
set fo=tcrqn " see help (complex)
set tabstop=4 " tab spacing (settings below are just to unify it)
set softtabstop=4 " unify
set shiftwidth=4 " unify
set noexpandtab " real tabs please!
set smarttab " use tabs at the start of a line, spaces elsewhere
set nowrap " do not wrap lines
set formatoptions+=mM " so that vim can reformat multibyte text (e.g. Chinese)

"-------------------------------------------------------------------------------------
" folding
"-------------------------------------------------------------------------------------
set foldenable " turn on folding
set foldmethod=indent " make folding indent sensitive
set foldlevel=100 " don't autofold anything (but I can still fold manually)
set foldopen -=search " don't open folds when you search into them
set foldopen -=undo " don't open folds when you undo stuff

"-------------------------------------------------------------------------------------
" plugin - a.vim
"-------------------------------------------------------------------------------------
" alternate files quickly (.c --> .h etc)

"-------------------------------------------------------------------------------------
" plugin - c.vim
"-------------------------------------------------------------------------------------
"set makeprg=g++\ %
let g:C_AuthorName = 'Leal'
let g:C_Email = 'linxiao.li NOSPAM gmail DOT com'

"-------------------------------------------------------------------------------------
" plugin - runscript.vim (for Python)
"-------------------------------------------------------------------------------------
"let PYTHON_BIN_PATH = ...

"-------------------------------------------------------------------------------------
" plugin - calendar.vim
"-------------------------------------------------------------------------------------
let g:calendar_diary = $VIMDATA.'/diary' " where to store your diary

"-------------------------------------------------------------------------------------
" plugin - mru.vim (most recently used files)
"-------------------------------------------------------------------------------------
let MRU_File = $VIMDATA.'/_vim_mru_files' " which file to save mru entries
let MRU_Max_Entries = 20 " max mru entries in _vim_mru_files

"-------------------------------------------------------------------------------------
" plugin - favmenu.vim
"-------------------------------------------------------------------------------------
let FAV_File = $VIMDATA.'/_vim_fav_files' " which file to save favorite entries

"-------------------------------------------------------------------------------------
" plugin - minibufexpl.vim
"-------------------------------------------------------------------------------------
let g:miniBufExplTabWrap = 1 " make tabs show complete (no broken on two lines)
let g:miniBufExplModSelTarget = 1

"-------------------------------------------------------------------------------------
" plugin - taglist.vim
"-------------------------------------------------------------------------------------
if has("win32")
let Tlist_Ctags_Cmd = $VIMFILES.'/ctags.exe' " location of ctags tool
else
let Tlist_Ctags_Cmd = '/usr/local/bin/ctags'
endif

let Tlist_Sort_Type = "name" " order by
let Tlist_Use_Right_Window = 1 " split to the right side of the screen
let Tlist_Compart_Format = 1 " show small meny
let Tlist_Exist_OnlyWindow = 1 " if you are the last, kill yourself
let Tlist_File_Fold_Auto_Close = 0 " do not close tags for other files
let Tlist_Enable_Fold_Column = 0 " do not show folding tree

"-------------------------------------------------------------------------------------
" plugin - matchit.vim
"-------------------------------------------------------------------------------------
let b:match_ignorecase = 1

"-------------------------------------------------------------------------------------
" plugin - supertab.vim
"-------------------------------------------------------------------------------------
" <Tab> has been mapped to SuperTab() func in that plugin

"-------------------------------------------------------------------------------------
" plugin - timestamp.vim
"-------------------------------------------------------------------------------------
let g:timestamp_regexp = '\v\C%(<Last %([cC]hanged?|[Mm]odified):\s+)@<=.*$'
let g:timestamp_rep = '%w %d/%m/%Y %H:%M:%S #u@#h:#f'
augroup TimeStampHtml
au filetype html let b:timestamp_regexp = '\v\C%(\<!-- Timestamp:\s{-1,})@<=.{-}%(\s*--\>)@='
au filetype html let b:timestamp_rep = '%a %d/%m/%Y %r #u@#h:#f'
augroup END

"-------------------------------------------------------------------------------------
" plugin - perl-support.vim
"-------------------------------------------------------------------------------------
let g:Perl_AuthorName = 'Leal'
let g:Perl_Email = 'linxiao.li NOSPAM gmail DOT com'
let tlist_perl_settings = 'perl;c:constants;l:labels;s:subroutines;d:POD'

"-------------------------------------------------------------------------------------
" utilities
"-------------------------------------------------------------------------------------
" select range, then hit :SuperRetab($width) - by p0g and FallingCow
fu! SuperRetab(width) range
sil! exe a:firstline . ',' . a:lastline . 's/\v%(^ *)@<= {'. a:width .'}/\t/g'
endf

" bind :CD to :cd %:h, then change cwd to the dir that includes current file
sil! com -nargs=0 CD exe 'cd %:h'

"TODO just do it
fu! AddLineNo(isVM)
if(a:isVM == 1)
sil! exe 's/^/\=' . strpart((line('.')-line("'<")+1)." ", 0, 4)
else
sil! exe '%s/^/\=' . strpart(line('.')." ", 0, 4)
endif
endf
" add line number befor each line of the text
" :g/^/exec "s/^/".strpart(line(".")." ", 0, 4)
" :%s/^/\=strpart(line('.')." ", 0, 4)
" visual mode
" :s/^/\=strpart((line('.')-line("'<")+1)." ", 0, 4)

" XML support, e.g. element,,,<CR> -> <element> </element>
" Bart van Deenen , www.vandeenensupport.com
fu! MakeElement()
if match(getline('.'),'^\s*>\s*$') == -1
"the deleted word was not alone on the line
let @w = "i<pla</pa>F<i"
else
"the deleted word was on it's own on the line
let @w = "i<po</pa>kA"
endif
endf

" include colon(5Cool for namespaces in xsl for instance
"setlocal iskeyword=@,48-57,_,192-255,58
inoremap <buffer> ,,, ><Esc>db:call MakeElement()<enter>@w

"-------------------------------------------------------------------------------------
" mappings
"-------------------------------------------------------------------------------------
map <right> <ESC>:MBEbn<RETURN>
" -> switches buffers
map <left> <ESC>:MBEbp<RETURN>
" <- switches buffers
map <up> <ESC>:Sex<RETURN><ESC><C-W><C-W>
" up arrow to bring up a file explorer
map <down> <ESC>:Tlist<RETURN>
" down arrow to bring up the taglist
map <A-i> i <ESC>r
" Alt-i inserts a single char, and back to normal
map <F3> <ESC>ggVG:call SuperRetab()<left>
map <F4> ggVGg?
" Rot13 encode the current file

"noremap <silent> <C-F11> :cal VimCommanderToggle()<CR>

" plugin - php_console.vim
"map <F5> :call ParsePhpFile()<cr> " call function in normal mode
"imap <F5> <ESC>:call ParsePhpFile()<cr> " call function in insert mode

"-------------------------------------------------------------------------------------
" autocommands
"-------------------------------------------------------------------------------------
au BufEnter * :syntax sync fromstart " ensure every file does syntax highlighting (full)
au BufNewFile,BufRead *.asp :set ft=jscript " all my .asp files ARE jscript
au BufNewFile,BufRead *.tpl :set ft=html " all my .tpl files ARE html

" ftplugin - python_fold - $VIMFILES/ftplugin/python_fold.vim
" add names in pydiction to autocomplete class or object's class, attribute or method
au FileType python set complete+=k$VIMFILES/dict/pydiction isk+=.,(
"au FileType python pyfile $VIMFILES/plugin/pyCallTips.py
au FileType python source $VIMFILES/plugin/python.vim
"au FileType java source $VIMFILES/ftplugin/JavaRun.vim
"au FileType php set complete+=k$VIMFILES/dict/php.dict isk+=.,(
au FileType text setlocal textwidth=78

"-------------------------------------------------------------------------------------
" highlight active line in normal mode, Vim7 don't need this
"-------------------------------------------------------------------------------------
"highlight CurrentLine guibg=darkgrey guifg=white ctermbg=darkgrey ctermfg=white
"au! Cursorhold * exe 'match CurrentLine /\%' . line('.') . 'l.*/'
"set ut=19

"-------------------------------------------------------------------------------------
" stuffs I don't like
"-------------------------------------------------------------------------------------
"set ignorecase -- turns out, I like case sensitivity
"set list -- turns out, do not display unprintable characters such as Tab
"autocmd GUIEnter * :simalt ~x -- having it auto maximize the screen is annoying
"autocmd BufEnter * :lcd %:p:h -- switch to current dir (breaks some scripts)

"-------------------------------------------------------------------------------------
" useful abbrevs
"-------------------------------------------------------------------------------------
iab xasp <%@language=jscript%><CR><%<CR><TAB><CR><BS>%><ESC><<O<TAB>
iab xdate <c-r>=strftime("%m/%d/%y %H:%M:%S")<cr>

"-------------------------------------------------------------------------------------
" customize cursor color to indicate IM is on
"-------------------------------------------------------------------------------------
if has('multi_byte_ime')
hi Cursor guifg=NONE guibg=Green
hi CursorIM guifg=NONE guibg=Blue
endif

"-------------------------------------------------------------------------------------
" TVO defaults - otl.vim
"-------------------------------------------------------------------------------------
let otl_install_menu =1
let no_otl_maps =0
let no_otl_insert_maps =0

let otl_bold_headers =0
let otl_use_thlnk =0

let g:otl_use_viki =0
let maplocalleader =","

map <c-w><c-f> :FirstExplorerWindow<cr>
map <c-w><c-b> :BottomExplorerWindow<cr>
map <c-w><c-t> :WMToggle<cr>

let g:winManagerWidth = 35
let g:winManagerWindowLayout = 'TodoList'

let g:tskelDir = $VIMFILES."/skeletons"

"-------------------------------------------------------------------------------------
" vim: set et ft=vim tw=98 path+=$VIMFILES/*:

Vim 是最重要的编辑器之一,主要有下面几个优点。

    可以不使用鼠标,完全用键盘操作。
    系统资源占用小,打开大文件毫无压力。
    键盘命令变成肌肉记忆以后,操作速度极快。
    服务器默认都安装 Vi 或 Vim。

Vim 的配置不太容易,它有自己的语法,许许多多的命令。我总是记不清楚,所以就整理了下面这篇文章,列出主要配置项的含义。
一、基础知识

Vim 的全局配置一般在/etc/vim/vimrc或者/etc/vimrc,对所有用户生效。用户个人的配置在~/.vimrc。

如果只对单次编辑启用某个配置项,可以在命令模式下,先输入一个冒号,再输入配置。举例来说,set number这个配置可以写在.vimrc里面,也可以在命令模式输入。

:set number

配置项一般都有"打开"和"关闭"两个设置。“关闭"就是在"打开"前面加上前缀"no”。

" 打开
set number

" 关闭
set nonumber

上面代码中,双引号开始的行表示注释。

查询某个配置项是打开还是关闭,可以在命令模式下,输入该配置,并在后面加上问号。

:set number?

上面的命令会返回number或者nonumber。

如果想查看帮助,可以使用help命令。

:help number

二、基本配置

(1)

set nocompatible

不与 Vi 兼容(采用 Vim 自己的操作命令)。

(2)

syntax on

打开语法高亮。自动识别代码,使用多种颜色显示。

(3)

set showmode

在底部显示,当前处于命令模式还是插入模式。

(4)

set showcmd

命令模式下,在底部显示,当前键入的指令。比如,键入的指令是2y3d,那么底部就会显示2y3,当键入d的时候,操作完成,显示消失。

(5)

set mouse=a

支持使用鼠标。

(6)

set encoding=utf-8  

使用 utf-8 编码。

(7)

set t_Co=256

启用256色。

(8)

filetype indent on

开启文件类型检查,并且载入与该类型对应的缩进规则。比如,如果编辑的是.py文件,Vim 就是会找 Python 的缩进规则~/.vim/indent/python.vim。
三、缩进

(9)

set autoindent

按下回车键后,下一行的缩进会自动跟上一行的缩进保持一致。

(10)

set tabstop=2

按下 Tab 键时,Vim 显示的空格数。

(11)

set shiftwidth=4

在文本上按下>>(增加一级缩进)、<<(取消一级缩进)或者==(取消全部缩进)时,每一级的字符数。

(12)

set expandtab

由于 Tab 键在不同的编辑器缩进不一致,该设置自动将 Tab 转为空格。

(13)

set softtabstop=2

Tab 转为多少个空格。
四、外观

(14)

set number

显示行号

(15)

set relativenumber

显示光标所在的当前行的行号,其他行都为相对于该行的相对行号。

(16)

set cursorline

光标所在的当前行高亮。

(17)

set textwidth=80

设置行宽,即一行显示多少个字符。

(18)

set wrap

自动折行,即太长的行分成几行显示。

set nowrap

关闭自动折行

(19)

set linebreak

只有遇到指定的符号(比如空格、连词号和其他标点符号),才发生折行。也就是说,不会在单词内部折行。

(20)

set wrapmargin=2

指定折行处与编辑窗口的右边缘之间空出的字符数。

(21)

set scrolloff=5

垂直滚动时,光标距离顶部/底部的位置(单位:行)。

(22)

set sidescrolloff=15

水平滚动时,光标距离行首或行尾的位置(单位:字符)。该配置在不折行时比较有用。

(23)

set laststatus=2

是否显示状态栏。0 表示不显示,1 表示只在多窗口时显示,2 表示显示。

(24)

set  ruler

在状态栏显示光标的当前位置(位于哪一行哪一列)。
五、搜索

(25)

set showmatch

光标遇到圆括号、方括号、大括号时,自动高亮对应的另一个圆括号、方括号和大括号。

(26)

set hlsearch

搜索时,高亮显示匹配结果。

(27)

set incsearch

输入搜索模式时,每输入一个字符,就自动跳到第一个匹配的结果。

(28)

set ignorecase

搜索时忽略大小写。

(29)

set smartcase

如果同时打开了ignorecase,那么对于只有一个大写字母的搜索词,将大小写敏感;其他情况都是大小写不敏感。比如,搜索Test时,将不匹配test;搜索test时,将匹配Test。
六、编辑

(30)

set spell spelllang=en_us

打开英语单词的拼写检查。

(31)

set nobackup

不创建备份文件。默认情况下,文件保存时,会额外创建一个备份文件,它的文件名是在原文件名的末尾,再添加一个波浪号(〜)。

(32)

set noswapfile

不创建交换文件。交换文件主要用于系统崩溃时恢复文件,文件名的开头是.、结尾是.swp。

(33)

set undofile

保留撤销历史。

Vim 会在编辑时保存操作历史,用来供用户撤消更改。默认情况下,操作记录只在本次编辑时有效,一旦编辑结束、文件关闭,操作历史就消失了。

打开这个设置,可以在文件关闭后,操作记录保留在一个文件里面,继续存在。这意味着,重新打开一个文件,可以撤销上一次编辑时的操作。撤消文件是跟原文件保存在一起的隐藏文件,文件名以.un~开头。

(34)

set backupdir=~/.vim/.backup//  
set directory=~/.vim/.swp//
set undodir=~/.vim/.undo// 

设置备份文件、交换文件、操作历史文件的保存位置。

结尾的//表示生成的文件名带有绝对路径,路径中用%替换目录分隔符,这样可以防止文件重名。

(35)

set autochdir

自动切换工作目录。这主要用在一个 Vim 会话之中打开多个文件的情况,默认的工作目录是打开的第一个文件的目录。该配置可以将工作目录自动切换到,正在编辑的文件的目录。

(36)

set noerrorbells

出错时,不要发出响声。

(37)

set visualbell

出错时,发出视觉提示,通常是屏幕闪烁。

(38)

set history=1000

Vim 需要记住多少次历史操作。

(39)

set autoread

打开文件监视。如果在编辑过程中文件发生外部改变(比如被别的编辑器编辑了),就会发出提示。

(40)

set listchars=tab:»■,trail:■
set list

如果行尾有多余的空格(包括 Tab 键),该配置将让这些空格显示成可见的小方块。

(41)

set wildmenu
set wildmode=longest:list,full

命令模式下,底部操作指令按下 Tab 键自动补全。第一次按下 Tab,会显示所有匹配的操作指令的清单;第二次按下 Tab,会依次选择各个指令。
七、参考链接

Turn your vim editor into a productivity powerhouse
A Good Vimrc
Vim documentation: options
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值