1. 程式人生 > >VIM 的配置和神一般的外掛。

VIM 的配置和神一般的外掛。

       剛開始工作的時候,主要是C和C++,使用VIM比較多,後來轉到Android,主要使用IDE開發,VIM用的比較少,但是也有用到VIM的時候,總是遇到這樣那樣的問題,自動補全,檔案查詢,搜尋什麼的,然後就是各種外掛,外掛多了,衝突的問題就來了,問題是外掛也不能解決一些問題,搞的很糾結。下決心苦學一番vim,看來我不是那種傳說中的天賦異常的武林高手,折騰了一頓,沒有什麼結果,偃旗息鼓了。最近心血來潮,重新配置了一下vimrc, 終於有了一個滿意的vimrc。
      一. 基本的配置:

  1. syn on "語法高亮
  2. set helplang=cn     "使用中文幫助文件
  3. set backspace=2
  4. set tabstop=4 "製表符的寬度
  5. set softtabstop=4
  6. set shiftwidth=4 "縮排的空格
  7. set autoindent     "自動縮排
  8. set cindent "C 外掛
  9. set number "設定行號
  10. set ignorecase "忽略大小寫 (查詢字串時)
  11. set nohlsearch "高亮顯示 (查詢字串是,找到後高亮顯示)
  12. set mouse=a "使用滑鼠
  13. set ruler     "在右下角顯示游標位置
  14. set showcmd     "顯示未敲完的命令
  15. set cmdheight=1 "設定命令列的行數為 1
  16. set laststatus=2 "顯示狀態列 (預設值為 1, 無法顯示狀態列)
  17. set incsearch "在輸入搜尋的字元串同時就開始搜尋已經輸入的部分
  18. set nowrap "一行就一行,別弄到第二行去
  19. set sidescroll=10     "螢幕放不下時,按一次螢幕移動一個字元    
  20. set whichwrap=b,s,<,>,[,]     "跨行移動
  21. set fileformats=unix,dos
  22. set cursorline "突出顯示當前行
  23. set showmatch "插入括號時,短暫地跳轉到匹配的對應括號
  24. set matchtime=2 "短暫跳轉到匹配括號的時間
  25. set smartindent "開啟新行時使用智慧自動縮排
  26. filetype plugin indent on "自動識別檔案型別,用檔案型別plugin指令碼,使用縮排定義檔案
  27. "set autochdir
映射了一些快捷鍵,模仿windows 的得文字操作:

點選(此處)摺疊或開啟

  1. "貼上複製的一些操作
  2. vmap "+y "選中狀態下 Ctrl+c 複製
  3. nmap "+yy "選中狀態下 Ctrl+c 複製
  4. nmap "+p "正常模式下貼上
  5. nmap ggvG "正常模式下全選
  6. vmap <C-x> dd<Esc> "正常模式下DEL
  7. nmap s :call SaveFile()
  8. imap s :call SaveFile()
  9. vmap s :call SaveFile()
  10. func! SaveFile()
  11.      exec "w
一些小技巧

點選(此處)摺疊或開啟

  1. "記憶上次開啟檔案的位置
  2. if has("autocmd")
  3.     autocmd BufRead *.txt set tw=78
  4.     autocmd BufReadPost * if line("'\"") > 0 && line("'\"") <= line("$") | exe "normal g'\"
  5. autocmd BufWritePre * :%s/\s+\+$//e    "自動去除行尾空格 這個在gerrit 檢查程式碼比較有用。
  6. set fileencodings=ucs-bom,utf-8,GB18030,GBK,GB2312                                                 "字元編碼支援中文

二. 外掛管理--Vundle
    vim下有各種各樣的外掛,每次換機器,都要重新配置一遍,後來發現了外掛管理神奇 Vundle。
下載Vundle: git clone https://github.com/gmarik/vundle.git ~/.vim/bundle/vundle

配置 vundle 。在vundle 中的外掛主要分三類:
1.vimrc-script 網站上的外掛    "Bundle 'minibufexpl.vim'
2.github 上的外掛。               "Bundle 'fholgado/minibufexpl.vim'
3.自定義git上的外掛。            "Bundle 'https://github.com/Lokaltog/powerline'
其實第二種也可以採用第三種方式管理,原理是一樣的,都是git管理。


在vimrc 配置完外掛以後,vundle 可以自動下載外掛,自動更新。
重啟vim,在vim執行命令::BundleInstall  自動安裝外掛。
在vimrc 中配置:map :!git clone https://github.com/gmarik/vundle.git  ~/.vim/bundle/vundle
連Vundle 也自動下載。

點選(此處)摺疊或開啟

  1. """""""""""""""""""""""""""""""""""VUNDLE setting"""""""""""""""""""""""""""""""""""""""""""
  2. set nocompatible " be iMproved
  3. filetype off "
  4.     set rtp+=~/.vim/bundle/vundle/
  5. call vundle#rc()
  6.     " let Vundle manage Vundle
  7.     Bundle 'gmarik/vundle'
  8.     " vim-scripts repos
  9.     "Bundle 'minibufexpl.vim'
  10.     "Bundle 'bufexplorer.zip'
  11.     "Bundle 'taglist.vim'
  12.     "Bundle 'c.vim'
  13.     "Bundle 'winmanager'
  14.     "Bundle 'The-NERD-Commenter'
  15.     "Bundle 'snipMate'
  16.     "Bundle 'TxtBrowser'
  17.     "Bundle 'genutils'
  18.     "Bundle 'lookupfile'
  19.     "Bundle 'LeaderF'
  20.     Bundle 'The-NERD-tree'
  21.     "Bundle 'ctrlp.vim'
  22.     "Bundle 'cscope.vim'
  23.     "Bundle 'Tagbar'
  24.     "github repos.
  25.     "Bundle 'fholgado/minibufexpl.vim'
  26.     "Bundle 'bling/vim-airline'
  27.     "Bundle 'tpope/vim-fugitive'
  28.     "Bundle 'Shougo/neocomplcache.vim'
  29.     "Bundle 'ervandew/supertab'
  30.     "Bundle 'simplyzhao/cscope_maps.vim'
  31.     Bundle 'vim-scripts/EasyGrep'
  32.     Bundle 'kien/ctrlp.vim'
  33.     Bundle 'majutsushi/tagbar'
  34.     Bundle 'brookhong/cscope.vim'
  35.     Bundle 'scrooloose/nerdtree'
  36.     Plugin 'Valloric/YouCompleteMe'
  37.     "file repos or git repos
  38.     "Bundle 'git://git.wincent.com/command-t.git'
  39.     "Bundle 'https://github.com/Lokaltog/powerline'
  40.     filetype plugin indent on " required!
  41.     "
  42.     " Brief help
  43.     " :BundleList - list configured bundles
  44.     " :BundleInstall(!) - install(update) bundles
  45.     " :BundleSearch(!) foo - search(or refresh cache first) for foo
  46.     " :BundleClean(!) - confirm(or auto-approve) removal of unused bundles
  47.     "
  48.     " see :h vundle for more details or wiki for FAQ
  49.     " NOTE: comments after Bundle command are not allowed..
  50. """"""""""""""""""""""""END VUNDLE""""""""""""""""""""""""""

三。Tag  Tagbar 外掛
    在傳統的vim 配置中,瀏覽檔案中的變數,函式都是藉助tags 和Taglist 外掛。你out 了,vimer 們總有給人想不到的驚喜,Tagbar 橫空出世。
TagBar 比Taglist 有更清晰的顯示層次結構, 在色彩上也更豐富。對C++ 的支援更友好。git 地址:https://github.com/majutsushi/tagbar。Taglist 可以安然退休了。
在vimr 中對映:nnoremap :TagbarToggle
screenshot1

在TagBar 視窗按F1 彈出幫助:

四. 檔案查詢
    檔案查詢傳統的外掛有fuzzyfind lookup等。Sublime的快捷鍵Ctrl  P 深得程式猿的好評,vim 不能讓sublime 獨美,推出了CtrlP .傳統的查詢方式都是切換另外一個shell,find 。現在直接搞定。
ctrlp 的設計思想很先進,能夠根據.git 目錄自動確定根目錄位置,自動確定查詢範圍,支援路徑,檔名。
git 地址:https://github.com/kien/ctrlp.vim
這裡有一篇視訊講解:http://zuyunfei.com/2013/08/26/vim-plugin-ctrlp/ 
不過還像又有人有意見了:https://github.com/Yggdroot/LeaderF    號稱效能比crtlp 更快。

點選(此處)摺疊或開啟

  1. """""""""""""""""""""""""""ctrlp"""""""""""""""""""""""""""
  2. let g:ctrlp_working_path_mode = 'ra'
  3. let g:ctrlp_by_filename = 0
  4. let g:ctrlp_open_multiple_files = 'v'
  5. set wildignore+=*/tmp/*,*.so,*.swp,*.zip " MacOSX/Linux
  6. set wildignore+=*\\tmp\\*,*.swp,*.zip,*.exe " Windows
  7. let g:ctrlp_custom_ignore = {
  8.   \ 'dir': '\v[\/]\.(git|hg|svn)$',
  9.   \ 'file': '\v\.(exe|so|dll)$',
  10.   \ 'link': 'some_bad_symbolic_links',
  11.   \ }
  12. """""""""""""""""""""END ctrlp"""""""""""""""""""""""""""""


五. grep  
   vim自帶Grep 功能。EasyGrep 基於vim的grep 功能,不受作業系統限制,Windows下的vimer的幸福了。

EasyGrep uses Vim's leader key, which is by default '\'.  For information on
this key, type ":help mapleader".
vv  - Grep for the word under the cursor, match all occurences, like 'g*'.  See ":help gstar".
vV  - Grep for the word under the cursor, match whole word, like '*'.  See ":help star".
va  - Like vv, but add to existing list.
vA  - Like vV, but add to existing list.
vr  - Perform a global search on the word under the cursor and prompt for a pattern with which to replace it.
vR  - Like vr, but match whole word.

vo 開啟設定,可以對EasyGrep進行一些設定。

vo  - Open an options explorer to select the files to search in and set grep options.
可配置搜尋模式:
*EasyGrepMode*  [default=0]
Specifies the mode in which to start.
0 - All files
1 - Open Buffers
2 - Track the current extension
3 - User mode -- Use a custom, on demand set of extensions
最好用的是2,根據檔案字尾匹配搜尋檔案,模式0一直用不出來,不知道是不是Winddows的原因。




6.NerdTree 外掛
vim自帶視窗瀏覽外掛,不過NerdTree功能更強大,更方便,更好用。
vim 中命令列輸入 :NERDTree 顯示如下。然後按? 可以顯示幫助,是不是很貼心。






<script>window._bd_share_config={"common":{"bdSnsKey":{},"bdText":"","bdMini":"2","bdMiniList":false,"bdPic":"","bdStyle":"0","bdSize":"16"},"share":{}};with(document)0[(getElementsByTagName('head')[0]||body).appendChild(createElement('script')).src='http://bdimg.share.baidu.com/static/api/js/share.js?v=89860593.js?cdnversion='+~(-new Date()/36e5)];</script> 閱讀(4153) | 評論(2) | 轉發(8) | 給主人留下些什麼吧!~~ 12_avatar_small.jpg

2015-01-27 14:51:46

博主,您好,能不能將你的完整的VIM的配置檔案給我一份。謝謝
([email protected])

回覆 | 舉報 75_avatar_small.jpg

2015-01-26 17:42:02

文明上網,理性發言...63.gif

回覆 | 舉報 評論熱議