Linux vim IDE配置---基于Ubuntu 16.04

1.序言        

本文是本人在Ubuntu 16.04上Vim IDE搭建过程的总结。在Windows环境开发的小伙伴也许都知道sourceinsight这款编辑器,为了提高Linux下开发效率,本人搭建了类sourceinsight的IDE开发环境(勉强称作VIM IDE吧:))。

         废话不多,先上图展示本人的VIM IDE。 一个窗口同时显示了4个子终端窗口(当然也可以有更多),每一个子终端窗口类似sourceinsight又切分成了3栏(左下Taglist栏-显示函数,宏等标签;左上filelist栏-显式代码文件目录文件结构; 右侧栏-代码编辑区)。 如果我们看着窗口字太小,没关系, 当编辑程序时候你可以选择4个子终端窗口中要编辑的那个终端单独放大全屏显示(terminator安装后Ctrl+shit+X),编辑完成之后仍然可以切换回来(terminator安装后Ctrl+shift+Z)。

       

Linux vim IDE配置---基于Ubuntu 16.04_第1张图片

2. 配置过程

     如果要完成以上显示效果,我们需要安装一些必要的工具和插件,下面将逐步展开。

2.1 Terminator 安装

      工具Terminator可以在一个窗口中显示多个终端窗口,并且可以按照用户的要求对窗口进行任意分割。

      Ubuntu环境安装terminator工具命令为:sudo apt-get install terminator

      Terminator常用快捷按键有

     1)Ctrl+Shift+E垂直分割窗口

       2)Ctrl+Shift+O 水平分割窗口

       3)Ctrl+Tab  在分割的窗口间切换

       4)Ctrl+Shift+C/V 复制/粘贴

       5)Ctrl+Shift+X 放大窗口到全屏

       6)Ctrl+Shift+Z 从放大窗口回到多窗口

     

      注意:如果您对窗口风格不满意,可以右键->preferences进行必要设置和修改。比如修改快捷按键等等。

2.2 vim 编辑器安装

      Ubuntu环境安装terminator工具命令为: sudo apt-get install vim vim-scripts vim-doc

     其中vim-scripts是vim的一些基本插件 ,vim安装之后紧接着应该个性化配置。Vim本身的系统配置文件夹是在/usr/share/vim/和/etc/vim/两个文件夹下。但是通常我们不会去改变这两个文件夹下的配置文件,而是在用户文件夹/home/user下建立自己的配置文件.vimrc然后对其配置,这里不再解释,后面我将附上我的vim配置。


2.3 vim插件安装

    vim常用插件有:1)vim-addons 2)ctags 3)cscope 4)winmanager 5)minibufexplorer 6)omnicppcomplete 7)AutoComplPop  8)echofunc 9)taglist


2.3.1 vim-addons

   通过vim-addons,我们可以管理vim插件。可通过命令sudo apt-get install vim-addon-manager手动安装。

  查看插件状态 命令: vim-addons status

# Name                     User Status  System Status
align                       removed       removed       
alternate                   removed       removed       
bufexplorer                 removed       removed       
calendar                    removed       removed       
closetag                    removed       removed       
colors-sampler-pack         removed       removed       
cvsmenu                     removed       removed       
debPlugin                   removed       removed       
detectindent                removed       removed       
doxygen-toolkit             removed       removed       
editexisting                removed       removed       
enhanced-commentify         removed       removed       
gnupg                       removed       removed       
info                        removed       removed       
justify                     removed       removed       
lbdbq                       removed       removed       
matchit                     removed       removed       
minibufexplorer             installed     removed       
nerd-commenter              removed       removed       
omnicppcomplete             installed     removed       
po                          removed       removed       
project                     installed     removed       
python-indent               removed       removed       
secure-modelines            removed       removed       
snippetsEmu                 removed       removed       
sokoban                     removed       removed       
supertab                    removed       removed       
surround                    removed       removed       
taglist                     installed     removed       
tetris                      removed       removed       
utl                         removed       removed       
vcscommand                  removed       removed       
vimplate                    removed       removed       
whatdomain                  removed       removed       
winmanager                  installed     removed       
xmledit                     removed       removed


    安装某个插件X命令(前提是在目录/home/user/.vim/下建立好了plugin和doc两个文件夹):vim-addons install X

2.3.2 ctags

    ctags用来建立源码树的标签索引(标签就是一个标识符被定义的地方,如函数定义),在编程时能迅速定位函数、变量、宏定义等位置去查看原形。
    1)ctags安装命令

          sudo apt-get install ctags

    2)ctags配置

         要正确使用ctags功能,我们也需要配置vimrc文件,我将在附件列举。

     3)建立项目project的源码索引命令(通常在项目project文件夹根目录执行)

          ctags -R *

          执行以上命令之后,你会发现多了一个tags文件,这个就是ctags索引文件。

     4)ctags常用方法

          Ctrl+]  跳到当前光标下单词的标签
          Ctrl+O  返回上一个标签
          Ctrl+T  返回上一个标签
         Ctrl+W + ]  新窗口显示当前光标下单词的标签,光标跳到标签处


2.3.3 cscope

      cscope是类似于ctags一样的工具,但是比ctags更强大。

     1)cscope安装命令

          sudo apt-get install cscope

    2)cscope配置

         要正确使用cscope功能,我们也需要配置vimrc文件,我将在附件列举。

     3)建立项目project的源码cscope数据库命令(通常在项目project文件夹根目录执行)

          cscope -Rbq cscope.out

          执行以上命令之后,你会发现多了3个文件,cscope.in.out,cscope.po.out, cscope.out, 它们就是cscope索引数据库。

        注意:以上命令收集的是project目录下cscope缺省类型文件索引,有时候我们自定义文件不能索引,所以我们可以用如下命令替换以上命令:

         find  .  -name "*.h" -o -name "*.cpp" -o -name "*.c" > cscope.file

        cscope -bqk -i cscope.file

     4)添加cscope库到vim

         对于简单1个文件,用vim打开某个源码文件,末行模式下,输入“:cs add cscope.out"。对于整个project工程文件,我们需要借助vim自己读取配置文件。

        我是在用户目录~/.vim/plugin/下放置了cscope_map.vim配置文件,后面会附上。

     5)cscope常用方法

       Ctrl-\ s 查找所有当前光标所在符号出现过位置。
       Ctrl-\ c 查找所有调用当前光标所在函数的函数。

      当然还有其它按键,请阅读配置文件或者在vim命令行执行:help cscope

     注意:通过快捷键查找某个符号后,会立即跳转到第一个找到的该符号出现的位置。如果你对这次默认跳转的位置不满意,在Vim命令行下运行cw命令,就能在编辑区下面quickfix窗口看到所有查找结果的列表,点击相应列表项就能跳转到相应位置。

2.3.4  winmanager

    WinManager用于管理文件浏览器和缓冲区(buffer)。

    安装命令为:vim-addons install winmanager

    配置见附录vimrc,进入vim之后打开或者关闭命令是输入:WMToggle


2.3.5 minibufexplorer

    安装命令为:vim-addons install minibufexplorer

    配置见附录vimrc

   

2.3.6 omnicppcomplete(自动补齐)

   安装命令为:vim-addons install omnicppcomplete

  配置文件见附录vimrc


2.3.7 AutoComplPop(普通变量和函数自动弹出补齐)

      下载链接: http://www.vim.org/scripts/script.php?script_id=1879 

        安装方法:

               先解压:unzip vim-autocomplpop.zip,将解压后的文件拷贝到~/.vim/ 下的相应目录里:

                     autoload/*    ->    ~/.vim/autoload/

                     doc/*    ->    ~/.vim/doc/

                     plugin/*    ->    ~/.vim/plugin/

       帮助使用:

              重新打开vim即可使用。添加help文件:helptags ~/.vim/doc/即可(打开帮助文件:help autocomplpop)


2.3.8 echofunc(函数原型提示)

     echofunc下载地址:http://www.vim.org/scripts/script.php?script_id=1735
     下载完成后,把echofunc.vim文件放到 ~/.vim/plugin文件夹中
     当在vim插入(insert)模式下紧接着函数名后输入一个"("的时候, 这个函数的声明就会自动显示在信息提示栏。

    注意:这个插件需要tags文件的支持, 并且在创建tags文件的时候要加选项"--fields=+lS"。  ctags -R --field=+IS

2.3.9 taglist

      taglist用于列出了当前文件中的所有标签(宏, 全局变量, 函数名等)。
     安装Taglist命令: vim-addons install taglist

      配置见附录vimrc,进入vim之后关闭或者打开taglist方法是末行命令模式输入:Taglist。


2.3.10 quickfix

      在程序的开发过程中,很重要的一个循环是:编辑-编译-编辑,vim中的quickfix功能就是为了提高这一循环的效率。

     推荐使用Makefile的方式进行项目的编译、管理,可以实现项目的自动化管理、有利于提高效率。通过make命令完成程序的编译工作后,会得到编译结果,一般会有一些编译错误,quickfix功能使我们可以直接跳到文件中的错误位置,直接进行修改,并通过使用quickfix的命令完成错误列表的跳转。

     如果仅有1个文件也懒得写Makefile文件,可以采用如下2条命令处理:

     :set makeprg=gcc\ test.c\ -o\ test
     :make
     变量的值为字符串,当在其中有空格时需要用\进行转义,同样如果想输入\也要进行转义。
     注意:在vim运行时通过命令行设置的变量值均是临时的,即当退出vim环境时,该变量值会恢复为配置文件中的值或者默认值。
    
   
     常用的quickfix命令为:
     :cc     显示详细错误信息
     :cp     跳到上一个错误
     :cn     跳到下一个错误
     :cl      列出所有错误
     :cw     如果有错误列表,则打开quickfix窗口,没有则什么也不错


3. 附录

3.1 vimrc设置

    下面是我的vimrc配置,这个文件放在目录/home/user/下。

runtime! debian.vim

" Uncomment the next line to make Vim more Vi-compatible
" NOTE: debian.vim sets 'nocompatible'. Setting 'compatible' changes numerous
" options, so any other options should be set AFTER setting 'compatible'.
set nocompatible

" Vim5 and later versions support syntax highlighting. Uncommenting the
" following enables syntax highlighting by default.
if has("syntax")
    syntax on
endif
colorscheme ron  

" detect file type
filetype on
filetype plugin on

" If using a dark background within the editing area and syntax highlighting
" turn on this option as well
set background=dark

" Uncomment the following to have Vim jump to the last position when
" reopening a file
if has("autocmd")
    au BufReadPost * if line("'\"") > 1 && line("'\"") <= line("$") | exe "normal! g'\"" | endif
    "have Vim load indentation rules and plugins according to the detected filetype
    filetype plugin indent on
endif

" The following are commented out as they cause vim to behave a lot
" differently from regular Vi. They are highly recommended though.

"set ignorecase
"set smartcase       
set autowrite        
set autoindent        
"set smartindent     
set tabstop=4       
set softtabstop=4  
set shiftwidth=4  
set cindent      
set cinoptions={0,1s,t0,n-2,p2s,(03s,=.5s,>1s,=1s,:1s     
"set backspace=2    
set showmatch      
set linebreak     
set whichwrap=b,s,<,>,[,]
"set hidden " Hide buffers when they are abandoned
set mouse=a            
set number            
"set previewwindow   
set history=50      


set laststatus=2
set ruler            

set showcmd   
set showmode

"--find setting--
set incsearch       
set hlsearch   


"--ctags setting--
map :!ctags -R --c++-kinds=+p --fields=+iaS --extra=+q . :TlistUpdate
imap :!ctags -R --c++-kinds=+p --fields=+iaS --extra=+q . :TlistUpdate
set tags=tags
set tags+=./tags "add current directory's generated tags file
"set tags+=~/test/tags


"-- omnicppcomplete setting --
imap
imap
set completeopt=menu,menuone
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 (but don't insert)
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"]
let OmniCpp_ShowScopeInAbbr=1 " show scope in abbreviation and remove the last column
let OmniCpp_ShowAccess=1


"-- Taglist setting --
let Tlist_Ctags_Cmd='ctags'
let Tlist_Use_Right_Window=1
let Tlist_Show_One_File=0
let Tlist_File_Fold_Auto_Close=1
let Tlist_Exit_OnlyWindow=1
let Tlist_Process_File_Always=1
let Tlist_Inc_Winwidth=0



"-- WinManager setting --
let g:winManagerWindowLayout='FileExplorer|TagList'
"let g:persistentBehaviour=0
nmap wm :WMToggle


" -- MiniBufferExplorer --
let g:miniBufExplMapWindowNavVim = 1
let g:miniBufExplMapWindowNavArrows = 1
let g:miniBufExplMapCTabSwitchBufs = 1
let g:miniBufExplModSelTarget = 1   


"--fold setting--
set foldmethod=syntax
set foldlevel=100
set foldcolumn=5


"-- QuickFix setting --
map :make clean
map :make :copen
map :cp
map :cn
imap :make clean
imap :make :copen
imap :cp
imap :cn


3.2 cscope配置文件

   cscope配置文件位于目录(/home/user/.vim/plugin)名叫cscope_map.vim,内容为

 """"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" CSCOPE settings for vim           
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
"
" This file contains some boilerplate settings for vim's cscope interface,
" plus some keyboard mappings that I've found useful.
"
" USAGE:
" -- vim 6:     Stick this file in your ~/.vim/plugin directory (or in a
"               'plugin' directory in some other directory that is in your
"               'runtimepath'.
"
" -- vim 5:     Stick this file somewhere and 'source cscope.vim' it from
"               your ~/.vimrc file (or cut and paste it into your .vimrc).
"
" NOTE:
" These key maps use multiple keystrokes (2 or 3 keys).  If you find that vim
" keeps timing you out before you can complete them, try changing your timeout
" settings, as explained below.
"
" Happy cscoping,
"
" Jason Duell       [email protected]     2002/3/7
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""


" This tests to see if vim was configured with the '--enable-cscope' option
" when it was compiled.  If it wasn't, time to recompile vim...
if has("cscope")

    set csprg=/usr/bin/cscope "指定用来执行cscope的命令


    """"""""""""" Standard cscope/vim boilerplate

    " use both cscope and ctag for 'ctrl-]', ':ta', and 'vim -t'
    set cscopetag
    set csto=0  "设置cstag命令查找次序:0先找cscope数据库再找标签文件;1先找标签文件再找cscope数据库"
    set cst                            "同时搜索cscope数据库和标签文件
    set cscopequickfix=s-,c-,d-,i-,t-,e-    " 使用QuickFix窗口来显示cscope查找结果
    
    set nocsverb

    " add any cscope database in current directory
    if filereadable("cscope.out")
        cs add cscope.out  
    " else add the database pointed to by environment variable
    elseif $CSCOPE_DB != ""
        cs add $CSCOPE_DB
    endif

    " show msg when any other cscope db added
    set csverb
   " set cscopeverbose  


    """"""""""""" My cscope/vim key mappings
    "
    " The following maps all invoke one of the following cscope search types:
    "
    "   's'   symbol: find all references to the token under cursor
    "   'g'   global: find global definition(s) of the token under cursor
    "   'c'   calls:  find all calls to the function name under cursor
    "   't'   text:   find all instances of the text under cursor
    "   'e'   egrep:  egrep search for the word under cursor
    "   'f'   file:   open the filename under cursor
    "   'i'   includes: find files that include the filename under cursor
    "   'd'   called: find functions that function under cursor calls
    "
    " Below are three sets of the maps: one set that just jumps to your
    " search result, one that splits the existing vim window horizontally and
    " diplays your search result in the new window, and one that does the same
    " thing, but does a vertical split instead (vim 6 only).
    "
    " I've used CTRL-\ and CTRL-@ as the starting keys for these maps, as it's
    " unlikely that you need their default mappings (CTRL-\'s default use is
    " as part of CTRL-\ CTRL-N typemap, which basically just does the same
    " thing as hitting 'escape': CTRL-@ doesn't seem to have any default use).
    " If you don't like using 'CTRL-@' or CTRL-\, , you can change some or all
    " of these maps to use other keys.  One likely candidate is 'CTRL-_'
    " (which also maps to CTRL-/, which is easier to type).  By default it is
    " used to switch between Hebrew and English keyboard mode.
    "
    " All of the maps involving the macro use '^$': this is so
    " that searches over '#include " return only references to
    " 'time.h', and not 'sys/time.h', etc. (by default cscope will return all
    " files that contain 'time.h' as part of their name).


    " To do the first type of search, hit 'CTRL-\', followed by one of the
    " cscope search types above (s,g,c,t,e,f,i,d).  The result of your cscope
    " search will be displayed in the current window.  You can use CTRL-T to
    " go back to where you were before the search.  
    "

    nmap s :cs find s =expand("")    
    nmap g :cs find g =expand("")    
    nmap c :cs find c =expand("")    
    nmap t :cs find t =expand("")    
    nmap e :cs find e =expand("")    
    nmap f :cs find f =expand("")    
    nmap i :cs find i ^=expand("")$
    nmap d :cs find d =expand("")    


    " Using 'CTRL-spacebar' (intepreted as CTRL-@ by vim) then a search type
    " makes the vim window split horizontally, with search result displayed in
    " the new window.
    "
    " (Note: earlier versions of vim may not have the :scs command, but it
    " can be simulated roughly via:
    "    nmap s :cs find s =expand("")    

    nmap s :scs find s =expand("")    
    nmap g :scs find g =expand("")    
    nmap c :scs find c =expand("")    
    nmap t :scs find t =expand("")    
    nmap e :scs find e =expand("")    
    nmap f :scs find f =expand("")    
    nmap i :scs find i ^=expand("")$    
    nmap d :scs find d =expand("")    


    " Hitting CTRL-space *twice* before the search type does a vertical
    " split instead of a horizontal one (vim 6 and up only)
    "
    " (Note: you may wish to put a 'set splitright' in your .vimrc
    " if you prefer the new window on the right instead of the left

    nmap s :vert scs find s =expand("")
    nmap g :vert scs find g =expand("")
    nmap c :vert scs find c =expand("")
    nmap t :vert scs find t =expand("")
    nmap e :vert scs find e =expand("")
    nmap f :vert scs find f =expand("")    
    nmap i :vert scs find i ^=expand("")$    
    nmap d :vert scs find d =expand("")


    """"""""""""" key map timeouts
    "
    " By default Vim will only wait 1 second for each keystroke in a mapping.
    " You may find that too short with the above typemaps.  If so, you should
    " either turn off mapping timeouts via 'notimeout'.
    "
    "set notimeout
    "
    " Or, you can keep timeouts, by uncommenting the timeoutlen line below,
    " with your own personal favorite value (in milliseconds):
    "
    "set timeoutlen=4000
    "
    " Either way, since mapping timeout settings by default also set the
    " timeouts for multicharacter 'keys codes' (like ), you should also
    " set ttimeout and ttimeoutlen: otherwise, you will experience strange
    " delays as vim waits for a keystroke after you hit ESC (it will be
    " waiting to see if the ESC is actually part of a key code like ).
    "
    "set ttimeout
    "
    " personally, I find a tenth of a second to work well for key code
    " timeouts. If you experience problems and have a slow terminal or network
    " connection, set it higher.  If you don't set ttimeoutlen, the value for
    " timeoutlent (default: 1000 = 1 second, which is sluggish) is used.
    "
    "set ttimeoutlen=100

endif


3.3 ctags和cscope脚本

     为方便使用,如下写到一个脚本中,在创建工程project之后调用一下即可创建tags索引和cscope数据库。

#!/bin/sh

ctags -R --fields=+lS

find . -name "*.h" -o -name "*.c" -o -name "*.cpp" > cscope.files
cscope -bkq -i cscope.files




你可能感兴趣的:(Vim,vim,ubuntu,ctags,terminator)