Welcome 微信登录

首页 / 操作系统 / Linux / 针对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. 使用快捷键 t 创建 ctags的标签
  3. 使用快捷键 c 创建 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-colorsset 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 nowrapif filereadable("tags.vim")
    so tags.vim
endifhi cFunction guifg=LightGreen
hi cMacro    guifg=LightRed
hi cGlobal guifg=LightBlue
hi cMember guifg=LightMagenta
hi def link cTypedef cType附录4 htags.sh#!/bin/bashctags -R --fields=+l ;
awk -F """ "$2 ~ /^ f/    {print $1 " "}" tags | awk "$1 ~ /^[a-zA-Z]/ {print "syn keyword cFunction " $1}" 1>  tags.vim ;
awk -F """ "$2 ~ /^ [de]/ {print $1 " "}" tags | awk "$1 ~ /^[a-zA-Z]/ {print "syn keyword cMacro " $1}"    1>> tags.vim ;
awk -F """ "$2 ~ /^ t/    {print $1 " "}" tags | awk "$1 ~ /^[a-zA-Z]/ {print "syn keyword cTypedef " $1}"  1>> tags.vim ;
awk -F """ "$2 ~ /^ v/    {print $1 " "}" tags | awk "$1 ~ /^[a-zA-Z]/ {print "syn keyword cGlobal " $1}" 1>> tags.vim ;附录5 .ycm_extra_conf.pyimport osflags = [
    "-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.htmCentOS 5.4 安装高亮Vim编辑工具 http://www.linuxidc.com/Linux/2013-06/86508.htmVim技巧分享:C语言设置 http://www.linuxidc.com/Linux/2012-12/77124.htmUbuntu中设置Vim的行号 http://www.linuxidc.com/Linux/2012-12/75485.htm本文永久更新链接地址