2021-05-12 14:32:11
針對C語言開發者打造Vim IDE
使用Vim打造IDE, 針對C語言開發者,建議使用Gvim。
推薦:把Vim打造成優秀的C++ IDE http://www.linuxidc.com/Linux/2016-06/132262.htm
把Vim打造成一個簡單實用的IDE http://www.linuxidc.com/Linux/2011-06/37032.htm
先上兩個截圖
# 安裝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 <hans@fugal.net>
" 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"))
更多Vim相關教學見以下內容:
Vim學習指南 http://www.linuxidc.com/Linux/2013-08/89096.htm
快速學會 Vi編輯器 http://www.linuxidc.com/Linux/2013-08/88586.htm
強大的Vim 編輯器 http://www.linuxidc.com/Linux/2013-07/87544.htm
在CentOS 6.2上搭建Vim開發環境 http://www.linuxidc.com/Linux/2013-07/87363.htm
CentOS 5.4 安裝高亮Vim編輯工具 http://www.linuxidc.com/Linux/2013-06/86508.htm
Vim技巧分享:C語言設定 http://www.linuxidc.com/Linux/2012-12/77124.htm
Ubuntu中設定Vim的行號 http://www.linuxidc.com/Linux/2012-12/75485.htm
本文永久更新連結地址:http://www.linuxidc.com/Linux/2016-06/132273.htm
相關文章