Vim配置

给自己看的,以后到了新机子上要会。。。

  1 set nocompatible
  2 source $VIMRUNTIME/vimrc_example.vim
  3 source $VIMRUNTIME/mswin.vim
  4 
  5 set guifont=Courier\ New:h10
  6 set lines=35 columns=118
  7 
  8 
  9 
 10 "------------------------------------------------------------------------------
 11 "  < 判断操作系统是否是 Windows 还是 Linux >
 12 "------------------------------------------------------------------------------
 13 if(has("win32") || has("win64") || has("win95") || has("win16"))
 14     let g:iswindows = 1
 15 else
 16     let g:iswindows = 0
 17 endif
 18  
 19 "------------------------------------------------------------------------------
 20 "  < 判断是终端还是 Gvim >
 21 "------------------------------------------------------------------------------
 22 if has("gui_running")
 23     let g:isGUI = 1
 24 else
 25     let g:isGUI = 0
 26 endif
 27  
 28 "------------------------------------------------------------------------------
 29 "  < 编译、连接、运行配置 >
 30 "------------------------------------------------------------------------------
 31 " F9 一键保存、编译、连接存并运行
 32 map <F9> :call Run()<CR>
 33 imap <F9> <ESC>:call Run()<CR>
 34  
 35 " Ctrl + F9 一键保存并编译
 36 map <c-F9> :call Compile()<CR>
 37 imap <c-F9> <ESC>:call Compile()<CR>
 38  
 39 " Ctrl + F10 一键保存并连接
 40 map <c-F10> :call Link()<CR>
 41 imap <c-F10> <ESC>:call Link()<CR>
 42  
 43 let s:LastShellReturn_C = 0
 44 let s:LastShellReturn_L = 0
 45 let s:ShowWarning = 1
 46 let s:Obj_Extension = '.o'
 47 let s:Exe_Extension = '.exe'
 48 let s:Sou_Error = 0
 49  
 50 let s:windows_CFlags = 'gcc\ -fexec-charset=gbk\ -Wall\ -g\ -O0\ -c\ %\ -o\ %<.o'
 51 let s:linux_CFlags = 'gcc\ -Wall\ -g\ -O0\ -c\ %\ -o\ %<.o'
 52  
 53 let s:windows_CPPFlags = 'g++\ -fexec-charset=gbk\ -Wall\ -g\ -O0\ -c\ %\ -o\ %<.o'
 54 let s:linux_CPPFlags = 'g++\ -Wall\ -g\ -O0\ -c\ %\ -o\ %<.o'
 55  
 56 func! Compile()
 57     exe ":ccl"
 58     exe ":update"
 59     if expand("%:e") == "c" || expand("%:e") == "cpp" || expand("%:e") == "cxx"
 60         let s:Sou_Error = 0
 61         let s:LastShellReturn_C = 0
 62         let Sou = expand("%:p")
 63         let Obj = expand("%:p:r").s:Obj_Extension
 64         let Obj_Name = expand("%:p:t:r").s:Obj_Extension
 65         let v:statusmsg = ''
 66         if !filereadable(Obj) || (filereadable(Obj) && (getftime(Obj) < getftime(Sou)))
 67             redraw!
 68             if expand("%:e") == "c"
 69                 if g:iswindows
 70                     exe ":setlocal makeprg=".s:windows_CFlags
 71                 else
 72                     exe ":setlocal makeprg=".s:linux_CFlags
 73                 endif
 74                 echohl WarningMsg | echo " compiling..."
 75                 silent make
 76             elseif expand("%:e") == "cpp" || expand("%:e") == "cxx"
 77                 if g:iswindows
 78                     exe ":setlocal makeprg=".s:windows_CPPFlags
 79                 else
 80                     exe ":setlocal makeprg=".s:linux_CPPFlags
 81                 endif
 82                 echohl WarningMsg | echo " compiling..."
 83                 silent make
 84             endif
 85             redraw!
 86             if v:shell_error != 0
 87                 let s:LastShellReturn_C = v:shell_error
 88             endif
 89             if g:iswindows
 90                 if s:LastShellReturn_C != 0
 91                     exe ":bo cope"
 92                     echohl WarningMsg | echo " compilation failed"
 93                 else
 94                     if s:ShowWarning
 95                         exe ":bo cw"
 96                     endif
 97                     echohl WarningMsg | echo " compilation successful"
 98                 endif
 99             else
100                 if empty(v:statusmsg)
101                     echohl WarningMsg | echo " compilation successful"
102                 else
103                     exe ":bo cope"
104                 endif
105             endif
106         else
107             echohl WarningMsg | echo ""Obj_Name"is up to date"
108         endif
109     else
110         let s:Sou_Error = 1
111         echohl WarningMsg | echo " please choose the correct source file"
112     endif
113     exe ":setlocal makeprg=make"
114 endfunc
115  
116 func! Link()
117     call Compile()
118     if s:Sou_Error || s:LastShellReturn_C != 0
119         return
120     endif
121     let s:LastShellReturn_L = 0
122     let Sou = expand("%:p")
123     let Obj = expand("%:p:r").s:Obj_Extension
124     if g:iswindows
125         let Exe = expand("%:p:r").s:Exe_Extension
126         let Exe_Name = expand("%:p:t:r").s:Exe_Extension
127     else
128         let Exe = expand("%:p:r")
129         let Exe_Name = expand("%:p:t:r")
130     endif
131     let v:statusmsg = ''
132     if filereadable(Obj) && (getftime(Obj) >= getftime(Sou))
133         redraw!
134         if !executable(Exe) || (executable(Exe) && getftime(Exe) < getftime(Obj))
135             if expand("%:e") == "c"
136                 setlocal makeprg=gcc\ -o\ %<\ %<.o
137                 echohl WarningMsg | echo " linking..."
138                 silent make
139             elseif expand("%:e") == "cpp" || expand("%:e") == "cxx"
140                 setlocal makeprg=g++\ -o\ %<\ %<.o
141                 echohl WarningMsg | echo " linking..."
142                 silent make
143             endif
144             redraw!
145             if v:shell_error != 0
146                 let s:LastShellReturn_L = v:shell_error
147             endif
148             if g:iswindows
149                 if s:LastShellReturn_L != 0
150                     exe ":bo cope"
151                     echohl WarningMsg | echo " linking failed"
152                 else
153                     if s:ShowWarning
154                         exe ":bo cw"
155                     endif
156                     echohl WarningMsg | echo " linking successful"
157                 endif
158             else
159                 if empty(v:statusmsg)
160                     echohl WarningMsg | echo " linking successful"
161                 else
162                     exe ":bo cope"
163                 endif
164             endif
165         else
166             echohl WarningMsg | echo ""Exe_Name"is up to date"
167         endif
168     endif
169     setlocal makeprg=make
170 endfunc
171  
172 func! Run()
173     let s:ShowWarning = 0
174     call Link()
175     let s:ShowWarning = 1
176     if s:Sou_Error || s:LastShellReturn_C != 0 || s:LastShellReturn_L != 0
177         return
178     endif
179     let Sou = expand("%:p")
180     let Obj = expand("%:p:r").s:Obj_Extension
181     if g:iswindows
182         let Exe = expand("%:p:r").s:Exe_Extension
183     else
184         let Exe = expand("%:p:r")
185     endif
186     if executable(Exe) && getftime(Exe) >= getftime(Obj) && getftime(Obj) >= getftime(Sou)
187         redraw!
188         echohl WarningMsg | echo " running..."
189         if g:iswindows
190             exe ":!%<.exe"
191         else
192             if g:isGUI
193                 exe ":!gnome-terminal -e ./%<"
194             else
195                 exe ":!./%<"
196             endif
197         endif
198         redraw!
199         echohl WarningMsg | echo " running finish"
200     endif
201 endfunc
202 
203 behave mswin
204 
205    " This line should not be removed as it ensures that various options are
206     " properly set to work with the Vim-related packages available in Debian.
207 
208     " Uncomment the next line to make Vim more Vi-compatible
209     " NOTE: debian.vim sets 'nocompatible'. Setting 'compatible' changes numerous
210     " options, so any other options should be set AFTER setting 'compatible'.
211     set nocompatible
212 
213     " Vim5 and later versions support syntax highlighting. Uncommenting the
214     " following enables syntax highlighting by default.
215     if has("syntax")
216       syntax on            " 语法高亮
217     endif
218     colorscheme ron        " elflord ron peachpuff default 设置配色方案,vim自带的配色方案保存在/usr/share/vim/vim72/colors目录下
219 
220     " detect file type
221     filetype on
222     filetype plugin on
223 
224     " If using a dark background within the editing area and syntax highlighting
225     " turn on this option as well
226     set background=dark
227 
228     " Uncomment the following to have Vim jump to the last position when
229     " reopening a file
230     if has("autocmd")
231       au BufReadPost * if line("'\"") > 1 && line("'\"") <= line("$") | exe "normal! g'\"" | endif
232       "have Vim load indentation rules and plugins according to the detected filetype
233       filetype plugin indent on
234     endif
235 
236     " The following are commented out as they cause vim to behave a lot
237     " differently from regular Vi. They are highly recommended though.
238 
239     "set ignorecase        " 搜索模式里忽略大小写
240     "set smartcase        " 如果搜索模式包含大写字符,不使用 'ignorecase' 选项。只有在输入搜索模式并且打开 'ignorecase' 选项时才会使用。
241     set autowrite        " 自动把内容写回文件: 如果文件被修改过,在每个 :next、:rewind、:last、:first、:previous、:stop、:suspend、:tag、:!、:make、CTRL-] 和 CTRL-^命令时进行;用 :buffer、CTRL-O、CTRL-I、'{A-Z0-9} 或 `{A-Z0-9} 命令转到别的文件时亦然。
242     set autoindent        " 设置自动对齐(缩进):即每行的缩进值与上一行相等;使用 noautoindent 取消设置
243     "set smartindent        " 智能对齐方式
244     set tabstop=4        " 设置制表符(tab键)的宽度
245     set softtabstop=4     " 设置软制表符的宽度    
246     set shiftwidth=4    " (自动) 缩进使用的4个空格
247     set cindent            " 使用 C/C++ 语言的自动缩进方式
248     set cinoptions={0,1s,t0,n-2,p2s,(03s,=.5s,>1s,=1s,:1s     "设置C/C++语言的具体缩进方式
249     "set backspace=2    " 设置退格键可用
250     set showmatch        " 设置匹配模式,显示匹配的括号
251     set linebreak        " 整词换行
252     set whichwrap=b,s,<,>,[,] " 光标从行首和行末时可以跳到另一行去
253     "set hidden " Hide buffers when they are abandoned
254     set mouse=a            " Enable mouse usage (all modes)    "使用鼠标
255     set number            " Enable line number    "显示行号
256     "set previewwindow    " 标识预览窗口
257     set history=50        " set command history to 50    "历史记录50条
258 
259 
260     "--状态行设置--
261     set laststatus=2 " 总显示最后一个窗口的状态行;设为1则窗口数多于一个的时候显示最后一个窗口的状态行;0不显示最后一个窗口的状态行
262     set ruler            " 标尺,用于显示光标位置的行号和列号,逗号分隔。每个窗口都有自己的标尺。如果窗口有状态行,标尺在那里显示。否则,它显示在屏幕的最后一行上。
263 
264     "--命令行设置--
265     set showcmd            " 命令行显示输入的命令
266     set showmode        " 命令行显示vim当前模式
267 
268     "--find setting--
269     set incsearch        " 输入字符串就显示匹配点
270     set hlsearch        
271 
272 set diffexpr=MyDiff()
273 function MyDiff()
274   let opt = '-a --binary '
275   if &diffopt =~ 'icase' | let opt = opt . '-i ' | endif
276   if &diffopt =~ 'iwhite' | let opt = opt . '-b ' | endif
277   let arg1 = v:fname_in
278   if arg1 =~ ' ' | let arg1 = '"' . arg1 . '"' | endif
279   let arg2 = v:fname_new
280   if arg2 =~ ' ' | let arg2 = '"' . arg2 . '"' | endif
281   let arg3 = v:fname_out
282   if arg3 =~ ' ' | let arg3 = '"' . arg3 . '"' | endif
283   let eq = ''
284   if $VIMRUNTIME =~ ' '
285     if &sh =~ '\<cmd'
286       let cmd = '""' . $VIMRUNTIME . '\diff"'
287       let eq = '"'
288     else
289       let cmd = substitute($VIMRUNTIME, ' ', '" ', '') . '\diff"'
290     endif
291   else
292     let cmd = $VIMRUNTIME . '\diff'
293   endif
294   silent execute '!' . cmd . ' ' . opt . arg1 . ' ' . arg2 . ' > ' . arg3 . eq
295 endfunction
296 
297 "进行版权声明的设置
298 "添加或更新头
299 map <F4> :call TitleDet()<cr>'s
300 function AddTitle()
301 call append(0,"/*=============================================================================")
302     call append(1,"* ")
303     call append(2,"*  Author: Crt_16")
304     call append(3,"* ")
305     call append(4,"*  Last modified: ".strftime("%Y-%m-%d %H:%M"))
306     call append(5,"* ")
307     call append(6,"*  Category:")  
308     call append(7,"* ")  
309     call append(8,"/*=============================================================================*/")
310 call append(9,"")  
311 call append(10,"#include <iostream>")  
312 call append(11,"#include<cstdio>")
313 call append(12,"#include<cmath>")
314 call append(13,"#include<algorithm>")
315 call append(14,"#include<stack>")
316 call append(15,"#include<queue>")
317 call append(16,"#include<cstring>")
318 call append(17,"#define pau putchar(' ')")
319 call append(18,"#define ent puts("")")
320 call append(19,"#define mse(a,b) memset(a,b,sizeof(a))")
321 call append(20,"#define ren(x) for(ted*e=fch[x];e;e=e->nxt)")
322 call append(21,"#define rep(i,s,t) for(int i=s,__=t;i<=__;i++)")
323 call append(22,"#define dwn(i,s,t) for(int i=s,__=t;i>=__;i--)")
324 call append(23,"using namespace std;")
325 call append(24,"inline int read(){")
326 call append(25,"    int x=0;bool sig=true;char ch=getchar();")
327 call append(26,"    for(;!isdigit(ch);ch=getchar())if(ch=='-')sig=false;")
328 call append(27,"    for(;isdigit(ch);ch=getchar())x=10*x+ch-'0';return sig?x:-x;")
329 call append(28,"}")
330 call append(29,"inline void write(int x){")
331 call append(30,"    if(x==0){putchar('0');return;}if(x<0)putchar('-'),x=-x;")
332 call append(31,"    int len=0;static int buf[20];while(x)buf[len++]=x%10,x/=10;")
333 call append(32,"    for(int i=len-1;i>=0;i--)putchar(buf[i]+'0');return;")
334 call append(33,"}")
335 call append(34,"int main(){")
336 call append(35,"    return 0;")
337 call append(36,"}")
338 
339     echohl WarningMsg | echo "Successful in adding the copyright." | echohl None
340 endf
341 "更新最近修改时间和文件名
342 function UpdateTitle()
343     normal m'
344     execute '/# *Last modified:/s@:.*$@\=strftime(":\t%Y-%m-%d %H:%M")@'
345     normal ''
346     normal mk
347     execute "noh"
348     normal 'k
349     echohl WarningMsg | echo "Successful in updating the copy right." | echohl None
350 endfunction
351 "判断前10行代码里面,是否有Last modified这个单词,
352 "如果没有的话,代表没有添加过作者信息,需要新添加;
353 "如果有的话,那么只需要更新即可
354 function TitleDet()
355     let n=1
356     "默认为添加
357     while n < 10
358         let line = getline(n)
359         if line =~ '^\#\s*\S*Last\smodified:\S*.*$'
360             call UpdateTitle()
361             return
362         endif
363         let n = n + 1
364     endwhile
365     call AddTitle()
366 endfunction

 

posted @ 2015-08-25 11:32  AI_Believer  阅读(254)  评论(0编辑  收藏  举报