打造VIM IDE(针对C语言开发者)

 

================================
使用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

  1. "===================通用配置======================
  2.  
  3. "文件搜索路径
  4. set path=.,/usr/include,,
  5.  
  6. " 控制
  7. set nocompatible "关闭vi兼容
  8. filetype off "关闭文件类型侦测,vundle需要
  9. set fileencodings=utf-8,gbk "使用utf-8gbk编码方式
  10. syntax on "语法高亮
  11. set backspace=2 "退格键正常模式
  12. set whichwrap=<,>,[,] "当光标到行首或行尾,允许左右方向键换行
  13. set autoread "文件在vim外修改过,自动重载
  14. set nobackup "不使用备份
  15. set confirm "在处理未保存或只读文件时,弹出确认消息
  16. set scrolloff=3 "光标移动到距离顶部或底部开始滚到距离
  17. set history=1000 "历史记录数
  18. set mouse= "关闭鼠标
  19. set selection=inclusive "选择包含最后一个字符
  20. set selectmode=mouse,key "启动选择模式的方式
  21. set completeopt=longest,menu "智能补全,弹出菜单,无歧义时才自动填充
  22. set noswapfile "关闭交换文件
  23. set hidden "允许在有未保存的修改时切换缓冲区
  24.  
  25. "显示
  26. colorscheme mycolor "选择配色方案
  27. set t_Co=256 "可以使用的颜色数目
  28. set number "显示行号
  29. set laststatus=2 "显示状态行
  30. set ruler "显示标尺
  31. set showcmd "显示输入的命令
  32. set showmatch "高亮括号匹配
  33. set matchtime=1 "匹配括号高亮的时间(十分之一秒)
  34. set matchpairs={:},(:) "匹配括号"{}""()"
  35. set hlsearch "检索时高亮匹配项
  36. set incsearch "边检索边显示匹配
  37. set go-=T "去除gvim的toolbar
  38.  
  39. "格式
  40. set noexpandtab "不要将tab转换为空格
  41. set shiftwidth=4 "自动缩进的距离,也是平移字符的距离
  42. set tabstop=4 "tab键对应的空格数
  43. set autoindent "自动缩进
  44. set smartindent "智能缩进
  45.  
  46. "===================按键映射======================
  47.  
  48. "按键映射的起始字符
  49. let mapleader = '\'
  50.  
  51. "使用Ctrl-l Ctrl+h 切换标签页
  52. nnoremap <C-l> gt
  53. nnoremap <c-h> gT
  54.  
  55. "在行末加上分号
  56. nnoremap <silent> <Leader>; :<Esc><End>a<Space>;<Esc><Down>
  57. "保存
  58. nnoremap <C-s> :w<CR>
  59. "替换
  60. nnoremap <C-h> :%s/<C-R>=expand("<cword>")<CR>/<C-R>=expand("<cword>")<CR>
  61. "===================插件管理======================
  62.  
  63. " 下载vundle
  64. " git clone https://github.com/VundleVim/Vundle.vim.git ~/.vim/bundle/Vundle.vim
  65.  
  66. " 下载pathogen
  67. " curl -LSso ~/.vim/autoload/pathogen.vim https://tpo.pe/pathogen.vim
  68.  
  69. " 将vundle加入到runtime path
  70. set rtp+=~/.vim/bundle/Vundle.vim
  71.  
  72. " 下载到bundle目录的插件
  73. call vundle#begin()
  74.  
  75. " plugin on GitHub repo
  76. Plugin 'VundleVim/Vundle.vim'
  77. Plugin 'scrooloose/nerdtree'
  78. Plugin 'Lokaltog/vim-powerline.git'
  79. Plugin 'wincent/command-t'
  80. Plugin 'Valloric/YouCompleteMe'
  81. Plugin 'tomtom/tlib_vim'
  82. Plugin 'tomtom/viki_vim'
  83.  
  84. " plugin from http://vim-scripts.org/vim/scripts.html
  85. Plugin 'taglist.vim'
  86. Plugin 'EasyGrep'
  87.  
  88. " Git plugin not hosted on GitHub
  89. " Plugin 'git://...'
  90.  
  91. " git repos on your local machine
  92. " Plugin 'file://...'
  93.  
  94. call vundle#end()
  95.  
  96. filetype plugin indent on " required
  97.  
  98. "===================插件配置======================
  99.  
  100. "-----pathogen-----
  101. execute pathogen#infect()
  102.  
  103. "-----NERDTree-----
  104. let g:NERDTreeCaseSensitiveSort = 1
  105. let g:NERDTreeWinSize = 25
  106. let g:NERDTreeWinPos = "right"
  107. nnoremap <silent> <Leader>t :NERDTreeToggle<CR>
  108. nnoremap <silent> <Leader>o :NERDTreeFind<CR>
  109.  
  110. "-----Powerline-----
  111. set fillchars+=stl:\ ,stlnc:\
  112. let g:Powerline_symbols = 'compatible'
  113. let g:Powerline_stl_path_style = 'filename' "只显示文件名
  114.  
  115. "-----Command-T-----
  116. let g:CommandTFileScanner = 'ruby' "使用ruby作为文件浏览器
  117. let g:CommandTTraverseSCM = 'dir' "根目录为执行vim时所在的目录
  118. "打开文件跳转
  119. nnoremap <silent> <Leader>f :CommandT<CR>
  120.  
  121. "-----taglist-----
  122. let Tlist_Show_One_File = 1 "只显示当前文件的taglist
  123. let Tlist_Exit_OnlyWindow = 1 "taglist是最后一个窗口时退出vim
  124. let Tlist_Use_Left_Window = 1 "在左侧窗口显示taglist
  125. let Tlist_GainFocus_On_ToggleOpen = 1 "打开taglist时,光标停在taglist窗口
  126. let Tlist_Auto_Update = 1 "自动更新
  127. " 打开标签浏览器
  128. nnoremap <silent><Leader>dt :Tlist<CR>
  129. " 重新生成标签
  130. nnoremap <silent><Leader>bt :!~/Myfiles/Tool/sh/ctags/hitags.sh<CR>
  131. " 高亮标签
  132. nnoremap <silent><Leader>ht :so tags.vim<CR>
  133.  
  134. "-----cscope-----
  135. "加载cscope库
  136. if filereadable("cscope.out")
  137. cs add cscope.out
  138. endif
  139. set cscopequickfix=s-,c-,d-,i-,t-,e- "使用quickfix窗口显示结果
  140. set cst "跳转时也使用cscope库
  141. "打开引用窗口
  142. nnoremap <silent><Leader>cw :cw<CR>
  143. "重新生成索引文件
  144. nnoremap <silent><Leader>bc :!cscope -Rbq<CR>
  145. "s: 查找本C符号
  146. "g: 查找本定义
  147. "d: 查找本函数调用的函数
  148. "c: 查找调用本函数的函数
  149. "t: 查找本字符串
  150. "e: 查找本egrep模式
  151. "f: 查找本文件
  152. "i: 查找包含本文件的文件
  153. nnoremap <C-\>s :scs find s <C-R>=expand("<cword>")<CR><CR>
  154. nnoremap <C-\>g :scs find g <C-R>=expand("<cword>")<CR><CR>
  155. nnoremap <C-\>c :scs find c <C-R>=expand("<cword>")<CR><CR>
  156. nnoremap <C-\>t :scs find t <C-R>=expand("<cword>")<CR><CR>
  157. nnoremap <C-\>e :scs find e <C-R>=expand("<cword>")<CR><CR>
  158. nnoremap <C-\>f :scs find f <C-R>=expand("<cfile>")<CR><CR>
  159. nnoremap <C-\>i :scs find i <C-R>=expand("<cfile>")<CR><CR>
  160. nnoremap <C-\>d :scs find d <C-R>=expand("<cword>")<CR><CR>
  161.  
  162. "-----YouCompleteMe-----
  163. let g:ycm_server_python_interpreter= '/usr/bin/python2'
  164. let g:ycm_global_ycm_extra_conf = '~/.ycm_extra_conf.py' "默认配置文件
  165. let g:ycm_key_invoke_completion = '<C-Tab>' "跨文件补全
  166. let g:ycm_confirm_extra_conf = 0 "关闭加载配置文件提示
  167. let g:ycm_cache_omnifunc = 0 "关闭补全缓存
  168. let g:ycm_enable_diagnostic_signs = 0 "关闭诊断提示符
  169. let g:ycm_enable_diagnostic_highlighting = 1 "关闭诊断高亮
  170. "let g:ycm_show_diagnostics_ui = 0 "关闭诊断ui
  171. let g:ycm_min_num_of_chars_for_completion = 3 "n字符开始自动补全
  172. "获取变量类型
  173. nnoremap <silent><Leader>yt :YcmCompleter GetType<CR>
  174. "跳转定义或声明
  175. nnoremap <silent><Leader>yg :YcmCompleter GoTo<CR>
  176. "跳转包含文件
  177. nnoremap <silent><Leader>yi :YcmCompleter GoToInclude<CR>
  178. "打开诊断信息
  179. nnoremap <silent><Leader>yd :YcmDiags<CR>
  180.  
  181. "-----EasyGrep-----
  182. let EasyGrepMode = 2 "根据文件类型搜索相应文件
  183. let EasyGrepRecursive = 1 "递归搜索
  184. let EasyGrepCommand = 1 "使用grep
  185. let EasyGrepJumpToMatch = 0 "不要跳转

附录2 mycolor.vim

  1. " Vim color file
  2. " Maintainer: Hans Fugal <hans@fugal.net>
  3. " Last Change: $Date: 2004/06/13 19:30:30 $
  4. " Last Change: $Date: 2004/06/13 19:30:30 $
  5. " URL: http://hans.fugal.net/vim/colors/desert.vim
  6. " Version: $Id: desert.vim,v 1.1 2004/06/13 19:30:30 vimboss Exp $
  7.  
  8. " cool help screens
  9. " :he group-name
  10. " :he highlight-groups
  11. " :he cterm-colors
  12.  
  13. set background=dark
  14. if version > 580
  15. " no guarantees for version 5.8 and below, but this makes it stop
  16. " complaining
  17. hi clear
  18. if exists("syntax_on")
  19. syntax reset
  20. endif
  21. endif
  22. let g:colors_name="desert"
  23.  
  24. hi Normal guifg=White guibg=grey20
  25.  
  26. " highlight groups
  27. hi Cursor guibg=khaki guifg=slategrey
  28. "hi CursorIM
  29. "hi Directory
  30. "hi DiffAdd
  31. "hi DiffChange
  32. "hi DiffDelete
  33. "hi DiffText
  34. "hi ErrorMsg
  35. hi VertSplit guibg=#c2bfa5 guifg=grey50 gui=none
  36. hi Folded guibg=grey30 guifg=gold
  37. hi FoldColumn guibg=grey30 guifg=tan
  38. hi IncSearch guifg=slategrey guibg=khaki
  39. "hi LineNr
  40. hi ModeMsg guifg=goldenrod
  41. hi MoreMsg guifg=SeaGreen
  42. hi NonText guifg=LightBlue guibg=grey30
  43. hi Question guifg=springgreen
  44. hi Search guibg=peru guifg=wheat
  45. hi SpecialKey guifg=yellowgreen
  46. hi StatusLine guibg=#c2bfa5 guifg=black gui=none
  47. hi StatusLineNC guibg=#c2bfa5 guifg=grey50 gui=none
  48. hi Title guifg=indianred
  49. hi Visual gui=none guifg=khaki guibg=olivedrab
  50. "hi VisualNOS
  51. hi WarningMsg guifg=salmon
  52. "hi WildMenu
  53. "hi Menu
  54. "hi Scrollbar
  55. "hi Tooltip
  56.  
  57. " syntax highlighting groups
  58. hi Comment guifg=SkyBlue
  59. hi Constant guifg=#ffa0a0
  60. hi Identifier guifg=palegreen
  61. hi Statement guifg=khaki
  62. hi PreProc guifg=indianred
  63. hi Type guifg=darkkhaki
  64. hi Special guifg=navajowhite
  65. "hi Underlined
  66. hi Ignore guifg=grey40
  67. "hi Error
  68. hi Todo guifg=orangered guibg=yellow2
  69.  
  70. " color terminal definitions
  71. hi SpecialKey ctermfg=darkgreen
  72. hi NonText cterm=bold ctermfg=darkblue
  73. hi Directory ctermfg=darkcyan
  74. hi ErrorMsg cterm=bold ctermfg=7 ctermbg=1
  75. hi IncSearch cterm=NONE ctermfg=yellow ctermbg=green
  76. hi Search cterm=NONE ctermfg=grey ctermbg=blue
  77. hi MoreMsg ctermfg=darkgreen
  78. hi ModeMsg cterm=NONE ctermfg=brown
  79. hi LineNr ctermfg=3
  80. hi Question ctermfg=green
  81. hi StatusLine cterm=bold,reverse
  82. hi StatusLineNC cterm=reverse
  83. hi VertSplit cterm=reverse
  84. hi Title ctermfg=5
  85. hi Visual cterm=reverse
  86. hi VisualNOS cterm=bold,underline
  87. hi WarningMsg ctermfg=1
  88. hi WildMenu ctermfg=0 ctermbg=3
  89. hi Folded ctermfg=darkgrey ctermbg=NONE
  90. hi FoldColumn ctermfg=darkgrey ctermbg=NONE
  91. hi DiffAdd ctermbg=4
  92. hi DiffChange ctermbg=5
  93. hi DiffDelete cterm=bold ctermfg=4 ctermbg=6
  94. hi DiffText cterm=bold ctermbg=1
  95. hi Comment ctermfg=darkcyan
  96. hi Constant ctermfg=brown
  97. hi Special ctermfg=5
  98. hi Identifier ctermfg=6
  99. hi Statement ctermfg=3
  100. hi PreProc ctermfg=5
  101. hi Type ctermfg=2
  102. hi Underlined cterm=underline ctermfg=5
  103. hi Ignore cterm=bold ctermfg=7
  104. hi Ignore ctermfg=darkgrey
  105. hi Error cterm=bold ctermfg=7 ctermbg=1
  106.  
  107. "vim: sw=4

附录3 c.vim

  1. "not wrap
  2. set nowrap
  3.  
  4. if filereadable("tags.vim")
  5. so tags.vim
  6. endif
  7.  
  8. hi cFunction guifg=LightGreen
  9. hi cMacro guifg=LightRed
  10. hi cGlobal guifg=LightBlue
  11. hi cMember guifg=LightMagenta
  12. hi def link cTypedef cType

附录4 htags.sh

  1. #!/bin/bash
  2.  
  3. ctags -R --fields=+l ;
  4. awk -F '"' '$2 ~ /^\tf/ {print $1 "\n"}' tags | awk '$1 ~ /^[a-zA-Z]/ {print "syn keyword cFunction " $1}' 1> tags.vim ;
  5. awk -F '"' '$2 ~ /^\t[de]/ {print $1 "\n"}' tags | awk '$1 ~ /^[a-zA-Z]/ {print "syn keyword cMacro " $1}' 1>> tags.vim ;
  6. awk -F '"' '$2 ~ /^\tt/ {print $1 "\n"}' tags | awk '$1 ~ /^[a-zA-Z]/ {print "syn keyword cTypedef " $1}' 1>> tags.vim ;
  7. 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

  1. import os
  2.  
  3. flags = [
  4. '-x',
  5. 'c',
  6. '-Wall',
  7. '-DOS=LINUX',
  8. '-I./mycode/igmpsnoop/h',
  9. '-I./mycode/mldsnoop/h',
  10. '-I./mycode/head_files',
  11. '-I./mycode/g8132/inc',
  12. '-I./mycode/nqa/inc',
  13. '-I./mycode/mplste/inc',
  14. '-I./mycode/mplsoam/inc',
  15. '-I./mycode/cli',
  16. '-I./mycode/trill/inc',
  17. '-I./mycode/igmpsnoop_onu/inc',
  18. '-I./mycode/hqos/inc',
  19. '-I./mycode/qos/inc',
  20. '-I./mycode/mplsqos/inc',
  21. '-I./mycode/pim/inc',
  22. '-I./USP_HEADFILE/protocol/acl/h',
  23. '-I./USP_HEADFILE/protocol/hwroute/h',
  24. '-I./USP_HEADFILE/protocol/uspIf/inc',
  25. '-I/home/taopeng/Workspace/vmware/linux_share/osal_linux/inc',
  26. '-I/home/taopeng/Workspace/vmware/linux_share/usp_linux3.12.17/inc'
  27. ]
  28.  
  29. def MakeFinalFlag():
  30.  
  31. workDir = os.path.dirname(os.path.abspath(__file__))
  32.  
  33. finalFlags = []
  34. for flag in flags:
  35.  
  36. if flag.startswith('-I'):
  37. path = flag[len('-I'):]
  38. flag = '-I' + os.path.join(workDir, path)
  39.  
  40. finalFlags.append(flag)
  41.  
  42. return finalFlags
  43.  
  44. def FlagsForFile(fileName, **kwargs):
  45.  
  46. return {
  47. 'flags': MakeFinalFlag(),
  48. 'do_cache': True
  49. }
  50.  
  51. if __name__ == '__main__':
  52. print(FlagsForFile("test"))

VIM IDE的更多相关文章

  1. vim IDE平台-打造属于自己的配置

    vim IDE平台-打造属于自己的配置 一.前言 目前工作环境基本以Linux为主,自然用到VIM也很多,很早就对如何提高VIM的使用效率有所研究,限于时间关系,也没做个系统记录和资料积累,时间久了又 ...

  2. dotfiles for linux/unix users automatically! (python Vim IDE)

    Here is a brief introduction and package of dotfiles for linux/unix user. I think there are enough i ...

  3. 初涉Linux ----------> 打造自己的 Vim IDE

    一.  开篇前言 (图片显示越界的话,请刷新) 装好Ubuntu15.04系统之后呢,玩了玩 Ubuntu,感觉还是很不错的.比windows快,一开机就可以打开你想要的程序,但是在windows下你 ...

  4. 一键打造vim ide 支持python golang shell等高级特性

    1.vim-for-devops github: https://github.com/yxxhero/vim_for_devops 利用vim插件打造支持python.shell.golang的id ...

  5. Vim ide for shell development

    Source : This article is part of the ongoing Vi / Vim Tips and Tricks Series. As a Linux sysadmin or ...

  6. emacs vim IDE

    原本想花点时间来学习下Vim或者emacs,结果在网上搜索到这篇文章 骂战挺多的,但是也长见识 http://bbs.csdn.net/topics/390306165 下面是windows下的ema ...

  7. 打造vim IDE

    pathogen.vim:vim插件目录自动识别.加载(注意:能用pathogen.vim安装插件,就不要用Vundle.因为Vundle下载插件速度非常慢.) https://github.com/ ...

  8. vim IDE配置

    参考: http://www.cnblogs.com/witcxc/archive/2011/12/28/2304704.html http://www.cnblogs.com/ma6174/arch ...

  9. 学以致用十四-----打造一个简单的vim IDE

    一.安装dircolors git clone https://github.com/seebi/dircolors-solarized.git cd dircolors-solarized/ mv ...

随机推荐

  1. oracle 开发笔记“跨数据库查询复制”

    1.方法一:创建DBL(data base link) CREATE PUBLIC DATABASE LINK 数据链名称 CONNECT TO 登陆用户名 IDENTIFIED BY 密码 USIN ...

  2. base64这种编码的意义

    BASE64不是用来加密的.你看看经过BASE64编码后的字符串,全部都是由标准键盘上面的常规字符组成,这样编码后的字符串在网关之间传递不会产生UNICODE字符串不能识别或者丢失的现象.你再仔细研究 ...

  3. 我的Python成长之路---第七天---Python基础(21)---2016年2月27日(晴)

    四.面向对象进阶 1.类方法 普通的方法通过对象调用,至少有一个self参数(调用的时候系统自动传递,不需要手工传递),而类方法由类直接调用,至少有一个cls参数,执行时,自动将调用该方法的类赋值个c ...

  4. android获取系统wifi状态等

    WIFI 获取WIFI状态 WifiManager wifiManager = (WifiManager)context.getSystemService(Context.WIFI_SERVICE); ...

  5. mongodb 限制ip访问

    <pre name="code" class="python">一.限制访问IP和端口 MongoDB可以限制只允许某一特定IP来访问,只要在启动时 ...

  6. 深入探究VC —— 编译器cl.exe(2)

    这一章节介绍的全是VC编译器选项,option参数是cl.exe的编译选项,是cl.exe命令行参数中最复杂.也是最常用的.下面介绍一些常用的编译选项: 1.代码生成有关 这些选项将影响编译完成后生成 ...

  7. DDB与DIB的区别

    DDB(设备相关位图) DDB依赖于具体设备:DDB的颜色模式必需与输出设备相一致.例如,如果当前的显示设备是256色模式,那么DDB必然也是256色的.在256色以下的位图中存储的像素值是系统调色板 ...

  8. H面试程序(16): 简单选择排序

    #include<stdio.h> #include<assert.h> void display(int * a, int n) { assert(a); for(int i ...

  9. BZOJ-1007-水平可见直线-HN2008

    描写叙述 在xoy直角坐标平面上有n条直线L1,L2,-Ln,若在y值为正无穷大处往下看,能见到Li的某个子线段,则称Li为可见的,否则Li为被覆盖的. 比如,对于直线: L1:y=x; L2:y=- ...

  10. __get __set 实例

    <?php class Person { //下面是人的成员属性,都是封装的私有成员 private $name; //人的名子 private $sex; //人的性别 private $ag ...