EverET.org

好记性不如烂笔头

我的Vim配置

| Comments

我的Vim配置,参考了众多大牛的配置。这个配置我用了挺长的一段时间了,觉得用起来还算顺手。

此配置过的Vim非常适合用来写Python、shell和一般的配置文件。

最新的配置文件可以在https://github.com/cedricporter/vim-emacs-setting获取,我不定时会更新它。

配置效果如下图:

配置文件

vim
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
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
"""""""""""""""""""""""""""""""""""""""""""""""""""
" Vim setting by Hua Liang [Stupid ET [Cedric Porter]]
"    email:   et@everet.org
"    website: http://EverET.org
"
" How to upgrade:
" Git:
"    https://github.com/cedricporter/vim-emacs-setting
" How to install:
"    copy the file you pull from git to the HOME folder
"

"tags
set tags=~/.vim/tagfiles/stl_tags
set tags+=~/.vim/tagfiles/systags
set tags+=~/.vim/tagfiles/opengl_tags
set tags+=./tags,./../tags,./**/tags

" build tags of your own project with CTRL+F12
map   :w:!ctags -R --c++-kinds=+p --fields=+iaS --extra=+q .

" Vimgdb
:run macros/gdb_mappings.vim

"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" => General
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" Platform Test
function! MySys()
  if has("win32")
    return "windows"
  else
    return "linux"
  endif
endfunction

"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
set nocompatible "不要vim模仿vi模式,建议设置,否则会有很多不兼容的问题

" Sets how many lines of history VIM has to remember
set history=700

" Enable filetype plugin
filetype plugin on
filetype indent on

" Set to auto read when a file is changed from the outside
set autoread

" With a map leader it's possible to do extra key combinations
" like w saves the current file
let mapleader = ","
let g:mapleader = ","

" Fast saving
nmap w :w!
" Fast make
nmap m :w:make

"Fast reloading of the .vimrc
map  ss :source $HOME/.vimrc
"Fast editing of .vimrc
map  ee :e $HOME/.vimrc
"When .vimrc is edited, reload it
"autocmd! bufwritepost .vimrc source ~/.vimrc

"Language Setting
set langmenu=zh_CN.UTF-8               "设置菜单语言
language messages zh_CN.utf-8          "设置提示信息语言

"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" => VIM user interface
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" Set 5 lines to the curors - when moving vertical..
set so=5
set wildmenu "Turn on Wild menu
set ruler "Always show current position
set cmdheight=2 "The commandbar height
set hid "Change buffer - without saving

" Set backspace config
set backspace=eol,start,indent
set whichwrap+=,h,l

set ignorecase "Ignore case when searching
set smartcase

set hlsearch "Highlight search things
set incsearch "Make search act like search in modern browsers
set nolazyredraw "Don't redraw while executing macros
set magic "Set magic on, for regular expressions
set showmatch "Show matching bracets when text indicator is over them
set mat=2 "How many tenths of a second to blink
set showcmd    "show (partial) command when not complete

" No sound on errors
set noerrorbells
set novisualbell
set t_vb=
set tm=500

"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" => Colors and Fonts
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
syntax enable "Enable syntax hl

" set position and the size of the window
winpos 130 20
set lines=37 columns=150

" Set font according to system
if MySys() == "mac"
  set gfn=Menlo:h14
  set shell=/bin/bash
elseif MySys() == "windows"
  set gfn=Bitstream\ Vera\ Sans\ Mono:h10
elseif MySys() == "linux"
  "set gfn=Monospace\ 10
  set gfn=Ubuntu\ Mono\ 12
  set shell=/bin/bash
endif

if has("gui_running")
  set guioptions-=T
  set t_Co=256
  set background=dark
  colorscheme peaksea
  set nu        "开启行号显示
else
  colorscheme zellner
  set background=dark
  set nonu
endif

set encoding=utf8
try
    lang en_US
catch
endtry

let &termencoding=&encoding
" 解决打开Windows下编辑的txt乱码问题
" 通过添加gb18030编码等来解决
set fileencodings=utf-8,gbk,ucs-bom,cp936
set fenc=utf-8 " default fileencoding
set fencs=utf-8,gb18030,gbk,gb2312,cp936,ucs-bom,euc-jp
set ffs=unix,dos,mac "Default file types

"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" => Text, tab and indent related
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
set expandtab
set shiftwidth=4
set tabstop=4
set smarttab
"set softtabstop=4

set lbr     "Line break
set tw=500  "Textwidth

set ai "Auto indent
set si "Smart indet
set wrap "Wrap lines

"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" => Moving around, tabs and buffers
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" Map space to / (search) and c-space to ? (backgwards search)
"map  /
"map  ?
map   :noh "Cancel the Highlight of the matching

" Smart way to move btw. windows
map  j
map  k
map  h
map  l

" Close the current buffer
map bd :Bclose

" Close all the buffers
map ba :1,300 bd!

" Use the arrows to something usefull
map  :bn
map  :bp

" Tab configuration
map tn :tabnew
map te :tabedit
map tc :tabclose
map tm :tabmove

" When pressing cd switch to the directory of the open buffer
map cd :cd %:p:h

""""""""""""""""""""""""""""""
" => Statusline
""""""""""""""""""""""""""""""
" Always hide the statusline
set laststatus=2

" Format the statusline
set statusline=\ %{HasPaste()}%t%m%r%h\ %w\ \ CWD:\ %r%{CurDir()}%h\ \ \ Line:\ %l/%L:%c\ %=[%{GitBranch()}]
"set statusline=\ %{HasPaste()}%F%m%r%h\ %w\ \ CWD:\ %r%{CurDir()}%h\ \ [%{(&fenc==\"\")?&enc:&fenc}%{(&bomb?\",BOM\":\"\")}]\ [ASCII=\%03.3b]\ [HEX=\%02.2B]\ [POS=%04l,%04v][%p%%]\ [LEN=%L]\ %=[%{GitBranch()}]“

function! CurDir()
    let curdir = substitute(getcwd(), '/Users/amir/', "~/", "g")
    return curdir
endfunction

function! HasPaste()
    if &paste
        return 'PASTE MODE  '
    else
        return ''
    endif
endfunction

"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" => Parenthesis/bracket expanding
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
vnoremap $1 `>a)`
vnoremap $2 `>a]`
vnoremap $3 `>a}`
vnoremap $$ `>a"`
vnoremap $q `>a'`
vnoremap $e `>a"`

" Map auto complete of (, ", ', [
inoremap $1 ()i
inoremap $2 []i
inoremap $3 {}i
inoremap $4 {o}O
inoremap $q ''i
inoremap $e ""i
inoremap $t <>i

"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" => General Abbrevs
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
iab xdate =strftime("%d/%m/%y %H:%M:%S")

"-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
"common setting
autocmd BufEnter * lcd %:p:h

" 缩进
if has("autocmd")
    filetype plugin indent on "根据文件进行缩进
    augroup vimrcEx
        au!
        autocmd FileType text setlocal textwidth=78
        autocmd BufReadPost *
                    \ if line("'\"") > 1 && line("'\"")  Git Vim configuration
"“”"”"”"”"”"”"”"”"”"”"”"”"”"”"”"
"gd :GitDiff
"gD :GitDiff —cached
"gs :GitStatus
"gl :GitLog
"ga :GitAdd
"gA :GitAdd 
"gc :GitCommit
"In the git-status buffer:
"    :GitAdd 

"--------------------------------------------------
"neocomplcache 自动补全
" Disable AutoComplPop.
"let g:acp_enableAtStartup = 0
" Use neocomplcache.
let g:neocomplcache_enable_at_startup = 1
" Use smartcase.
let g:neocomplcache_enable_smart_case = 1
" Use camel case completion.
let g:neocomplcache_enable_camel_case_completion = 1
" Use underbar completion.
let g:neocomplcache_enable_underbar_completion = 1
" Set minimum syntax keyword length.
let g:neocomplcache_min_syntax_length = 3
let g:neocomplcache_lock_buffer_name_pattern = '\*ku\*'
" Auto select the first one.
let g:neocomplcache_enable_auto_select=1

"WinManager
"    显示帮助
"    如果光标下是目录, 则进入该目录; 如果光标下文件, 则打开该文件
"-   返回上级目录
"c   切换vim 当前工作目录正在浏览的目录
"d   创建目录
"D   删除目录或文件
"i   切换显示方式
"R   文件或目录重命名
"s   选择排序方式
"x   定制浏览方式, 使用你指定的程序打开该文件
let g:winManagerWindowLayout='FileExplorer|TagList'
nmap wm :WMToggle

"Cscope
"$ cscope -Rbq
":cs add /home/wooin/vim71/cscope.out /home/wooin/vim71
"cs find c Print
":cw 打开Quickix窗口
"--------------------------------------------------
":make
":cw
":cn        // 切换到下一个结果
":cp        // 切换到上一个结果
"如果你经常使用这两个命令, 你还可以给他们设定快捷键, 比如在~/.vimrc中增加:
"nmap  :cn
"nmap  :cp
"-------------------------------------------------
"cscope的主要功能是通过同的子命令"find"来实现的
"cscope find"的用法:
"cs find c|d|e|f|g|i|s|t name
"0 或 s 查找本 C 符号(可以跳过注释)
"1 或 g 查找本定义
"2 或 d 查找本函数调用的函数
"3 或 c 查找调用本函数的函数
"4 或 t 查找本字符串
"6 或 e 查找本 egrep 模式
"7 或 f 查找本文件
"8 或 i 查找包含本文件的文件
set cscopequickfix=s-,c-,d-,i-,t-,e-
map  :call Do_CsTag()
nmap s :cs find s =expand(""):copen
nmap g :cs find g =expand("")
nmap c :cs find c =expand(""):copen
nmap t :cs find t =expand(""):copen
nmap e :cs find e =expand(""):copen
nmap f :cs find f =expand(""):copen
nmap i :cs find i ^=expand("")$:copen
nmap d :cs find d =expand(""):copen
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

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

"MiniBufExplorer
"        向前循环切换到每个buffer名上
"    向后循环切换到每个buffer名上
"    在打开光标所在的buffer
"d       删除光标所在的buffer
"--------------------------------------------------
"    向前循环切换到每个buffer上,并在但前窗口打开
"    向后循环切换到每个buffer上,并在但前窗口打开
let g:miniBufExplMapCTabSwitchBufs=1
"--------------------------------------------------
"可以用切换到上下左右的窗口中去,就像:
"C-w,h j k l    向"左,下,上,右"切换窗口.
"let g:miniBufExplMapWindowNavVim = 1
"--------------------------------------------------
"用切换到上下左右窗口中去
"let g:miniBufExplMapWindowNavArrows = 1
"--------------------------------------------------
"防止打开很多个
let g:miniBufExplorerMoreThanOne=0

"c/h文件间相互切换 -- 插件: A
":A  在新Buffer中切换到c/h文件
":AS 横向分割窗口并打开c/h文件
":AV 纵向分割窗口并打开c/h文件
":AT 新建一个标签页并打开c/h文件
"nnoremap   :A

"Grep
"--------------------------------------------------
":Grep   按照指定的规则在指定的文件中查找
":Rgrep  同上, 但是是递归的grep
":GrepBuffer 在所有打开的缓冲区中查找
":Bgrep  同上
":GrepArgs   在vim的argument filenames (:args)中查找
":Fgrep  运行fgrep
":Rfgrep 运行递归的fgrep
":Egrep  运行egrep
":Regrep 运行递归的egrep
":Agrep  运行agrep
":Ragrep 运行递归的agrep
"For example, the following map invokes the :Grep command to search for the keyword under the cursor:
nnoremap   :Grep

"高亮的书签 -- 插件: VisualMark
"如果是gvim, 直接在代码上按下Ctrl+F2, 如果是vim, 用"mm"
"如果你设置了多个书签, 你可以用F2键正向在期间切换, 用Shift+F2反向在期间切换.

"自动补全
":help new-omni-completion
"按下"Ctrl+X Ctrl+O", 此时会弹出一个下列菜单, 显示所有匹配的标签
"--------------------------------------------------
"Ctrl+P      向前切换成员
"Ctrl+N      向后切换成员
"Ctrl+E      表示退出下拉窗口, 并退回到原来录入的文字
"Ctrl+Y      表示退出下拉窗口, 并接受当前选项
"Ctrl+X Ctrl+L   整行补全
"Ctrl+X Ctrl+N   根据当前文件里关键字补全
"Ctrl+X Ctrl+K   根据字典补全
"Ctrl+X Ctrl+T   根据同义词字典补全
"Ctrl+X Ctrl+I   根据头文件内关键字补全
"Ctrl+X Ctrl+]   根据标签补全
"Ctrl+X Ctrl+F   补全文件名
"Ctrl+X Ctrl+D   补全宏定义
"Ctrl+X Ctrl+V   补全vim命令
"Ctrl+X Ctrl+U   用户自定义补全方式
"Ctrl+X Ctrl+S   拼写建议
" OmniCppComplete
let OmniCpp_NamespaceSearch = 1
let OmniCpp_GlobalScopeSearch = 1
let OmniCpp_ShowAccess = 1
let OmniCpp_ShowPrototypeInAbbr = 1 " show function parameters
let OmniCpp_MayCompleteDot = 1 " autocomplete after .
let OmniCpp_MayCompleteArrow = 1 " autocomplete after ->
let OmniCpp_MayCompleteScope = 1 " autocomplete after ::
let OmniCpp_DefaultNamespaces = ["std", "_GLIBCXX_STD"]
" automatically open and close the popup menu / preview window
"au CursorMovedI,InsertLeave * if pumvisible() == 0|silent! pclose|endif
set completeopt=menuone,menu,longest    ",preview

"加速你的补全 -- 插件: SuperTab
":SuperTabHelp
"--------------------------------------------------
" 设置按下后默认的补全方式, 默认是,
" 现在改为. 关于的补全方式,
" 还有其他的补全方式, 你可以看看下面的一些帮助:
" :help ins-completion
" :help compl-omni
"let g:SuperTabDefaultCompletionType=""
"--------------------------------------------------
" 0 - 不记录上次的补全方式
" 1 - 记住上次的补全方式,直到用其他的补全命令改变它
" 2 - 记住上次的补全方式,直到按ESC退出插入模式为止
"let g:SuperTabRetainCompletionType=2

"Doxygen注释
"To customize the output of the script, see the g:DoxygenToolkit_*
"variables in the script's source.  These variables can be set in your
".vimrc.
"--------------------------------------------------
"Use:
"- Type of comments (C/C++: /// or /** ... */, Python: ## and # ) :
"  In vim, default C++ comments are : /** ... */. But if you prefer to use ///
"  Doxygen comments just add 'let g:DoxygenToolkit_commentType = "C++"'
"  (without quotes) in your .vimrc file
"
"- License :
"  In vim, place the cursor on the line that will follow doxygen license
"  comment.  Then, execute the command :DoxLic.  This will generate license
"  comment and leave the cursor on the line just after.
"
"- Author :
"  In vim, place the cursor on the line that will follow doxygen author
"  comment.  Then, execute the command :DoxAuthor.  This will generate the
"  skeleton and leave the cursor just after @author tag if no variable
"  define it, or just after the skeleton.
"
"- Function / class comment :
"  In vim, place the cursor on the line of the function header (or returned
"  value of the function) or the class.  Then execute the command :Dox.  This
"  will generate the skeleton and leave the cursor after the @brief tag.
"
"- Ignore code fragment (C/C++ only) :
"  In vim, if you want to ignore all code fragment placed in a block such as :
"    #ifdef DEBUG
"    ...
"    #endif
"  You only have to execute the command :DoxUndoc(DEBUG) !
"
"- Group :
"  In vim, execute the command :DoxBlock to insert a doxygen block on the
"  following line.
map fg : Dox
let g:DoxygenToolkit_authorName="Cedric Porter"
let g:DoxygenToolkit_licenseTag="GPL v4 \"
let g:DoxygenToolkit_undocTag="DOXIGEN_SKIP_BLOCK"
let g:DoxygenToolkit_briefTag_pre = "@brief\t"
let g:DoxygenToolkit_paramTag_pre = "@param\t"
let g:DoxygenToolkit_returnTag = "@return\t"
let g:DoxygenToolkit_briefTag_funcName = "no"
let g:DoxygenToolkit_maxFunctionProtoLines = 30

"对NERD_commenter的设置
"在光标所在行上,按下一次ctrl+h是注释,再按下一次是取消注释。
"而其内建的指令\cm是多行注释,类似C++的/**/,\cu是取消注释。
"let NERDShutUp=1

map  :call StartPython()
function StartPython()
    :w
    :!python %:t
endfunction

"NERD_tree 提供展示文件/目录列表的功能,比自带的文件浏览器要好很多
" 让Tree把自己给装饰得多姿多彩漂亮点
let NERDChristmasTree=1
" 控制当光标移动超过一定距离时,是否自动将焦点调整到屏中心
let NERDTreeAutoCenter=1
let NERDTreeShowHidden=1
map  : NERDTreeToggle
map  : NERDTreeClose
本文链接: http://everet.org/vim.html

您可能也喜欢

Comments