刘毅同学

About Python, MySQL & Life

我的vimrc——vim配置的一些建议(附详细注释的vimrc)

| Comments

最近仔细的研究了一下传说中的史上最强的vimrc,收获不少,其中有许多亮点,在这里分享下我觉得非常不错的地方。我的vimrc主要是汇集吸收了多个网上流传的vimrc以及加入了我的自定义的部分

  • 绑定CTRL-s,CTRL-v,CTRL-c,CTRL-v一些其他编辑器中常用的快捷键

  • 绑定键来翻页(因为使用j k 光标总是左右动,绑定功能键来翻页则不需要同时按CTRL-E了,更轻松)

  • 绑定CTRL-[hjkl]来在窗口间移动

  • 绑定左右箭头(<—|—>)在buffer间移动,适合多文件的编辑

  • 绑定t[nef]实现方便的进行标签业的操作(新建,移动等等)

  • 绑定ALT+[jk]实现行的上移下移

  • 独特的括号匹配,使用$[123$]自动生成一对括号

  • 自动载入每个项目的配置——易水博客

还有一些插件的配置,这部分我还没有仔细的研究,对于minibuffer的配置,我个人目前觉得默认就已经能满足我的个人需求了,所以注释掉了

我的vimrc编写经验:
对于vimrc的定制,最好是从个人的日常使用中总结,留心一下使用哪些组合按键使用的频率最多或者哪些操作最多,然后将这些组合按键或者操作加以绑定更简洁的快捷键,极为频繁的就用F[1-12]绑定。

下面是我的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
set nocompatible
""""""""""界面""""""""""
colorscheme my-molokai
"set guifont=Monospace\ 11
set tabstop=4
set backspace=2
set nu! "显示行号
set nowrap
set linebreak "整词换行
set whichwrap=b,s,<,>,[,] "左右光标移动到头时可以自动下移
"set autochdir "自动设置目录为正在编辑的文件所在目录
set hidden "没有保存的缓冲区可以自动隐藏
set scrolloff=7 "设置光标上下保留的最小的行数
set smartindent   "智能对齐方式
set shiftwidth=4 "换行时行间交错使用4个空格
set autoindent    "自动对齐
set ai!  "设置自动缩进
set showcmd       " display incomplete commands
set wildmenu "增强模式的命令行
"=========状态栏相关===========
set laststatus=2 "总是显示状态栏status line
set ruler "在编辑过程中,右下角显示光表位置的状态行
set statusline=[%F]%y%r%m%*%=[Line:%l/%L,Column:%c][%p%%]
"==========编辑相关============
" allow backspacing over everything in insert mode
set backspace=indent,eol,start
" Don't use Ex mode, use Q for formatting
"map Q gq
" In many terminal emulators the mouse works just fine, thus enable it.
if has('mouse')
  set mouse=v
endif
set autowrite "在切换buffer时自动保存当前的文件
set autoread
"===========查找替换相关============
set hlsearch
set incsearch "" 查询时非常方便,如要查找book单词,当输入到/b时,会自动找到
               " 第一个b开头的单词,当输入到/bo时,会自动找到第一个bo开头的
               " 单词,依次类推,进行查找时,使用此设置会快速找到答案,当你
               " 找要匹配的单词时,别忘记回车
set gdefault     "替换所有的行内匹配都被替换,而不是第一个
"=========编程相关===========
"set completeopt=longest,menu   "关掉只能补全的时的预览窗口
filetype plugin indent on
syntax on
syntax enable
"========================================"
if has("vms")
  set nobackup       " do not keep a backup file, use versions instead
else
  set backup     " keep a backup file
endif
" Put these in an autocmd group, so that we can delete them easily.
"===================autocmd====================="
augroup vimrcEx
au!
"如果文件类型为text,则设置文本显示的宽度为78,更多参考:h setlocal
autocmd FileType text setlocal textwidth=78
" When editing a file, always jump to the last known cursor position.
" Don't do it when the position is invalid or when inside an event handler
" (happens when dropping a file on gvim).
" Also don't do it when the mark is in the first line, that is the default
" position when opening a file.
" 如下的autocmd实现打开文件自动跳到上次编辑的位置
autocmd BufReadPost *
  \ if line("'\"") > 1 && line("'\"") <= line("$") |
  \   exe "normal! g`\"" |
  \ endif
autocmd! bufwritepost vimrc source ~/.vimrc
augroup END
" Convenient command to see the difference between the current buffer and the
" file it was loaded from, thus the changes you made.
" Only define it when not defined already.
if !exists(":DiffOrig")
  command DiffOrig vert new | set bt=nofile | r # | 0d_ | diffthis
       \ | wincmd p | diffthis
endif
"==========自定义的键映射=================="
"实现CTRL-S保存操作
nmap <c-s> :w<CR>
imap <c-s> <Esc>:w<CR>a
"实现了CTRL-C、CTRL-V复制粘贴操作的映射
vnoremap <c-c> "+y
inoremap <c-v> <esc>"+p<CR>i
map <F2> <c-e>   "使用F2上翻页
map <F3> <c-y>   "使用F3下翻页
map <silent> <F12> :nohlsearch<CR>
let mapleader = ","
let g:mapleader = ","
"使用CTRL+[hjkl]在窗口间导航
"map <C-c> <C-W>c
map <C-j> <C-W>j
map <C-k> <C-W>k
map <C-h> <C-W>h
map <C-l> <C-W>l
"map <C-c> <C-W>c
"使用箭头导航buffer
map <right> :bn<cr>
map <left> :bp<cr>
" Tab configuration
map <leader>tn :tabnew<cr>
map <leader>te :tabedit
map <leader>tc :tabclose<cr>
map <leader>tm :tabmove
"括号匹配
vnoremap $1 <esc>`>a)<esc>`<i(<esc>
vnoremap $2 <esc>`>a]<esc>`<i[<esc>
vnoremap $3 <esc>`>a}<esc>`<i{<esc>
vnoremap $$ <esc>`>a"<esc>`<i"<esc>
vnoremap $q <esc>`>a'<esc>`<i'<esc>
vnoremap $e <esc>`>a"<esc>`<i"<esc>
"非常好用的括号匹配,实际是自动生成括号
"实现便利和兼容的折中
inoremap $1 ()<esc>i
inoremap $2 []<esc>i
inoremap $3 {}<esc>i
inoremap $4 {<esc>o}<esc>O
inoremap $q ''<esc>i
inoremap $e ""<esc>i
inoremap $t <><esc>i
"使用ALT+[jk]来移动行内容
nmap <M-j> mz:m+<cr>`z
nmap <M-k> mz:m-2<cr>`z
vmap <M-j> :m'>+<cr>`<my`>mzgv`yo`z
vmap <M-k> :m'<-2<cr>`>my`<mzgv`yo`z
"========================================
" CTRL-U in insert mode deletes a lot.  Use CTRL-G u to first break undo,
" so that you can undo CTRL-U after inserting a line break.
inoremap <C-U> <C-G>u<C-U>
"========================================"
"========自动载入项目的配置文件=========="
if filereadable("workplace.vim")
 source workplace.vim
endif
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" cscope setting
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
"自动判断是否存在cscope并配置
if has("cscope")
 set csprg=/usr/local/bin/cscope
 set csto=0
 set cst
 set nocsverb
 " add any database in current directory
 if filereadable("cscope.out")
     cs add cscope.out
 " else add database pointed to by environment
 elseif $CSCOPE_DB != ""
     cs add $CSCOPE_DB
 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>
" Using 'CTRL-spacebar' then a search type makes the vim window
" split horizontally, with search result displayed in
" the new window.
"nmap <C-M>s :scs find s <C-R>=expand("<cword>")<CR><CR>
"nmap <C-M>g :scs find g <C-R>=expand("<cword>")<CR><CR>
"nmap <C-M>c :scs find c <C-R>=expand("<cword>")<CR><CR>
"nmap <C-M>t :scs find t <C-R>=expand("<cword>")<CR><CR>
"nmap <C-M>e :scs find e <C-R>=expand("<cword>")<CR><CR>
"nmap <C-M>f :scs find f <C-R>=expand("<cfile>")<CR><CR>
"nmap <C-M>i :scs find i ^<C-R>=expand("<cfile>")<CR>$<CR>
"nmap <C-M>d :scs find d <C-R>=expand("<cword>")<CR><CR>
"
"" Hitting CTRL-space *twice* before the search type does a vertical
"" split instead of a horizontal one
"
"nmap <C-M><C-M>s
"    \:vert scs find s <C-R>=expand("<cword>")<CR><CR>
"nmap <C-M><C-M>g
"    \:vert scs find g <C-R>=expand("<cword>")<CR><CR>
"nmap <C-M><C-M>c
"    \:vert scs find c <C-R>=expand("<cword>")<CR><CR>
"nmap <C-M><C-M>t
"    \:vert scs find t <C-R>=expand("<cword>")<CR><CR>
"nmap <C-M><C-M>e
"    \:vert scs find e <C-R>=expand("<cword>")<CR><CR>
"nmap <C-M><C-M>i
"    \:vert scs find i ^<C-R>=expand("<cfile>")<CR>$<CR>
"nmap <C-M><C-M>d
"    \:vert scs find d <C-R>=expand("<cword>")<CR><CR>
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" end cscope setting
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
""""""""""""""""""""
"====== Minibuffer plugin======="
"let g:miniBufExplModSelTarget = 1
"let g:miniBufExplorerMoreThanOne = 2
"let g:miniBufExplModSelTarget = 0
"let g:miniBufExplUseSingleClick = 1
"let g:miniBufExplMapWindowNavVim = 1
"let g:miniBufExplVSplit = 25
"let g:miniBufExplSplitBelow=1
let g:bufExplorerSortBy = "name"
autocmd BufRead,BufNew :call UMiniBufExplorer
"使用<leader>u 打开Minibuffer
map <leader>u :TMiniBufExplorer<cr>
"========================================"
"Auto Completion Popmenu
let g:acp_behaviorSnipmateLength=1

Comments