owenzhang的博客

vim的IDE配置和vim tips

字数统计: 3.1k阅读时长: 17 min
2013/11/10
loading

写在前面

用vim时间也很久了,但是发现最初从网上cp了一份vimrc和插件后就没怎么大修改过,而且每次备份都是一堆的插件,删除都不知道怎么删。
最近知道了有vundle这个插件,重新配置了一下vim,发现vim有了足够都vimrc和插件后,功能大幅增长。自己平时也爱折腾,立下此文,以后都修改都列到这里。

VIM 配置

把从前的配置移植过来,用vundle配置了vimrc文件,见最后都附。

VIM tips

vim补全时发现候选菜单的上下选择ctrl-p, ctrl-n相反

原因:使用verbose imap <c-n> 查看是由于什么设置所致,发现是supertab插件导致,删除解决。

#include<iostream>
int main

参考

把vim打造成IDE

附:vimrc文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
" For vundle
set nocompatible " be iMproved
filetype off " required!

set rtp+=~/.vim/bundle/vundle/
call vundle#rc()

" let Vundle manage Vundle
" required!
Bundle 'gmarik/vundle'

" original repos on github
"Bundle 'mattn/zencoding-vim'
Bundle 'drmingdrmer/xptemplate'

" vim-scripts repos
Bundle 'EnhCommentify.vim'
Bundle 'a.vim'
Bundle 'omnicppcomplete.vim'
Bundle 'DoxygenToolkit.vim'
Bundle 'minibufexpl.vim'
"Bundle 'NERD_commenter.vim'
"Bundle 'The-NERD-Commenter'
Bundle 'bufexplorer.zip'
Bundle 'taglist.vim'
Bundle 'The-NERD-tree'
Bundle 'taglist.vim'
Bundle 'winmanager'
"Bundle 'SuperTab'
Bundle 'minibufexpl.vim'
Bundle 'Markdown'
Bundle 'c.vim'
Bundle 'snipMate'
Bundle 'bash-support.vim'
Bundle 'perl-support.vim'

"Bundle 'Lokaltog/vim-powerline'
"Bundle 'L9'
"Bundle 'FuzzyFinder'
"Bundle 'Mark'
"Bundle 'matrix.vim'
"Bundle 'closetag.vim'
"Bundle 'matchit.zip'
"Bundle 'AutoComplPop'
"Bundle 'jsbeautify'
"Bundle 'YankRing.vim'
"Bundle 'vimwiki'
"Bundle 'matrix.vim--Yang'
"Bundle 'FencView.vim'
"Bundle 'Conque-Shell'

" non github reposo
" 非github的插件,可以直接使用其git地址
" Bundle 'git://git.wincent.com/command-t.git'
" ...

"Bundle 支持的格式有三种:
"第一种是一个普通的字符串,就是 vim 插件页面里左上角的插件名字,如果插件名有空格,使用 - 替换。
"第二种是github帐号/项目名。
"第三种就是git repository
"现在大家就可以使用 :BundleInstall ,:BundleInstall! 来下载插件和更新插件了,是不是很方便。
"
" Brief help
" :BundleList - list configured bundles
" :BundleInstall(!) - install(update) bundles
" :BundleSearch(!) foo - search(or refresh cache first) for foo
" :BundleClean(!) - confirm(or auto-approve) removal of unused bundles
" vundle主要就是上面这个四个命令,例如BundleInstall是全部重新安装,BundleInstall!则是更新
" 一般安装插件的流程为,先BundleSearch一个插件,然后在列表中选中,按i安装
" 安装完之后,在vimrc中,添加Bundle 'XXX',使得bundle能够加载,这个插件,同时如果
" 需要配置这个插件,也是在vimrc中设置即可
" see :h vundle for more details or wiki for FAQ
" NOTE: comments after Bundle command are not allowed..



syn on "语法支持
"common conf {{ 通用配置
set ai "自动缩进
set bs=2 "在insert模式下用退格键删除
set showmatch "代码匹配
set laststatus=2 "总是显示状态行
set expandtab "以下三个配置配合使用,设置tab和缩进空格数
set shiftwidth=4
set tabstop=4
set cursorline "为光标所在行加下划线
set number "显示行号
set autoread "文件在Vim之外修改过,自动重新读入
set ignorecase "检索时忽略大小写
set hls "检索时高亮显示匹配项
set helplang=cn "帮助系统设置为中文
"set foldmethod=syntax "代码折叠
"检测文件类型,对于依赖文件类型功能有用
set history=1000
set vb t_vb=
"set nohls
set mouse=a
set cindent
set incsearch
set cursorline
set nobackup " make backup file
set autowrite " auto writefile when sth happened such as :make or :last or others.See the help
set makeef=error.err " When using make, where should it dump the file
set wildmenu " turn on wild menu
set ruler " Always show current positions along the bottom
set backspace=indent,eol,start
set so=7 "距下面的行数为7
colorscheme desert
set tabstop=4 " tab spacing (settings below are just to unify it)
set softtabstop=4 " unify
set shiftwidth=4 " unify
set nowrap " do not wrap lines
set smarttab " use tabs at the start of a line, spaces elsewhere
"解决乱码
set encoding=utf-8
set fileencoding=utf-8
set termencoding=utf-8
set fileencodings=ucs-bom,utf-8,gb18030,big5,gbk,gb2312,cp936
set viminfo+=! " make sure it can save viminfo
set isk+=_,$,@,%,#,- " none of these should be word dividers, so make them not be
"keep the last close cursor position
if has("autocmd")
autocmd BufReadPost *
\ if line("'\"") > 0 && line("'\"") <= line("$") |
\ exe "normal g`\"" |
\ endif
endif


"}}

"conf for tabs, 为标签页进行的配置,通过ctrl h/l切换标签等
"let mapleader = ','
nnoremap <C-l> gt
nnoremap <C-h> gT
nnoremap <leader>t : tabe<CR>

"conf for plugins {{ 插件相关的配置
""进行Tlist的设置
"""TlistUpdate可以更新tags
"map <F3> :silent! Tlist<CR> "按下F3就可以呼出了
"let Tlist_Ctags_Cmd='ctags' "因为我们放在环境变量里,所以可以直接执行
"let Tlist_Use_Right_Window=1 "让窗口显示在右边,0的话就是显示在左边
let Tlist_Show_One_File=1 "让taglist可以同时展示多个文件的函数列表,如果想只有1个,设置为1
"let Tlist_File_Fold_Auto_Close=1 "非当前文件,函数列表折叠隐藏
let Tlist_Exit_OnlyWindow=1 "当taglist是最后一个分割窗口时,自动推出vim
""是否一直处理tags.1:处理;0:不处理
""let Tlist_Process_File_Always=0 "不是一直实时更新tags,因为没有必要
""let Tlist_Inc_Winwidth=0
map <F3> :silent! Tlist<CR>

let g:winManagerWindowLayout='TagList|FileExplorer'
nmap wm :WMToggle<cr>

let g:miniBufExplMapWindowNavVim = 1

"xptemplate
let g:xptemplate_brace_complete=0
let g:xptemplate_key = '<Tab>'
let g:xptemplate_nav_prev = '<C-k>'

" -- MiniBufferExplorer -- "
let g:miniBufExplMapWindowNavArrows = 1
let g:miniBufExplMapCTabSwitchBufs = 1
let g:miniBufExplModSelTarget = 1
let g:miniBufExplMapWindowNavVim = 1 "<C-h,j,k,l>
"DoxygenToolkit
map fg : Dox<cr>
let g:DoxygenToolkit_authorName="owenzhang, owenzhang2010@qq.com"
let s:licenseTag = "Tencent.Co.Ltd\<enter>"
let s:licenseTag = s:licenseTag . "All right reserved\<enter>"
let g:DoxygenToolkit_licenseTag = s:licenseTag
let g:DoxygenToolkit_briefTag_funcName="yes"
let g:doxygen_enhanced_color=1

" --- OmniCppComplete ---

"set completeopt=menu
" -- ctags --
" " map <ctrl>+F12 to generate ctags for current folder:
" map <C-F12> :!ctags -R --c++-kinds=+p --fields=+iaS --extra=+q .<CR><CR>
"map <F12> :!ctags -R --c++-kinds=+p --fields=+iaS --extra=+q . ~/code/commlib32_proj/trunk/baselib/<CR><CR>
" " add current directory's generated tags file to available tags
"set tags+=./tags
"map <F12> :!ctags -R *<CR>
"map <F12> :!ctags -R . ~/code/commlib32_proj/trunk/baselib/ <CR>
"nnoremap ,t :sil !find -maxdepth 1 -name '*.[ch]' -print0 -o
" \ -name '*.cpp' -print0 \| xargs -0 ctags --c++-kinds=+p --fields=+iaS --extra=+q <cr>

""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
set cscopequickfix=s-,c-,d-,i-,t-,e-
if(has("win32") || has("win95") || has("win64") || has("win16"))
let g:iswindows=1
else
let g:iswindows=0
endif
autocmd BufEnter * lcd %:p:h


map <F12> :call Do_CsTag()<CR>
nmap <C-_>s :cs find s <C-R>=expand("<cword>")<CR><CR>:copen<CR>
nmap <C-_>g :cs find g <C-R>=expand("<cword>")<CR><CR>
nmap <C-_>c :cs find c <C-R>=expand("<cword>")<CR><CR>:copen<CR>
nmap <C-_>t :cs find t <C-R>=expand("<cword>")<CR><CR>:copen<CR>
nmap <C-_>e :cs find e <C-R>=expand("<cword>")<CR><CR>:copen<CR>
nmap <C-_>f :cs find f <C-R>=expand("<cfile>")<CR><CR>:copen<CR>
nmap <C-_>i :cs find i ^<C-R>=expand("<cfile>")<CR>$<CR>:copen<CR>
"nmap <C-_>i :cs find i <C-R>=expand("<cfile>")<CR><CR>:copen<CR>
nmap <C-_>d :cs find d <C-R>=expand("<cword>")<CR><CR>:copen<CR>
function Do_CsTag()
let dir = getcwd()
if filereadable("tags")
if(g:iswindows==1)
let tagsdeleted=delete(dir."\\"."tags")
else
let tagsdeleted=delete("./"."tags")
endif
if(tagsdeleted!=0)
echohl WarningMsg | echo "Fail to do tags! I cannot delete the tags" | echohl None
return
endif
endif
if has("cscope")
silent! execute "cs kill -1"
endif
if filereadable("cscope.files")
if(g:iswindows==1)
let csfilesdeleted=delete(dir."\\"."cscope.files")
else
let csfilesdeleted=delete("./"."cscope.files")
endif
if(csfilesdeleted!=0)
echohl WarningMsg | echo "Fail to do cscope! I cannot delete the cscope.files" | echohl None
return
endif
endif
if filereadable("cscope.out")
if(g:iswindows==1)
let csoutdeleted=delete(dir."\\"."cscope.out")
else
let csoutdeleted=delete("./"."cscope.out")
endif
if(csoutdeleted!=0)
echohl WarningMsg | echo "Fail to do cscope! I cannot delete the cscope.out" | echohl None
return
endif
endif
if(executable('ctags'))
"silent! execute "!ctags -R --c-types=+p --fields=+S *"
silent! execute "!ctags -R --c++-kinds=+p --fields=+iaS --extra=+q ."
endif
if(executable('cscope') && has("cscope") )
if(g:iswindows!=1)
silent! execute "!find . -name '*.h' -o -name '*.c' -o -name '*.cpp' -o -name '*.java' -o -name '*.cs' > cscope.files"
else
silent! execute "!dir /s/b *.c,*.cpp,*.h,*.java,*.cs >> cscope.files"
endif
silent! execute "!cscope -b"
execute "normal :"
if filereadable("cscope.out")
execute "cs add cscope.out"
endif
endif
endfunction

"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" cscope setting
" """""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
"if has("cscope")
" set csprg=/usr/bin/cscope
" set csto=1
" set cst
" set nocsverb
"" add any database in current directory
" if filereadable("cscope.out")
" cs add cscope.out
" endif
" set csverb
"endif

"nmap <C-@>s :cs find s <C-R>=expand("<cword>")<CR><CR>
"nmap <C-@>g :cs find g <C-R>=expand("<cword>")<CR><CR>
"nmap <C-@>c :cs find c <C-R>=expand("<cword>")<CR><CR>
"nmap <C-@>t :cs find t <C-R>=expand("<cword>")<CR><CR>
"nmap <C-@>e :cs find e <C-R>=expand("<cword>")<CR><CR>
"nmap <C-@>f :cs find f <C-R>=expand("<cfile>")<CR><CR>
"nmap <C-@>i :cs find i ^<C-R>=expand("<cfile>")<CR>$<CR>
"nmap <C-@>d :cs find d <C-R>=expand("<cword>")<CR><CR>


"if filereadable("feed.vim")
" source feed.vim
"endif
"set tags+=/data/home/owenzhang/feedcenter_proj/trunk/tags,/data/home/owenzhang/code/commlib32_proj/trunk/tags

":cs add /data/home/owenzhang/feedcenter_proj/trunk/cscope.out /data/home/owenzhang/feedcenter_proj/trunk/

"}}

func! CompileGcc()
exec "w"
let compilecmd="!gcc "
let compileflag="-o %< "
if search("mpi\.h") != 0
let compilecmd = "!mpicc "
endif
if search("glut\.h") != 0
let compileflag .= " -lglut -lGLU -lGL "
endif
if search("cv\.h") != 0
let compileflag .= " -lcv -lhighgui -lcvaux "
endif
if search("omp\.h") != 0
let compileflag .= " -fopenmp "
endif
if search("math\.h") != 0
let compileflag .= " -lm "
endif
exec compilecmd." % ".compileflag
endfunc
func! CompileGpp()
exec "w"
let compilecmd="!g++ "
let compileflag="-o %< "
if search("mpi\.h") != 0
let compilecmd = "!mpic++ "
endif
if search("glut\.h") != 0
let compileflag .= " -lglut -lGLU -lGL "
endif
if search("cv\.h") != 0
let compileflag .= " -lcv -lhighgui -lcvaux "
endif
if search("omp\.h") != 0
let compileflag .= " -fopenmp "
endif
if search("math\.h") != 0
let compileflag .= " -lm "
endif
exec compilecmd." % ".compileflag
endfunc
func! CompileCode()
exec "w"
if &filetype == "cpp"
exec "call CompileGpp()" elseif &filetype == "c"
exec "call CompileGcc()"
endif
endfunc
func! RunResult()
exec "w"
if search("mpi\.h") != 0
exec "!mpirun -np 4 ./%<"
elseif &filetype == "cpp"
exec "! ./%<"
elseif &filetype == "c"
exec "! ./%<"
elseif &filetype == "python"
exec "call RunPython"
elseif &filetype == "java"
exec "!java %<"
endif
endfunc
map <F5> :call CompileCode()<CR>
imap <F5> <ESC>:call CompileCode()<CR>
vmap <F5> <ESC>:call CompileCode()<CR>
map <F6> :call RunResult()<CR>


function! s:insert_gates()
let gatename = substitute(toupper(expand("%:t")), "\\.", "_", "g")
execute "normal! i#ifndef " . gatename
execute "normal! o#define " . gatename . " "
execute "normal! Go#endif /* " . gatename . " */"
normal! kk
endfunction
autocmd BufNewFile *.{h,hpp} call <SID>insert_gates()
CATALOG
  1. 1. 写在前面
  2. 2. VIM 配置
  3. 3. VIM tips
    1. 3.1. vim补全时发现候选菜单的上下选择ctrl-p, ctrl-n相反
    2. 3.2. 参考
    3. 3.3. 附:vimrc文件