vim插件和配置

vim插件和配置

插件

pathogen 能夠方便地管理vim插件

在沒有pathogen的狀況下,vim插件的文件所有都放在.vim目錄,卸載插件很麻煩,pathogen能夠將不一樣的插件放在一個單獨的目錄下,安裝插件只須要將插件的文件夾複製到制定文件夾(bundle)內,卸載插件時只須要刪除文件夾,方便好用。python

如下插件均可以採用「下載zip包到vundle文件夾,解壓zip包」的方法進行安裝c++

下載及安裝https://github.com/tpope/vim-pathogengit

nerdtree 用於查看目錄樹

下載https://github.com/scrooloose/nerdtreegithub

tabular 用於格式化代碼

下載https://github.com/godlygeek/tabularvim

Tagbar 查看代碼結構

下載https://github.com/majutsushi/tagbarui

airline 用於設置vim主題

下載https://github.com/bling/vim-airlinethis

molokai 用於設置vim主題

下載https://github.com/tomasr/molokaispa

mru 保存最近打開的文件

下載https://github.com/vim-scripts/mru.vimprototype

easymotion 用於搜索

下載https://github.com/easymotion/vim-easymotion插件

ctrlp 強大的搜索插件

下載https://github.com/kien/ctrlp.vim

AutoComplPop 用於自動補全

下載https://github.com/vim-scripts/AutoComplPop

omnicppcomplete 用於自動補全

下載https://github.com/vim-scripts/OmniCppComplete

配置

基本配置

按鍵映射

按鍵映射的配置都寫在.vimrc中,.vimrc放在主目錄

imap jk <ESC\>              " 按jk退出編輯狀態,不再用小指頭按Esc了
map wq <ESC>:wq<CR>         " 按wq就直接保存退出
imap wq <ESC>:wq<CR>        " 按wq就直接保存退出
map qq <ESC>:q!<CR>         " 按qq就直接不保存退出
imap qq <ESC>:q!<CR>        " 按qq就直接不保存退出
map <F2> <ESC><C-W><C-W>    " 按F2進行窗口切換,不按<C-W><C-W>
map <F3> <ESC>:Tagbar<CR>   " 按F3打開代碼結構
map <F4> <ESC>:NERDTreeToggle<CR>
map <F12> <ESC>:!ctags -R --c++-kinds=+px --fields=+aiKSz --extra=+q .<CR>  " 按F12生成tag

ctrlp按鍵映射

map ,pp <ESC>:CtrlP<CR>             "搜索文件
map ,pd <ESC>:CtrlPDir<CR>          "搜索文件夾
map ,pu <ESC>:CtrlPUndo<CR>         "搜索修改記錄,能夠回退
map ,pl <ESC>:CtrlPLine<CR>         "搜索行
map ,pc <ESC>:CtrlPChangeAll<CR>    "搜索修改記錄,能夠定位光標
map ,pm <ESC>:CtrlPMRU<CR>          "搜索最近修改的文件

補全

vim中的自動補全很麻煩,可是寫代碼沒有自動補全徹底沒法接受。爲了實現自動補全,我一開始使用omnicppcomplete和AutoComplPop,後來據說有個牛逼的插件叫作YouCompleteMe,能夠進行語法分析,實現自動補全,因而我就去試試。咱們的開發機沒法訪問外網,也不能直接用root用戶下直接安裝軟件,安裝YouCompleteMe確實麻煩。我前後用源碼編譯安裝了cmake、llvm、python、vim和YouCompleteMe,花了很多時間,終於把YouCompleteMe裝好了,不過使用的時候,補全仍是不夠智能,沒有想象中那麼好。後來搬到了新的開發機,不想再從新安裝YouCompleteMe了,從新回到omnicppcomplete和AutoComplPop。最後我發現,omnicppcomplete和AutoComplPop實現的補全效果比YouCompleteMe好。幾乎每一個vim使用者都會面臨自動補全的功能,在這裏,我建議使用omnicppcomplete和AutoComplPop組合,簡單方便,效果又好。

由於項目是使用blade build編譯,BUILD記錄了項目的依賴,因而我寫了一個python腳本去分析BUILD文件,獲取相應的依賴,再根據依賴去生成tag,提升代碼補全能力。最終,vim的代碼補全能力也不錯!使用方法以下:

  1. 安裝ctags、omnicppcomplete和AutoComplPop
  2. 在vim中添加一句配置:set tags+=./deps_tags
  3. 在BUILD所在目錄下運行deps_tag.py腳本,根據依賴生成deps_tags
  4. 按F12生成本項目的tags
  5. 愉快地使用補全
  6. 這裏其實能夠作到每次保存文件都自動從新生成tags,可是由於tags是根據所在工做路徑的,若是不是在項目路徑就會生成不必的tags

最後,歡迎使用個人vim配置

" set coding
set termencoding=utf-8
set encoding=utf-8
set fileencoding=utf-8
set fenc=utf-8 
set fencs=utf-8,usc-bom,euc-jp,gb18030,gbk,gb2312,cp936 

set backspace=indent,eol,start
set nocompatible                " be iMproved, required
set mouse=a                     " mouse useful
set nu                          " set number
set nocp                        
set nobackup                    " no backup
set noerrorbells                " no voice
set showmatch                   " show match of () {} []
set ignorecase                  " ignore case in search
set cursorline                  " hightlight current line
set cursorcolumn                " hightlight current collum
"set autochdir                  " auto change dir

set autoindent                  "  autoindent 是自動縮進, cindent是特別針對C語言語法自動縮進
set smartindent 
set cindent 
set tabstop=4                   " 製表符爲4 
set softtabstop=4 
set shiftwidth=4 

set scrolloff=3                 " 光標移動到buffer的頂部和底部時保持3行距離 
set cmdheight=2                 " 命令行(在狀態行下)的高度,默認爲1,這裏是2
set statusline=%F%m%r%h%w[%p%%] " 個人狀態行顯示的內容
set laststatus=2                " 老是顯示狀態行 

syntax on                       " 語法高亮

" key map
imap jk <ESC>                   " 按jk退出編輯狀態
map wq <ESC>:wq<CR>
imap wq <ESC>:wq<CR>
map qq <ESC>:q!<CR>
imap qq <ESC>:q!<CR>
map cp <ESC>:set mouse=c<CR>:set nonu<CR>
map ncp <ESC>:set mouse=a<CR>:set nu<CR>
map ,pa <ESC>:set paste<CR>
map ,np <ESC>:set nopaste<CR>
map gn <ESC>:tabnext<CR>
map gm <ESC><C-W><C-W>

map <F2> <ESC><C-W><C-W>
map <F3> <ESC>:Tagbar<CR>
map <F4> <ESC>:NERDTreeToggle<CR>
map <F12> <ESC>:!ctags -R --c++-kinds=+px --fields=+aiKSz --extra=+q .<CR> 

"pathogen
filetype off                    " required
call pathogen#infect()
filetype plugin on

"theme
syntax enable
set t_Co=256
let g:airline_theme="dark"
set background=dark
colorscheme molokai


"let g:AutoOpenWinManager = 1
let g:NERDTreeDirArrows = 1
let g:nerdtree_tabs_autoclose = 0
let g:bufExplorerSplitVertSize = 30
let g:bufExplorerMaxHeight=30
let g:winManagerWindowLayout = "FileExplorer|BufExplorer"
let g:winManagerWidth = 30


"easymotion
map  / <Plug>(easymotion-sn)
omap / <Plug>(easymotion-tn)
map  n <Plug>(easymotion-next)
map  N <Plug>(easymotion-prev)
map <Leader>l <Plug>(easymotion-lineforward)
map <Leader>j <Plug>(easymotion-j)
map <Leader>k <Plug>(easymotion-k)
map <Leader>h <Plug>(easymotion-linebackward)
let g:EasyMotion_use_upper = 1          " Use uppercase target labels and type as a lower case
let g:EasyMotion_smartcase = 1          " type `l` and match `l`&`L`
let g:EasyMotion_use_smartsign_us = 1   " Smartsign (type `3` and match `3`&`#`)

"easygrep
let g:EasyGrepMode = 2                  " All:0, Open Buffers:1, TrackExt:2
"let g:EasyGrepCommand = 0              " Use vimgrep:0, grepprg:1
let g:EasyGrepRecursive  = 1            " Recursive searching
let g:EasyGrepIgnoreCase = 1            " not ignorecase:0
let g:EasyGrepFilesToExclude = "*.bak, *~, cscope.*, *.a, *.o, *.pyc, *.bak"
"let g:EasyGrepOpenWindowOnMatch = 0

"ctrlp
let g:ctrlp_by_filename = 1
let g:ctrlp_working_path_mode = '0'
"let g:ctrlp_cmd = 'CtrlPMixed'
let g:ctrlp_match_window = 'bottom,order:ttb,min:1,max:10,results:20'
"let g:ctrlp_regexp = 0
let g:ctrlp_extensions = ['buffertag', 'dir', 'undo', 'line', 'changes', 'mixed', 'bookmarkdir']
map ,pp <ESC>:CtrlP<CR>
map ,pd <ESC>:CtrlPDir<CR>
map ,pu <ESC>:CtrlPUndo<CR>
map ,pl <ESC>:CtrlPLine<CR>
map ,pc <ESC>:CtrlPChangeAll<CR>
map ,pb <ESC>:CtrlPBookmarkDir<CR>
map ,pm <ESC>:CtrlPMRU<CR>


"acp
let g:acp_enableAtStartup = 1
let g:acp_completeoptPreview = 0
let g:acp_ignorecaseOption = 1
let g:acp_behaviorKeywordCommand = "\<C-p>"
let g:acp_behaviorKeywordLength = 1

"omnicppcomplete 
let OmniCpp_MayCompleteDot = 1      " autocomplete with .
let OmniCpp_MayCompleteArrow = 1    " autocomplete with ->
let OmniCpp_MayCompleteScope = 1    " autocomplete with ::
let OmniCpp_SelectFirstItem = 2     " select first item
let OmniCpp_NamespaceSearch = 2     " search namespaces in this and included files
let OmniCpp_ShowPrototypeInAbbr = 1 " show function prototype in popup window
let OmniCpp_GlobalScopeSearch=1     " enable the global scope search
let OmniCpp_DisplayMode=1           " Class scope completion mode: always show all members
let OmniCpp_DefaultNamespaces=["std"]

" au CursorMovedI,InsertLeave * if pumvisible() == 0|silent! pclose|endif  " 自動關閉補全窗口 
set completeopt=menu,longest
set tags+=./deps_tags
相關文章
相關標籤/搜索