vim配置vimrc

时间: 2023-10-04 admin IT培训

vim配置vimrc

vim配置vimrc

新建文件,自动加入文件头

修改文件,保存时,自动更新修改时间字段

自动匹配括号,引号等

vimrc文件如下

  1 "==========================================  
  2 "General  
  3 "==========================================  
  4   
  5 " history存储长度。  
  6 set history=1000         
  7 set encoding=utf-8
  8 set fileencodings=ucs-bom,utf-8,cp936
  9 set fileencoding=gb2312
 10 set termencoding=utf-8
 11   
 12 "检测文件类型  
 13 filetype on  
 14 " 针对不同的文件类型采用不同的缩进格式    
 15 filetype indent on                 
 16 "允许插件    
 17 filetype plugin on  
 18 "启动自动补全  
 19 filetype plugin indent on  
 20   
 21 " 非兼容vi模式。去掉讨厌的有关vi一致性模式,避免以前版本的一些bug和局限  
 22 set nocompatible        
 23 set autoread          " 文件修改之后自动载入。  
 24   
 25 " 取消备份。  
 26 " Turn backup off, since most stuff is in SVN, git et.c anyway...  
 27 set nobackup  
 28 set nowb  
 29 set noswapfile  
 30   
 31 " No annoying sound on errors  
 32 " 去掉输入错误的提示声音  
 33 set noerrorbells  
 34 set novisualbell  
 35 set t_vb=  
 36 set tm=500  
 37   
 38   
 39 "==========================================  
 40 " show and format  
 41 "==========================================  
 42 "显示行号:  
 43 set number  
 44 set nowrap                    " 取消换行。  
 45 ""为方便复制,用<F2>开启/关闭行号显示:  
 46 nnoremap <F2> :set nonumber!<CR>:set foldcolumn=0<CR>  
 47   
 48   
 49 "括号配对情况  
 50 set showmatch  
 51 " How many tenths of a second to blink when matching brackets  
 52 set mat=2  
 53   
 54 "设置文内智能搜索提示  
 55 " 高亮search命中的文本。  
 56 set hlsearch            
 57 " 搜索时忽略大小写  
 58 set ignorecase  
 59 " 随着键入即时搜索  
 60 set incsearch  
 61 " 有一个或以上大写字母时仍大小写敏感  
 62 set smartcase  
 63   
 64 " 代码折叠  
 65 " set foldenable  
 66 " 折叠方法  
 67 " manual    手工折叠  
 68 " indent    使用缩进表示折叠  
 69 " expr      使用表达式定义折叠  
 70 " syntax    使用语法定义折叠  
 71 " diff      对没有更改的文本进行折叠  
 72 " marker    使用标记进行折叠, 默认标记是 {{{ 和 }}}  
 73 " set foldmethod=syntax  
 74 " 在左侧显示折叠的层次  
 75 " set foldcolumn=4  
 76   
 77 set tabstop=4                " 设置Tab键的宽度        [等同的空格个数]  
 78 set shiftwidth=4  
 79 set expandtab                " 将Tab自动转化成空格    [需要输入真正的Tab键时,使用 Ctrl+V + Tab]  
 80 " 按退格键时可以一次删掉 4 个空格  
 81 set softtabstop=4  
 82   
 83 set ai "Auto indent  
 84 set si "Smart indent  
 85   
 86 "==========================================  
 87 " status  
 88 "==========================================  
 89 "显示当前的行号列号:  
 90 set ruler  
 91 ""在状态栏显示正在输入的命令  
 92 set showcmd  
 93   
 94 " Set 7 lines to the cursor - when moving vertically using j/k 上下滚动,始终在中间  
 95 set so=7  
 96   
 97 "set cursorline              " 突出显示当前行  
 98   
 99 " 命令行(在状态行下)的高度,默认为1,这里是2  
100 " set cmdheight=2  
101 set statusline=%F%m%r%h%w\ [FORMAT=%{&ff}]\ [TYPE=%Y]\ [POS=%l,%v][%p%%]\ %{strftime(\"%d/%m/%y\ -\ %H:%M\")}
102 " Always show the status line  
103 set laststatus=2  
104   
105   
106 "==========================================  
107 "colors and fonts  
108 "==========================================  
109 "开启语法高亮  
110 syntax enable  
111 syntax on  
112   
113 "配色方案 三种,选一个  
114 "colorscheme darkblue          " 深蓝色配色方案。  
115   
116 "colorscheme desert " 经典配色方案。  
117 "set background=dark  
118   
119 " Set extra options when running in GUI mode  
120 if has("gui_running")  
121     set guioptions-=T  
122     set guioptions+=e  
123     set t_Co=256  
124     set guitablabel=%M\ %t  
125 endif  
126 "set guifont=Monaco:h20          " 字体 && 字号    
127   
128 "==========================================  
129 " file encode  
130 "==========================================  
131 
132 " Use Unix as the standard file type  
133 set ffs=unix,dos,mac  
134   
135 " 如遇Unicode值大于255的文本,不必等到空格再折行。  
136 set formatoptions+=m  
137 " 合并两行中文时,不在中间加空格:  
138 set formatoptions+=B  
139   
140   
141 "==========================================  
142 "others  
143 "==========================================  
144   
145 " 自动完成   
146 set completeopt=longest,menu  
147 " 增强模式中的命令行自动完成操作  
148 set wildmenu  
149 " Ignore compiled files  
150 set wildignore=*.o,*~,*.pyc  
151   
152 " Python 文件的一般设置,比如不要 tab 等  
153 autocmd FileType python set tabstop=4 shiftwidth=4 expandtab  
154 "自动补全配置  
155 autocmd FileType python set omnifunc=pythoncomplete#Complete  
156   
157   
158 " Return to last edit position when opening files (You want this!)  
159 autocmd BufReadPost *  
160      \ if line("'\"") > 0 && line("'\"") <= line("$") |  
161      \   exe "normal! g`\"" |  
162      \ endif  
163   
164 " A buffer becomes hidden when it is abandoned  
165 "set hid  
166   
167 " For regular expressions turn magic on  
168 set magic  
169   
170 " Configure backspace so it acts as it should act  
171 set backspace=eol,start,indent  
172 set whichwrap+=<,>,h,l
173 
174 
175 "新建.c,.h,.sh,.Java文件,自动插入文件头 
176 autocmd BufNewFile *.cpp,*.hpp,*,*.[ch],*.sh,*.Java exec ":call SetTitle()"
177 ""定义函数SetTitle,自动插入文件头 
178 func SetTitle() 
179     if &filetype == 'sh' 
180         call setline(1,"\#########################################################################") 
181         call append(line("."), "\# File Name: ".expand("%")) 
182         call append(line(".")+1, "\# Author: carbonyang@shuame") 
183         call append(line(".")+2, "\# Description: carbonyang@shuame") 
184         call append(line(".")+3, "\# Created Time: ".strftime("%c"))
185         call append(line(".")+4, "\# Modified Time: ".strftime("%c"))
186         call append(line(".")+5, "\#########################################################################") 
187         call append(line(".")+6, "")
188         call append(line(".")+7, "")
189         call append(line(".")+8, "")
190         call append(line(".")+9, "\#!/bin/bash") 
191         call append(line(".")+10, "") 
192     else 
193         call setline(1, "/*************************************************************************") 
194         call append(line("."), "\ * File Name: ".expand("%"))
195         call append(line(".")+1, "\ * Author: carbonyang@shuame")
196         call append(line(".")+2, "\ * Created Time: ".strftime("%Y-%m-%d %H:%M:%S %Z"))
197         call append(line(".")+3, "\ * Modified Time: ".strftime("%Y-%m-%d %H:%M:%S %Z"))
198         call append(line(".")+4, "\ * Description: ") 
199         call append(line(".")+5,  "\ * ┏┛ ┻━━━━━┛ ┻┓") 
200         call append(line(".")+6,  "\ * ┃           ┃") 
201         call append(line(".")+7,  "\ * ┃      ━     ┃") 
202         call append(line(".")+8,  "\ * ┃  ┳┛    ┗┳   ┃") 
203         call append(line(".")+9,  "\ * ┃           ┃") 
204         call append(line(".")+10, "\ * ┃      ┻     ┃") 
205         call append(line(".")+11, "\ * ┃           ┃") 
206         call append(line(".")+12, "\ * ┗━┓      ┏━━━┛") 
207         call append(line(".")+13, "\ *   ┃      ┃   神兽保佑") 
208         call append(line(".")+14, "\ *   ┃      ┃   代码无虫") 
209         call append(line(".")+15, "\ *   ┃      ┗━━━━━━━━━┓") 
210         call append(line(".")+16, "\ *   ┃                ┣┓") 
211         call append(line(".")+17, "\ *   ┃               ┏┛") 
212         call append(line(".")+18, "\ *   ┗━┓ ┓ ┏━━━┳ ┓ ┏━┛") 
213         call append(line(".")+19, "\ *     ┃ ┫ ┫   ┃ ┫ ┫") 
214         call append(line(".")+20, "\ *    ┗━┻━┛   ┗━┻━┛") 
215         call append(line(".")+21, "************************************************************************/") 
216         call append(line(".")+22, "")
217         call append(line(".")+23, "")
218         call append(line(".")+24, "")
219     endif
220     if &filetype == 'cpp'
221         call append(line(".")+25, "#include<iostream>")
222         call append(line(".")+26, "")
223         call append(line(".")+27, "using namespace std;")
224         call append(line(".")+28, "")
225         call append(line(".")+29, "")
226     endif
227     if &filetype == 'c'
228         call append(line(".")+25, "#include<stdio.h>")
229         call append(line(".")+26, "")
230         call append(line(".")+27, "")
231     endif
232     autocmd BufNewFile * normal G
233 endfunc 
234 
235 let g:update_time_time_stamp_leader = 'Modified Time: '
236 let g:update_time_enable = 1
237 let g:update_time_time_format = '%Y-%m-%d %H:%M:%S %Z'
238 map <F4> gg: <Esc>O<Esc>: call SetTitle()<cr> <Esc>G
239 
240 " 括号自动补全
241 inoremap ( ()<Esc>i
242 inoremap [ []<Esc>i
243 inoremap { {<CR>}<Esc>O
244 autocmd Syntax html,vim inoremap < <lt>><Esc>i| inoremap > <c-r>=ClosePair('>')<CR>
245 inoremap ) <c-r>=ClosePair(')')<CR>
246 inoremap ] <c-r>=ClosePair(']')<CR>
247 inoremap } <c-r>=CloseBracket()<CR>
248 inoremap " <c-r>=QuoteDelim('"')<CR>
249 inoremap ' <c-r>=QuoteDelim("'")<CR>
250 
251 function ClosePair(char)
252  if getline('.')[col('.') - 1] == a:char
253  return "\<Right>"
254  else
255  return a:char
256  endif
257 endf
258 
259 function CloseBracket()
260  if match(getline(line('.') + 1), '\s*}') < 0
261  return "\<CR>}"
262  else
263  return "\<Esc>j0f}a"
264  endif
265 endf
266 
267 function QuoteDelim(char)
268  let line = getline('.')
269  let col = col('.')
270  if line[col - 2] == "\\"
271  return a:char
272  elseif line[col - 1] == a:char
273  return "\<Right>"
274  else
275  return a:char.a:char."\<Esc>i"
276  endif
277 endf
vimrc

 

自动更新修改时间需要用到插件update-time.vim

保存至.vim/plugin/即可自动加载

update-time代码:

 1 " File: update-time.vim
 2 " Author: QianChenglong <qianchenglong2012@gmail>
 3 " Create Time: 2013-12-04 19:36:21 CST
 4 " Last Change: 2013-12-05 12:14:15 CST
 5 " Description: Automatic update Last Change time
 6 
 7 " SECTION: Init"{{{
 8 if exists("g:loaded_update_time")
 9     finish
10 endif
11 let g:loaded_update_time = 1
12 
13 let s:save_cpo = &cpo
14 set cpo&vim
15 "}}}
16 " SECTION: Varible"{{{
17 if !exists('g:update_time_time_stamp_leader')
18     let s:time_stamp_leader = 'Last Change: '
19 else
20     let s:time_stamp_leader = g:update_time_time_stamp_leader
21 endif
22 
23 if !exists('g:update_time_time_format')
24     let s:time_format = '%Y-%m-%d %H:%M:%S %Z'
25 else
26     let s:time_format = g:update_time_time_format
27 endif
28 
29 if !exists("g:update_time_begin_line")
30     let s:begin_line = 0
31 else
32     let s:begin_line = g:update_time_begin_line
33 endif
34 
35 if !exists('g:update_time_end_line')
36     let s:end_line = 10
37 else
38     let s:end_line = g:update_time_end_line
39 endif
40 
41 if !exists('g:update_time_enable')
42     let s:update_time_enable = 1
43 else
44     let s:update_time_enable = g:update_time_enable
45 endif
46 "}}}
47 " SECTION: Funtions"{{{
48 fun Update_time_update()
49     if ! &modifiable
50         return
51     endif
52     if ! s:update_time_enable
53         return
54     endif
55     let bufmodified = getbufvar('%', '&mod')
56     if ! bufmodified
57         return
58     endif
59     let pos = line('.').' | normal! '.virtcol('.').'|'
60     exe s:begin_line
61     let line_num = search(s:time_stamp_leader, '', s:end_line)
62     if line_num > 0
63         let line = getline(line_num)
64         let line = substitute(line, s:time_stamp_leader . '\zs.*', strftime(s:time_format), '')
65         call setline(line_num, line)
66     endif
67     exe pos
68 endf
69 fun Update_time_toggle()
70     let s:update_time_enable = !s:update_time_enable
71 endf
72 "}}}
73 " SECTION: Autocommands"{{{
74 autocmd BufWritePre * call Update_time_update()
75 "}}}
76 " SECTION: Commands"{{{
77 com! -nargs=0 UpdateTimeToggle call Update_time_toggle()
78 "}}}
79 " SECTION: Clean up"{{{
80 let &cpo = s:save_cpo
81 unlet s:save_cpo
82 "}}}
update-time

 

转载于:.html