vim配置文件

vim配置文件  2012-02-21 13:50:14

分类:

原文地址:vim配置文件 作者:liujunwei1234

五一假期闲的难受,就把vim和emacs的配置文件重新整理的一遍,贴出来与大家共享,先介绍vim的配置文件。
主要参考某位大牛师弟的配置,修改而成。文中红色标记部分是比较常用的部分。

使用说明:

1. cscope使用说明

;ss ↔ cs find s ;sg ↔ cs find g
; st ↔ cs find t ;sd ↔ cs find d
; sc ↔ cs find c ;sf ↔ cs find f
; si ↔ cs find i ;se ↔ cs find e
命令说明:  c: Find functions calling this function
d: Find functions called by this function
e: Find this egrep pattern
f: Find this file
g: Find this definition
i: Find files #including this file
s: Find this C symbol
t: Find assignments to
  1. 常用快捷键

;F: BufExplorer 窗口 ;ffF5: LookupFile窗口

;t: TagList窗口 ;n: NERD Tree窗口

F12:更新tagscscope文件

C-hshift+tab:向前切换buffer C-ntab:向后切换buffer

注意:这里的前后是指buffer打开的先后

;cc:单行注释

;cs: 单行性感注释

;cu: 取消注释


  1. " vimrc by ustc.dylan@gmail.com
  2. " Modified Data: 2011-05-01
  3. let mapleader = ";" " 比较习惯用;作为命令前缀,右手小拇指直接能按到
  4. " 把空格键映射成:
  5. nmap <space> :
  6. " 快捷打开编辑vimrc文件的键盘绑定
  7. map <silent> <leader>ee :e $HOME/.vimrc<cr>
  8. autocmd! bufwritepost *.vimrc source $HOME/.vimrc
  9. " 保存文件
  10. nmap <leader>ww :w!<cr>
  11. " ^z快速进入shell
  12. nmap <C-Z> :shell<cr>
  13. " 判断操作系统
  14. if (has("win32") || has("win64") || has("win32unix"))
  15. let g:isWin = 1
  16. else
  17. let g:isWin = 0
  18. endif
  19. " 判断是终端还是gvim
  20. if has("gui_running")
  21. let g:isGUI = 1
  22. else
  23. let g:isGUI = 0
  24. endif
  25. set nocompatible " 关闭兼容模式
  26. syntax enable " 语法高亮
  27. filetype plugin on " 文件类型插件
  28. filetype indent on
  29. set autoindent
  30. autocmd BufEnter * :syntax sync fromstart
  31. set nu " 显示行号
  32. set showcmd " 显示命令
  33. set lz " 当运行宏时,在命令执行完成之前,不重绘屏幕
  34. set hid " 可以在没有保存的情况下切换buffer
  35. set backspace=eol,start,indent
  36. set whichwrap+=<,>,h,l " 退格键和方向键可以换行
  37. set incsearch " 增量式搜索
  38. set hlsearch " 高亮搜索
  39. set ignorecase " 搜索时忽略大小写
  40. set magic "
  41. set showmatch " 显示匹配的括号
  42. set nobackup " 关闭备份
  43. set nowb
  44. set noswapfile " 不使用swp文件,注意,错误退出后无法恢复
  45. set lbr " 在breakat字符处而不是最后一个字符处断行
  46. set ai " 自动缩进
  47. set si " 智能缩进
  48. set cindent " C/C++风格缩进
  49. set wildmenu
  50. set nofen
  51. set foldenable
  52. set fdl=3
  53. " tab转化为4个字符
  54. set expandtab
  55. set smarttab
  56. set shiftwidth=4
  57. set tabstop=4
  58. " 不使用beep或flash
  59. set vb t_vb=
  60. set background=dark
  61. colorscheme desert
  62. "set t_Co=256
  63. set history=400 " vim记住的历史操作的数量,默认的是20
  64. set autoread " 当文件在外部被修改时,自动重新读取
  65. set mouse=a " 在所有模式下都允许使用鼠标,还可以是n,v,i,c等
  66. "在gvim中高亮当前行
  67. if (g:isGUI)
  68. set cursorline
  69. hi cursorline guibg=#333333
  70. hi CursorColumn guibg=#333333
  71. set guifont=Consolas\ 14
  72. set guifontwide=Consolas\ 14
  73. endif
  74. " 设置字符集编码,默认使用utf8
  75. if (g:isWin)
  76. let &termencoding=&encoding " 通常win下的encoding为cp936
  77. set fileencodings=utf8,cp936,ucs-bom,latin1
  78. else
  79. set encoding=utf8
  80. set fileencodings=utf8,gb2312,gb18030,ucs-bom,latin1
  81. endif
  82. " 状态栏
  83. set laststatus=2 " 总是显示状态栏
  84. highlight StatusLine cterm=bold ctermfg=yellow ctermbg=blue
  85. " 获取当前路径,将$HOME转化为~
  86. function! CurDir()
  87. let curdir = substitute(getcwd(), $HOME, "~", "g")
  88. return curdir
  89. endfunction
  90. set statusline=[%n]\ %f%m%r%h\ \|\ \ pwd:\ %{CurDir()}\ \ \|%=\|\ %l,%c\ %p%%\ \|\ ascii=%b,hex=%b%{((&fenc==\"\")?\"\":\"\ \|\ \".&fenc)}\ \|\ %{$USER}\ @\ %{hostname()}\
  91. " 第80列往后加下划线
  92. "au BufWinEnter * let w:m2=matchadd('Underlined', '\%>' . 80 . 'v.\+', -1)
  93. " 根据给定方向搜索当前光标下的单词,结合下面两个绑定使用
  94. function! VisualSearch(direction) range
  95. let l:saved_reg = @"
  96. execute "normal! vgvy"
  97. let l:pattern = escape(@", '\\/.*$^~[]')
  98. let l:pattern = substitute(l:pattern, "\n$", "", "")
  99. if a:direction == 'b'
  100. execute "normal ?" . l:pattern . "<cr>"
  101. else
  102. execute "normal /" . l:pattern . "<cr>"
  103. endif
  104. let @/ = l:pattern
  105. let @" = l:saved_reg
  106. endfunction
  107. " 用 */# 向 前/后 搜索光标下的单词
  108. vnoremap <silent> * :call VisualSearch('f')<CR>
  109. vnoremap <silent> # :call VisualSearch('b')<CR>
  110. " 在文件名上按gf时,在新的tab中打开
  111. "map gf :tabnew <cfile><cr>
  112. " 用c-j,k在buffer之间切换
  113. nn <c-n> :bn<cr>
  114. nn <c-h> :bp<cr>
  115. nn <tab> :bn<cr>
  116. nn <s-tab> :bp<cr>
  117. " Bash(Emacs)风格键盘绑定
  118. imap <C-e> <END> “光标移动到行首
  119. imap <C-a> <HOME>“光标移动到行尾
  120. "imap <C-u> <esc>d0i
  121. "imap <C-k> <esc>d$i " 与自动补全中的绑定冲突
  122. " 恢复上次文件打开位置
  123. set viminfo='10,\"100,:20,%,n~/.viminfo
  124. au BufReadPost * if line("'\"") > 0|if line("'\"") <= line("$")|exe("norm '\"")|else|exe "norm $"|endif|endif
  125. " 删除buffer时不关闭窗口
  126. command! Bclose call <SID>BufcloseCloseIt()
  127. function! <SID>BufcloseCloseIt()
  128. let l:currentBufNum = bufnr("%")
  129. let l:alternateBufNum = bufnr("#")
  130. if buflisted(l:alternateBufNum)
  131. buffer #
  132. else
  133. bnext
  134. endif
  135. if bufnr("%") == l:currentBufNum
  136. new
  137. endif
  138. if buflisted(l:currentBufNum)
  139. execute("bdelete! ".l:currentBufNum)
  140. endif
  141. endfunction
  142. " 快捷输入
  143. " 自动完成括号和引号
  144. inoremap <leader>1 ()<esc>:let leavechar=")"<cr>i
  145. inoremap <leader>2 []<esc>:let leavechar="]"<cr>i
  146. inoremap <leader>3 {}<esc>:let leavechar="}"<cr>i
  147. inoremap <leader>4 {<esc>o}<esc>:let leavechar="}"<cr>O
  148. inoremap <leader>q ''<esc>:let leavechar="'"<cr>i
  149. inoremap <leader>w ""<esc>:let leavechar='"'<cr>i
  150. " 缩写
  151. iab idate <c-r>=strftime("%Y-%m-%d")<CR>
  152. iab itime <c-r>=strftime("%H:%M")<CR>
  153. iab imail Stephen <stephenpcg@gmail.com>
  154. iab iumail stephen1@mail.ustc.edu.cn
  155. iab igmail stephenpcg@gmail.com
  156. iab iname Zhang Cheng
  157. " 插件窗口的宽度,如TagList,NERD_tree等,自己设置
  158. let s:PlugWinSize = 30
  159. " txtbrowser.vim
  160. " http://www.vim.org/scripts/script.php?script_id=273
  161. let tlist_txt_settings = 'txt;c:content;f:figures;t:tables'
  162. au BufRead,BufNewFile *.txt setlocal ft=txt
  163. nmap <silent> <F10> <ESC>:TlistUpdate<RETURN>
  164. " ShowFunc.vim <-------- 暂时没有使用
  165. " http://www.vim.org/scripts/script.php?script_id=397
  166. " F2打开ShowFunc TagList窗口,显示C/C++函数原型
  167. " map <F2> <Plug>ShowFunc
  168. " map! <F2> <Plug>ShowFunc
  169. " taglist.vim
  170. " http://www.vim.org/scripts/script.php?script_id=273
  171. " <leader>t 打开TagList窗口,窗口在右边
  172. nmap <silent> <leader>t :TlistToggle<cr>
  173. nmap <silent> T :TlistToggle<cr>
  174. "let Tlist_Ctags_Cmd = '/usr/bin/ctags'
  175. let Tlist_Show_One_File = 0
  176. let Tlist_Exit_OnlyWindow = 1
  177. let Tlist_Use_Right_Window = 1
  178. let Tlist_File_Fold_Auto_Close = 1
  179. let Tlist_GainFocus_On_ToggleOpen = 0
  180. let Tlist_WinWidth = s:PlugWinSize
  181. let Tlist_Auto_Open = 0
  182. let Tlist_Display_Prototype = 0
  183. "let Tlist_Close_On_Select = 1
  184. " minibufexpl.vim
  185. " http://www.vim.org/scripts/script.php?script_id=159
  186. " 不需要配置
  187. " OmniCppComplete.vim
  188. " http://www.vim.org/scripts/script.php?script_id=1520
  189. set completeopt=longest,menu
  190. let OmniCpp_ShowPrototypeInAbbr = 1
  191. let OmniCpp_DefaultNamespaces = ["std"] " 逗号分割的字符串
  192. let OmniCpp_MayCompleteScope = 1
  193. let OmniCpp_ShowPrototypeInAbbr = 0
  194. let OmniCpp_SelectFirstItem = 2
  195. " c-j自动补全,当补全菜单打开时,c-j,k上下选择
  196. " 使用c-h代替c-j,避免与latex-suite冲突
  197. imap <expr> <c-h> pumvisible()?"\<C-N>":"\<C-X><C-O>"
  198. imap <expr> <c-k> pumvisible()?"\<C-P>":"\<esc>"
  199. imap <expr> <c-j> pumvisible()?"\<C-N>":"\<C-J>"
  200. imap <expr> <CR> pumvisible()?"\<C-Y>":"\<CR>"
  201. imap <expr> <C-U> pumvisible()?"\<C-E>":"\<C-U>"
  202. " f:文件名补全,l:行补全,d:字典补全,]:tag补全
  203. imap <C-]> <C-X><C-]>
  204. imap <C-F> <C-X><C-F>
  205. imap <C-D> <C-X><C-D>
  206. imap <C-L> <C-X><C-L>
  207. " NERD_commenter.vim
  208. " http://www.vim.org/scripts/script.php?script_id=1218
  209. " Toggle单行注释/“性感”注释/注释到行尾/取消注释
  210. map <leader>cc ,c<space>
  211. map <leader>cs ,cs
  212. map <leader>c$ ,c$
  213. map <leader>cu ,cu
  214. " NERD tree
  215. " http://www.vim.org/scripts/script.php?script_id=1658
  216. let NERDTreeShowHidden = 1
  217. let NERDTreeWinPos = "right"
  218. let NERDTreeWinSize = s:PlugWinSize
  219. nmap <leader>n :NERDTreeToggle<cr>
  220. " DoxygenToolkit.vim
  221. " http://www.vim.org/scripts/script.php?script_id=987
  222. " 暂时没有使用
  223. " 更新ctags和cscope索引
  224. " href: http://www.vimer.cn/2009/10/把vim打造成一个真正的ide2.html
  225. " 稍作修改,提取出DeleteFile函数,修改ctags和cscope执行命令
  226. map <F12> :call Do_CsTag()<cr>
  227. function! Do_CsTag()
  228. let dir = getcwd()
  229. "先删除已有的tags和cscope文件,如果存在且无法删除,则报错。
  230. if ( DeleteFile(dir, "tags") )
  231. return
  232. endif
  233. if ( DeleteFile(dir, "cscope.files") )
  234. return
  235. endif
  236. if ( DeleteFile(dir, "cscope.out") )
  237. return
  238. endif
  239. if(executable('ctags'))
  240. silent! execute "!ctags -R --c++-kinds=+p --fields=+iaS --extra=+q ."
  241. endif
  242. if(executable('cscope') && has("cscope") )
  243. if(g:isWin)
  244. silent! execute "!dir /s/b *.c,*.cpp,*.h,*.java,*.cs >> cscope.files"
  245. else
  246. silent! execute "!find . -iname '*.[ch]' -o -name '*.cpp' > cscope.files"
  247. endif
  248. silent! execute "!cscope -b"
  249. execute "normal :"
  250. if filereadable("cscope.out")
  251. execute "cs add cscope.out"
  252. endif
  253. endif
  254. " 刷新屏幕
  255. execute "redr!"
  256. endfunction
  257. function! DeleteFile(dir, filename)
  258. if filereadable(a:filename)
  259. if (g:isWin)
  260. let ret = delete(a:dir."\\".a:filename)
  261. else
  262. let ret = delete("./".a:filename)
  263. endif
  264. if (ret != 0)
  265. echohl WarningMsg | echo "Failed to delete ".a:filename | echohl None
  266. return 1
  267. else
  268. return 0
  269. endif
  270. endif
  271. return 0
  272. endfunction
  273. " cscope 绑定
  274. if has("cscope")
  275. set csto=1
  276. set cst
  277. set nocsverb
  278. if filereadable("cscope.out")
  279. cs add cscope.out
  280. endif
  281. set csverb
  282. " s: C语言符号 g: 定义 d: 这个函数调用的函数 c: 调用这个函数的函数
  283. " t: 文本 e: egrep模式 f: 文件 i: include本文件的文件
  284. nmap <leader>ss :cs find s <C-R>=expand("<cword>")<CR><CR>
  285. nmap <leader>sg :cs find g <C-R>=expand("<cword>")<CR><CR>
  286. nmap <leader>sc :cs find c <C-R>=expand("<cword>")<CR><CR>
  287. nmap <leader>st :cs find t <C-R>=expand("<cword>")<CR><CR>
  288. nmap <leader>se :cs find e <C-R>=expand("<cword>")<CR><CR>
  289. nmap <leader>sf :cs find f <C-R>=expand("<cfile>")<CR><CR>
  290. nmap <leader>si :cs find i ^<C-R>=expand("<cfile>")<CR>$<CR>
  291. nmap <leader>sd :cs find d <C-R>=expand("<cword>")<CR><CR>
  292. endif
  293. " Quick Fix 设置
  294. map <leader>cw :cw<cr>
  295. map <F3> :cp<cr>
  296. map <F4> :cn<cr>
  297. " lookup file
  298. " http://www.vim.org/scripts/script.php?script_id=1581
  299. " 增加一行(line 295): let pattern = '\c' . a:pattern
  300. " 按F5或者;ff打开文件搜索窗口
  301. let g:LookupFile_MinPatLength = 0
  302. let g:LookupFile_PreserveLastPattern = 0
  303. let g:LookupFile_PreservePatternHistory = 0
  304. let g:LookupFile_AlwaysAcceptFirst = 1
  305. let g:LookupFile_AllowNewFiles = 0
  306. if filereadable("./filenametags")
  307. let g:LookupFile_TagExpr = '"./filenametags"'
  308. endif
  309. nmap <silent> <leader>ff :LookupFile<cr>
  310. function! LookupFile_IgnoreCaseFunc(pattern)
  311. let _tags = &tags
  312. try
  313. let &tags = eval(g:LookupFile_TagExpr)
  314. let newpattern = '\c' . a:pattern
  315. let tags = taglist(newpattern)
  316. catch
  317. echohl ErrorMsg | echo "Exception: " . v:exception | echohl NONE
  318. return ""
  319. finally
  320. let &tags = _tags
  321. endtry
  322. " Show the matches for what is typed so far.
  323. let files = map(tags, 'v:val["filename"]')
  324. return files
  325. endfunction
  326. let g:LookupFile_LookupFunc = 'LookupFile_IgnoreCaseFunc'
  327. " Buffers Explorer (需要genutils.vim)
  328. " http://vim.sourceforge.net/scripts/script.php?script_id=42
  329. " http://www.vim.org/scripts/script.php?script_id=197
  330. let g:bufExplorerDefaultHelp=0 " Do not show default help.
  331. let g:bufExplorerShowRelativePath=1 " Show relative paths.
  332. let g:bufExplorerSortBy='mru' " Sort by most recently used.
  333. let g:bufExplorerSplitRight=0 " Split left.
  334. let g:bufExplorerSplitVertical=1 " Split vertically.
  335. let g:bufExplorerSplitVertSize = s:PlugWinSize " Split width
  336. let g:bufExplorerUseCurrentWindow=1 " Open in new window.
  337. autocmd BufWinEnter \[Buf\ List\] setl nonumber
  338. "nmap <silent> <Leader>b :BufExplorer<CR>
  339. nmap <silent> F :BufExplorer<CR>
  340. " vim latex suite
  341. " http://vim-latex.sourceforge.net/index.php?subject=download&title=Download
  342. " 使grep总是生成文件名
  343. set grepprg=grep\ -nH\ $*
  344. " vim默认把空的tex文件设为plaintex而不是tex,导致latex-suite不被加载
  345. let g:tex_flavor='latex'
  346. set iskeyword+=:
  347. autocmd BufEnter *.tex set sw=2
  348. " java complete
  349. " http://www.vim.org/scripts/script.php?script_id=1189
  350. autocmd Filetype java setlocal omnifunc=javacomplete#Complete
  351. "autocmd Filetype java setlocal completefunc=javacomplete#CompleteParamsInfo
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值