我的vim配置

我用vimrc,其中配置了函数列表,目录列表,函数跳转,定义跳转,xml排版,自动关联cscope和tags,快捷注释。

其中需要下载一些插件才能用,比如我用的fedora,需要下载的列表为

1
2
3
4
5
6
7
8
9
vim-X11.x86_64
vim-common.x86_64
vim-enhanced.x86_64
vim-filesystem.x86_64
vim-halibut.noarch
vim-minimal.x86_64
vim-perl-support.noarch
cscope.x86_64
ctags.x86_64

之后配置$HOME目录下的.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
if has("cscope")
set cscopequickfix=s-,c-,d-,i-,t-,e-
set csto=0
set cst
set csverb
endif

"------------------------------------------------------------------------------
"获取当前路径的上一级的路径
function! GET_UP_PATH(dir)
let pos=strlen(a:dir)-1
while pos>0
if (a:dir[pos]=="/" )
return strpart(a:dir,0,pos)
endif
let pos=pos-1
endwhile
return ""
endfunction

"设置相关tags
function! s:SET_TAGS()
let dir = getcwd() "获得源文件路径
"在路径上递归向上查找tags文件
while dir!=""
if findfile("tags",dir ) !=""
"找到了就加入到tags
exec "set tags+=".dir."/tags"
return
endif
"得到上级路径
let dir=GET_UP_PATH(dir)
endwhile
endfunction

"设置php cscope.out
function! SET_CSPHP()
if(executable('cscope') && has("cscope") )
silent! execute "!find . -name '*.php' -o -name '*.inc' -o -name '*.module' > cscope.files"
silent! execute "!cscope -bkq -i cscope.files"
if filereadable("cscope.out")
silent! execute "cs add cscope.out"
endif
silent! execute "!ctags -R --langmap=php:.engine.inc.module.theme.php --php-kinds=cdf --languages=php "
endif
endfunction

"设置c cscope.out
function! SET_CSAD()
if(executable('cscope') && has("cscope") )
silent! execute "!find . -name '*.h' -o -name '*.c' -o -name '*.cpp' > cscope.files"
silent! execute "!cscope -bkq -i cscope.files"
if filereadable("cscope.out")
silent! execute "cs add cscope.out"
endif
silent! execute "!ctags -R --c-kinds=+p --c++-kinds=+p --fields=+iaS --extra=+q ."
endif
endfunction

function! s:SET_CSC()
let dir = expand("%:p:h") "获得源文件路径
"在路径上递归向上查找tags文件
while dir!=""
if findfile("cscope.out",dir ) !=""
"找到了就加入到tags
if 0 == cscope_connection(1, "cscope.out", dir)
execute "cs add ".dir."/cscope.out ".dir
endif
return
endif
"得到上级路径
let dir=GET_UP_PATH(dir)
endwhile
endfunction

autocmd BufEnter * call s:SET_CSC()
autocmd BufEnter * call s:SET_TAGS()
"------------------------------------------------------------------------------
set laststatus=2 " 显示状态栏 (默认值为 1, 无法显示状态栏)

nmap <S-L> :bnext<CR>
nmap <S-H> :bprevious<CR>
nmap <C-s> :w!<CR>

nmap <leader>1 :set filetype=xhtml<CR>
nmap <leader>2 :set filetype=css<CR>
nmap <leader>3 :set filetype=javascript<CR>
nmap <leader>4 :set filetype=php<CR>
nmap <leader>5 :set filetype=default<CR>

nmap <silent> <leader>hl <Plug>MarkSet
nmap <silent> <leader>hh <Plug>MarkClear
nmap <silent> <leader>hr <Plug>MarkRegex
vmap <silent> <leader>hl <Plug>MarkSet
vmap <silent> <leader>hh <Plug>MarkClear
vmap <silent> <leader>hr <Plug>MarkRegex


let g:miniBufExplMapWindowNavVim = 1
let g:miniBufExplMapWindowNavArrows = 1
let g:miniBufExplMapCTabSwitchBufs = 1
let g:miniBufExplModSelTarget = 1
let g:miniBufExplMoreThanOne=0

set mouse=a

set noswf

let g:winManagerWindowLayout="FileExplorer|TagList"

let Tlist_File_Fold_Auto_Close=1

"当文件在外部被修改,自动更新该文件
set autoread

"设置历史记录步数
set history=400

"快速退出(保存)为,<F12>
map <F9> :set mouse=v<cr>
map <F8> :set mouse=a<cr>

"开启语法
syntax enable
syntax on

"头文件与实现文件转换
map <F12> :A <cr>

"设置字体

"设置配色
colorscheme candycode

set completeopt=menu

set t_Co=256

"高亮显示当前行
set cursorline
hi cursorline guibg=#222222
hi CursorColumn guibg=#333333

"tag
set tags=tags;

"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" => 缩进
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
"自动缩进
set ai

"智能缩进
set si

"设置缩进的空格数为4
set autoindent

"设置自动缩进:即每行的缩进值与上一行相等;使用 noautoindent 取消设置
set shiftwidth=4
set tabstop=4
set softtabstop=4

"设置使用 C/C++ 语言的自动缩进方式
set cin
set cindent

"显示标尺
set ruler

"设置命令行的高度
set cmdheight=1

"显示行数
set number

"显示匹配的括号([{和}])
set showmatch

"高亮显示搜索的内容
set hlsearch

"taglist
let Tlist_Show_One_File=1
let Tlist_Exit_OnlyWindow=1

""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" => 编码设置
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""

"Vim 内部使用的字符编码方式,包括 Vim 的 buffer (缓冲区)、菜单文本、
"消息文本等。用户手册上建议只在 .vimrc 中改变它的值,
"事实上似乎也只有在 .vimrc 中改变它的值才有意义。
set encoding=utf-8

"Vim 启动时会按照它所列出的字符编码方式逐一探测即将打开的文件的
"字符编码方式,并且将 fileencoding 设置为最终探测到的字符编码方式。
"因此最好将 Unicode 编码方式放到这个列表的最前面。
"set fileencodings=Unicode,utf-8,gb2312,gbk,gb18030,latin-1
"set fencs=utf-8,Unicode,gb2312,gbk,gb18030,latin-1,cp936
set fencs=utf-8,ucs-bom,shift-jis,gb18030,gbk,gb2312,cp936,latin-1,prc
"set fileencodings=ucs-bom,utf8,prc

"Vim 中当前编辑的文件的字符编码方式,
"Vim 保存文件时也会将文件保存为这种字符编码方式。
set fileencoding=utf-8
"set fileencoding=prc

" termencoding: Vim 所工作的终端 (或者 Windows 的 Console 窗口)
" 的字符编码方式。这个选项对 GUI 模式的 gVim 无效,
" 而对 Console 模式的 Vim 而言就是Windows 控制台的代码页
" (对于 Windows 而言),并且通常我们不需要改变它。
" 下面这句只影响普通模式 (非图形界面) 下的 Vim。
"set termencoding=prc
let &termencoding=&encoding

" "代码折叠, 命令 za
set foldmethod=syntax
set foldlevel=100 "启动vim时不要自动折叠代码

" "自动换行
if (has("gui_running")) "图形界面下的设置
" "
" "指定不折行。如果一行太长,超过屏幕宽度,则向右边延伸到屏幕外面
set nowrap
"
"添加水平滚动条。如果你指定了不折行,那为窗口添加一个水平滚动条就非常有必要
" 了
set guioptions+=b
"
else
"字符界面下的设置
set nowrap
endif


" "<F2>code_complete.vim插件:函数自动完成
map <F2> :silent %!xmllint -format %<CR>
if !exists("g:completekey")
let g:completekey = "<F2>" "hotkey
endif

map <C-1> gu
map <C-2> gU

map <F3> :cp <cr>
map <F4> :cn <cr>

" "当前目录生成tags语法文件,用于自动完成,函数提示:code_complete.vim
"OmniCppComplete.vim ...
"map <F5> :call SET_CSAD() <CR>
"
" "函数和变量列表
map <F6> :TlistToggle<CR>
"
" "文件浏览器
"map <F7> :WMToggle<CR>
"
" "文件树状列表
nmap <silent> <F7> :execute 'NERDTreeToggle '. expand("%:p:h")<CR>
"
" "映射复制、粘贴、剪贴ctrl+c ctrl+v ctrl+x
" map <C-V> "+pa<Esc>
" map! <C-V> <Esc>"+pa
map <C-C> "+y
map <C-X> "+x
"
" " 映射全选 ctrl+a
map <C-A> ggVG
map! <C-A> <Esc>ggVG
"
" " 多行缩进
map <Tab> >
map <S-Tab> <

"设置自动补齐
"filetype plugin indent on
"set completeopt=longest,menu


"if has("autocmd") && exists("+omnifunc")
" autocmd Filetype *
" \ if &omnifunc == "" |
" \ setlocal omnifunc=syntaxcomplete#Complete |
" \ endif
"endif

"let g:SuperTabDefaultCompletionType="<C-X><C-]>"

"let g:SuperTabRetainCompletionType=0
" 0 - 不记录上次的补全方式
" 1 - 记住上次的补全方式,直到用其他的补全命令改变它
" 2 - 记住上次的补全方式,直到按ESC退出插入模式为止


map <C-E> :call TitleDet()'s <cr>

map <C-F5> :call SET_CSAD()'s <cr>
map <C-F6> :call SET_CSPHP()'s <cr>

function AddTitle()
call append(0,"/*******************************************************************************")
"call append(1,"#")
call append(1," * Author : RKhuwq")
"call append(3,"#")
call append(2," * Email : huwq@neusoft.com")
"call append(5,"#")
call append(3," * Last modified : ".strftime("%Y-%m-%d %H:%M"))
"call append(7,"#")
call append(4," * Filename : ".expand("%"))
"call append(9,"#")
call append(5," * Description : ")
"call append(11,"#")
call append(6," * *****************************************************************************/")
echohl WarningMsg | echo "Successful in adding the copyright." | echohl None
endfunction

"更新最近修改时间和文件名
function UpdateTitle()
normal m'
execute ' * Last modified : /s@:.*$@\=strftime(":\t%Y-%m-%d %H:%M")@'
normal "
normal mk
execute ' * Filename : /s@:.*$@\=":\t\t".expand("%:t")@'
execute "noh"
normal 'k
echohl WarningMsg | echo "Successful in updating the copy right."| echohl None
endfunction

autocmd BufEnter :/ * Last modified : /s@:.*$@\=strftime(" : %Y-%m-%d %H:%M")@

"判断第四行行代码里面,是否有Last modified这个单词,
"如果没有的话,代表没有添加过作者信息,需要新添加;
"如果有的话,那么只需要更新即可
function TitleDet()
let line = getline(4)
if line =~ '\s\*\sLast\smodified\s:\s\S*'
:/ * Last modified : /s@:.*$@\=strftime(": %Y-%m-%d %H:%M")@
return
endif
call AddTitle()
endfunction



"自动补全
set completeopt=menu,menuone
let OmniCpp_MayCompleteDot = 1 " autocomplete with .
let OmniCpp_MayCompleteArrow = 1 " autocomplete with ->
let OmniCpp_MayCompleteScope = 1 " autocomplete with ::
let OmniCpp_SelectFirstItem = 2 " select first item (but don't insert)
let OmniCpp_NamespaceSearch = 2 " search namespaces in this and included files
let OmniCpp_ShowPrototypeInAbbr = 1 " show function prototype in popup window
let OmniCpp_GlobalScopeSearch=1
let OmniCpp_DisplayMode=1
let OmniCpp_DefaultNamespaces=["std"]


" ~/.vimrc (configuration file for vim only)
" skeletons
"autocmd BufNewFile *.spec call SKEL_spec()
" filetypes
filetype plugin on
filetype indent on
" ~/.vimrc ends here

其中F2为xml排版,F3为cscope搜索上一条,F4下一条,F6当前文件函数列表,F7目录列表,F8启用鼠标,F9不使用鼠标,F12 C语言中头文件与代码文件转换

Ctrl+E注释,Ctrl+F5生成C语言ctags与cscope,Ctrl+F6生成php语言ctags与cscope

之后下载本文的配置。复制到$HOME目录的.vim目录中。

下载地址

文章目录