使用vim打造IDE, 針對C語言開發者建議使用gvim 先上兩個截圖 # 安裝ctags1. 下載地址: http://ctags.sourceforge.net/# 安裝cscope1. 下載地址: http://cscope.sourceforge.net/ 2. 修改源碼,使其支持遞歸搜索文 ...
================================
使用vim打造IDE, 針對C語言開發者
建議使用gvim
================================
先上兩個截圖
# 安裝ctags
1. 下載地址: http://ctags.sourceforge.net/
# 安裝cscope
1. 下載地址: http://cscope.sourceforge.net/
2. 修改源碼,使其支持遞歸搜索文件夾的軟鏈接
修改文件: dir.c
修改方式: 替換函數調用 lstat 全部替換為 stat
3. 編譯源碼可能出現的錯誤
錯誤: fatal error: curses.h: No such file or directory
解決: sudo apt install libncurses5-dev libncursesw5-dev
# 安裝ruby, command-t插件會用到
sudo apt install ruby ruby-dev
# 安裝vim, vim-gtk
sudo apt install vim vim-gtk
# 在home目錄下創建 .vimrc 並編輯
1. 將附錄1中 vimrc 的內容拷貝進去
# 在home目錄下創建 .vim 目錄
1. 進入 .vim 目錄
2. 創建目錄 autoload bundle colors syntax
# 在 ~/.vim/colors 目錄中創建 mycolor.vim 並編輯
1. 將附錄2中 mycolor.vim 的內容拷貝進去
# 在 ~/.vim/syntax 目錄中創建 c.vim 並編輯
1. 將附錄3中 c.vim 的內容拷貝進去
# 下載插件 vundle 到 ~/.vim/bundle
1. git clone https://github.com/VundleVim/Vundle.vim.git ~/.vim/bundle/Vundle.vim
2. 需要安裝 git
3. vundle 可以自動安裝和更新其他vim插件
# 下載插件 pathogen 到 ~/.vim/autoload
1. curl -LSso ~/.vim/autoload/pathogen.vim https://tpo.pe/pathogen.vim
2. 需要安裝 curl
3. pathogen 可以自動載入插件
# 安裝其餘 vim 插件
1. 打開 vim 或 gvim
2. 執行 :PluginInstall
# 編譯 command-t插件
1. 進入 ~/.vim/bundle/command-t/ruby/command-t目錄
2. 執行 ruby extconf.rb
3. 執行 make
# 編譯 YouCompleteMe 插件
1. 進入 ~/.vim/bundle/YouCompleteMe
2. 執行 ./install.py --clang-completer
3. 需要安裝 cmake
# 創建 tag 生成和高亮腳本
1. 找一個地方創建 htags.sh 文件,註意同時修改 .vimrc 中該腳本的路徑
2. 將附錄4中 htags.sh 的內容拷貝進去
3. 給htags.sh增加執行許可權 chmod u+x htags.sh
# 生成 ctags 和 cscope 的標簽並高亮
1. 在工程的根目錄打開 gvim 或 vim
2. 使用快捷鍵 \bt 創建 ctags的標簽
3. 使用快捷鍵 \bc 創建 cscope的標簽
4. 使用快捷見 \ht 對重新高亮標簽
*每次啟動vim時會自動導入一次, 如果沒有
重新生成標簽就不要重新導入
# 使用YouCompleteMe的自動補全功能
1. 在工程的根目錄或創建 .ycm_extra_conf.py
2. 將附錄5中 .ycm_extra_conf.py 內容拷貝到其中
3. 根據工程修改其中的頭文件路徑
附錄1 .vimrc
"===================通用配置====================== "文件搜索路徑 set path=.,/usr/include,, " 控制 set nocompatible "關閉vi相容 filetype off "關閉文件類型偵測,vundle需要 set fileencodings=utf-8,gbk "使用utf-8或gbk編碼方式 syntax on "語法高亮 set backspace=2 "退格鍵正常模式 set whichwrap=<,>,[,] "當游標到行首或行尾,允許左右方向鍵換行 set autoread "文件在vim外修改過,自動重載 set nobackup "不使用備份 set confirm "在處理未保存或只讀文件時,彈出確認消息 set scrolloff=3 "游標移動到距離頂部或底部開始滾到距離 set history=1000 "歷史記錄數 set mouse= "關閉滑鼠 set selection=inclusive "選擇包含最後一個字元 set selectmode=mouse,key "啟動選擇模式的方式 set completeopt=longest,menu "智能補全,彈出菜單,無歧義時才自動填充 set noswapfile "關閉交換文件 set hidden "允許在有未保存的修改時切換緩衝區 "顯示 colorscheme mycolor "選擇配色方案 set t_Co=256 "可以使用的顏色數目 set number "顯示行號 set laststatus=2 "顯示狀態行 set ruler "顯示標尺 set showcmd "顯示輸入的命令 set showmatch "高亮括弧匹配 set matchtime=1 "匹配括弧高亮的時間(十分之一秒) set matchpairs={:},(:) "匹配括弧"{}""()" set hlsearch "檢索時高亮匹配項 set incsearch "邊檢索邊顯示匹配 set go-=T "去除gvim的toolbar "格式 set noexpandtab "不要將tab轉換為空格 set shiftwidth=4 "自動縮進的距離,也是平移字元的距離 set tabstop=4 "tab鍵對應的空格數 set autoindent "自動縮進 set smartindent "智能縮進 "===================按鍵映射====================== "按鍵映射的起始字元 let mapleader = '\' "使用Ctrl-l 和 Ctrl+h 切換標簽頁 nnoremap <C-l> gt nnoremap <c-h> gT "在行末加上分號 nnoremap <silent> <Leader>; :<Esc><End>a<Space>;<Esc><Down> "保存 nnoremap <C-s> :w<CR> "替換 nnoremap <C-h> :%s/<C-R>=expand("<cword>")<CR>/<C-R>=expand("<cword>")<CR> "===================插件管理====================== " 下載vundle " git clone https://github.com/VundleVim/Vundle.vim.git ~/.vim/bundle/Vundle.vim " 下載pathogen " curl -LSso ~/.vim/autoload/pathogen.vim https://tpo.pe/pathogen.vim " 將vundle加入到runtime path set rtp+=~/.vim/bundle/Vundle.vim " 下載到bundle目錄的插件 call vundle#begin() " plugin on GitHub repo Plugin 'VundleVim/Vundle.vim' Plugin 'scrooloose/nerdtree' Plugin 'Lokaltog/vim-powerline.git' Plugin 'wincent/command-t' Plugin 'Valloric/YouCompleteMe' Plugin 'tomtom/tlib_vim' Plugin 'tomtom/viki_vim' " plugin from http://vim-scripts.org/vim/scripts.html Plugin 'taglist.vim' Plugin 'EasyGrep' " Git plugin not hosted on GitHub " Plugin 'git://...' " git repos on your local machine " Plugin 'file://...' call vundle#end() filetype plugin indent on " required "===================插件配置====================== "-----pathogen----- execute pathogen#infect() "-----NERDTree----- let g:NERDTreeCaseSensitiveSort = 1 let g:NERDTreeWinSize = 25 let g:NERDTreeWinPos = "right" nnoremap <silent> <Leader>t :NERDTreeToggle<CR> nnoremap <silent> <Leader>o :NERDTreeFind<CR> "-----Powerline----- set fillchars+=stl:\ ,stlnc:\ let g:Powerline_symbols = 'compatible' let g:Powerline_stl_path_style = 'filename' "只顯示文件名 "-----Command-T----- let g:CommandTFileScanner = 'ruby' "使用ruby作為文件瀏覽器 let g:CommandTTraverseSCM = 'dir' "根目錄為執行vim時所在的目錄 "打開文件跳轉 nnoremap <silent> <Leader>f :CommandT<CR> "-----taglist----- let Tlist_Show_One_File = 1 "只顯示當前文件的taglist let Tlist_Exit_OnlyWindow = 1 "taglist是最後一個視窗時退出vim let Tlist_Use_Left_Window = 1 "在左側視窗顯示taglist let Tlist_GainFocus_On_ToggleOpen = 1 "打開taglist時,游標停在taglist視窗 let Tlist_Auto_Update = 1 "自動更新 " 打開標簽瀏覽器 nnoremap <silent><Leader>dt :Tlist<CR> " 重新生成標簽 nnoremap <silent><Leader>bt :!~/Myfiles/Tool/sh/ctags/hitags.sh<CR> " 高亮標簽 nnoremap <silent><Leader>ht :so tags.vim<CR> "-----cscope----- "載入cscope庫 if filereadable("cscope.out") cs add cscope.out endif set cscopequickfix=s-,c-,d-,i-,t-,e- "使用quickfix視窗顯示結果 set cst "跳轉時也使用cscope庫 "打開引用視窗 nnoremap <silent><Leader>cw :cw<CR> "重新生成索引文件 nnoremap <silent><Leader>bc :!cscope -Rbq<CR> "s: 查找本C符號 "g: 查找本定義 "d: 查找本函數調用的函數 "c: 查找調用本函數的函數 "t: 查找本字元串 "e: 查找本egrep模式 "f: 查找本文件 "i: 查找包含本文件的文件 nnoremap <C-\>s :scs find s <C-R>=expand("<cword>")<CR><CR> nnoremap <C-\>g :scs find g <C-R>=expand("<cword>")<CR><CR> nnoremap <C-\>c :scs find c <C-R>=expand("<cword>")<CR><CR> nnoremap <C-\>t :scs find t <C-R>=expand("<cword>")<CR><CR> nnoremap <C-\>e :scs find e <C-R>=expand("<cword>")<CR><CR> nnoremap <C-\>f :scs find f <C-R>=expand("<cfile>")<CR><CR> nnoremap <C-\>i :scs find i <C-R>=expand("<cfile>")<CR><CR> nnoremap <C-\>d :scs find d <C-R>=expand("<cword>")<CR><CR> "-----YouCompleteMe----- let g:ycm_server_python_interpreter= '/usr/bin/python2' let g:ycm_global_ycm_extra_conf = '~/.ycm_extra_conf.py' "預設配置文件 let g:ycm_key_invoke_completion = '<C-Tab>' "跨文件補全 let g:ycm_confirm_extra_conf = 0 "關閉載入配置文件提示 let g:ycm_cache_omnifunc = 0 "關閉補全緩存 let g:ycm_enable_diagnostic_signs = 0 "關閉診斷提示符 let g:ycm_enable_diagnostic_highlighting = 1 "關閉診斷高亮 "let g:ycm_show_diagnostics_ui = 0 "關閉診斷ui let g:ycm_min_num_of_chars_for_completion = 3 "n字元開始自動補全 "獲取變數類型 nnoremap <silent><Leader>yt :YcmCompleter GetType<CR> "跳轉定義或聲明 nnoremap <silent><Leader>yg :YcmCompleter GoTo<CR> "跳轉包含文件 nnoremap <silent><Leader>yi :YcmCompleter GoToInclude<CR> "打開診斷信息 nnoremap <silent><Leader>yd :YcmDiags<CR> "-----EasyGrep----- let EasyGrepMode = 2 "根據文件類型搜索相應文件 let EasyGrepRecursive = 1 "遞歸搜索 let EasyGrepCommand = 1 "使用grep let EasyGrepJumpToMatch = 0 "不要跳轉
附錄2 mycolor.vim
" Vim color file " Maintainer: Hans Fugal <[email protected]> " Last Change: $Date: 2004/06/13 19:30:30 $ " Last Change: $Date: 2004/06/13 19:30:30 $ " URL: http://hans.fugal.net/vim/colors/desert.vim " Version: $Id: desert.vim,v 1.1 2004/06/13 19:30:30 vimboss Exp $ " cool help screens " :he group-name " :he highlight-groups " :he cterm-colors set background=dark if version > 580 " no guarantees for version 5.8 and below, but this makes it stop " complaining hi clear if exists("syntax_on") syntax reset endif endif let g:colors_name="desert" hi Normal guifg=White guibg=grey20 " highlight groups hi Cursor guibg=khaki guifg=slategrey "hi CursorIM "hi Directory "hi DiffAdd "hi DiffChange "hi DiffDelete "hi DiffText "hi ErrorMsg hi VertSplit guibg=#c2bfa5 guifg=grey50 gui=none hi Folded guibg=grey30 guifg=gold hi FoldColumn guibg=grey30 guifg=tan hi IncSearch guifg=slategrey guibg=khaki "hi LineNr hi ModeMsg guifg=goldenrod hi MoreMsg guifg=SeaGreen hi NonText guifg=LightBlue guibg=grey30 hi Question guifg=springgreen hi Search guibg=peru guifg=wheat hi SpecialKey guifg=yellowgreen hi StatusLine guibg=#c2bfa5 guifg=black gui=none hi StatusLineNC guibg=#c2bfa5 guifg=grey50 gui=none hi Title guifg=indianred hi Visual gui=none guifg=khaki guibg=olivedrab "hi VisualNOS hi WarningMsg guifg=salmon "hi WildMenu "hi Menu "hi Scrollbar "hi Tooltip " syntax highlighting groups hi Comment guifg=SkyBlue hi Constant guifg=#ffa0a0 hi Identifier guifg=palegreen hi Statement guifg=khaki hi PreProc guifg=indianred hi Type guifg=darkkhaki hi Special guifg=navajowhite "hi Underlined hi Ignore guifg=grey40 "hi Error hi Todo guifg=orangered guibg=yellow2 " color terminal definitions hi SpecialKey ctermfg=darkgreen hi NonText cterm=bold ctermfg=darkblue hi Directory ctermfg=darkcyan hi ErrorMsg cterm=bold ctermfg=7 ctermbg=1 hi IncSearch cterm=NONE ctermfg=yellow ctermbg=green hi Search cterm=NONE ctermfg=grey ctermbg=blue hi MoreMsg ctermfg=darkgreen hi ModeMsg cterm=NONE ctermfg=brown hi LineNr ctermfg=3 hi Question ctermfg=green hi StatusLine cterm=bold,reverse hi StatusLineNC cterm=reverse hi VertSplit cterm=reverse hi Title ctermfg=5 hi Visual cterm=reverse hi VisualNOS cterm=bold,underline hi WarningMsg ctermfg=1 hi WildMenu ctermfg=0 ctermbg=3 hi Folded ctermfg=darkgrey ctermbg=NONE hi FoldColumn ctermfg=darkgrey ctermbg=NONE hi DiffAdd ctermbg=4 hi DiffChange ctermbg=5 hi DiffDelete cterm=bold ctermfg=4 ctermbg=6 hi DiffText cterm=bold ctermbg=1 hi Comment ctermfg=darkcyan hi Constant ctermfg=brown hi Special ctermfg=5 hi Identifier ctermfg=6 hi Statement ctermfg=3 hi PreProc ctermfg=5 hi Type ctermfg=2 hi Underlined cterm=underline ctermfg=5 hi Ignore cterm=bold ctermfg=7 hi Ignore ctermfg=darkgrey hi Error cterm=bold ctermfg=7 ctermbg=1 "vim: sw=4
附錄3 c.vim
"not wrap set nowrap if filereadable("tags.vim") so tags.vim endif hi cFunction guifg=LightGreen hi cMacro guifg=LightRed hi cGlobal guifg=LightBlue hi cMember guifg=LightMagenta hi def link cTypedef cType
附錄4 htags.sh
#!/bin/bash ctags -R --fields=+l ; awk -F '"' '$2 ~ /^\tf/ {print $1 "\n"}' tags | awk '$1 ~ /^[a-zA-Z]/ {print "syn keyword cFunction " $1}' 1> tags.vim ; awk -F '"' '$2 ~ /^\t[de]/ {print $1 "\n"}' tags | awk '$1 ~ /^[a-zA-Z]/ {print "syn keyword cMacro " $1}' 1>> tags.vim ; awk -F '"' '$2 ~ /^\tt/ {print $1 "\n"}' tags | awk '$1 ~ /^[a-zA-Z]/ {print "syn keyword cTypedef " $1}' 1>> tags.vim ; awk -F '"' '$2 ~ /^\tv/ {print $1 "\n"}' tags | awk '$1 ~ /^[a-zA-Z]/ {print "syn keyword cGlobal " $1}' 1>> tags.vim ;
附錄5 .ycm_extra_conf.py
import os flags = [ '-x', 'c', '-Wall', '-DOS=LINUX', '-I./mycode/igmpsnoop/h', '-I./mycode/mldsnoop/h', '-I./mycode/head_files', '-I./mycode/g8132/inc', '-I./mycode/nqa/inc', '-I./mycode/mplste/inc', '-I./mycode/mplsoam/inc', '-I./mycode/cli', '-I./mycode/trill/inc', '-I./mycode/igmpsnoop_onu/inc', '-I./mycode/hqos/inc', '-I./mycode/qos/inc', '-I./mycode/mplsqos/inc', '-I./mycode/pim/inc', '-I./USP_HEADFILE/protocol/acl/h', '-I./USP_HEADFILE/protocol/hwroute/h', '-I./USP_HEADFILE/protocol/uspIf/inc', '-I/home/taopeng/Workspace/vmware/linux_share/osal_linux/inc', '-I/home/taopeng/Workspace/vmware/linux_share/usp_linux3.12.17/inc' ] def MakeFinalFlag(): workDir = os.path.dirname(os.path.abspath(__file__)) finalFlags = [] for flag in flags: if flag.startswith('-I'): path = flag[len('-I'):] flag = '-I' + os.path.join(workDir, path) finalFlags.append(flag) return finalFlags def FlagsForFile(fileName, **kwargs): return { 'flags': MakeFinalFlag(), 'do_cache': True } if __name__ == '__main__': print(FlagsForFile("test"))