ubuntu将vim配置成c++IDE

1、安装g++

sudo apt-get install g++-4.4

2、安装vim

sudo apt-get install vim vim-scripts

3、设置vim配置文件

vim ~/.vimrc

在打开的vim界面中粘贴下述代码

 " This line should not be removed as it ensures that various options are
    " properly set to work with the Vim-related packages available in Debian.
    " 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        " elflord ron peachpuff default 设置配色方案,vim自带的配色方案保存在/usr/share/vim/vim72/colors目录下
    " 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        " 如果搜索模式包含大写字符,不使用 'ignorecase' 选项。只有在输入搜索模式并且打开 'ignorecase' 选项时才会使用。
    set autowrite        " 自动把内容写回文件: 如果文件被修改过,在每个 :next、:rewind、:last、:first、:previous、:stop、:suspend、:tag、:!、:make、CTRL-] 和 CTRL-^命令时进行;用 :buffer、CTRL-O、CTRL-I、'{A-Z0-9} 或 `{A-Z0-9} 命令转到别的文件时亦然。
    set autoindent        " 设置自动对齐(缩进):即每行的缩进值与上一行相等;使用 noautoindent 取消设置
    "set smartindent        " 智能对齐方式
    set tabstop=4        " 设置制表符(tab键)的宽度
    set softtabstop=4     " 设置软制表符的宽度    
    set shiftwidth=4    " (自动) 缩进使用的4个空格
    set cindent            " 使用 C/C++ 语言的自动缩进方式
    set cinoptions={0,1s,t0,n-2,p2s,(03s,=.5s,>1s,=1s,:1s     "设置C/C++语言的具体缩进方式
    "set backspace=2    " 设置退格键可用
    set showmatch        " 设置匹配模式,显示匹配的括号
    set linebreak        " 整词换行
    set whichwrap=b,s,<,>,[,] " 光标从行首和行末时可以跳到另一行去
    "set hidden " Hide buffers when they are abandoned
    set mouse=a            " Enable mouse usage (all modes)    "使用鼠标
    set number            " Enable line number    "显示行号
    "set previewwindow    " 标识预览窗口
    set history=50        " set command history to 50    "历史记录50条
    "--状态行设置--
    set laststatus=2 " 总显示最后一个窗口的状态行;设为1则窗口数多于一个的时候显示最后一个窗口的状态行;0不显示最后一个窗口的状态行
    set ruler            " 标尺,用于显示光标位置的行号和列号,逗号分隔。每个窗口都有自己的标尺。如果窗口有状态行,标尺在那里显示。否则,它显示在屏幕的最后一行上。
    "--命令行设置--
    set showcmd            " 命令行显示输入的命令
    set showmode        " 命令行显示vim当前模式
    "--find setting--
    set incsearch        " 输入字符串就显示匹配点
    set hlsearch        

4、安装ctags

sudo apt-get install ctags
sudo apt-get install exuberant-ctags

并在.vimrc中添加

" Ctags
map <C-F12> :!ctags -R --c++-kinds=+p --fields=+iaS --extra=+q .<CR>

用vim打开.cpp文件后,在命令模式下ctrl+F12生成tags文件。此时,我们已经具有定义跳转的功能了。有两组快捷键是最常用的

Ctrl-]    跳转到光标所指变量的定义所在行首;
Ctrl-t    回到上次跳转前的位置;

5、安装管理Vim插件 --- vim-addons

sudo apt-get install vim-addon-manager

cd ~

mkdir .vim

cd .vim

mkdir plugin

mkdir doc

安装好之后就可以用vim-addons install [plugin]这样的语法形式来安装其他的插件了。


6、安装c++自动补全插件 -- OmniCppComplete

vim-addons install omnicppcomplete

将下述代码添加到.vimrc中

扫描二维码关注公众号,回复: 3338189 查看本文章
    "-- omnicppcomplete setting --
    " 按下F3自动补全代码,注意该映射语句后不能有其他字符,包括tab;否则按下F3会自动补全一些乱码
    imap <F3> <C-X><C-O>
    " 按下F2根据头文件内关键字补全
    imap <F2> <C-X><C-I>
    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 

7、标签浏览器Taglist

vim-addons install taglist

在vim配置文件~/.vimrc中加入如下配置:

"-- Taglist setting --
    let Tlist_Ctags_Cmd='ctags' "因为我们放在环境变量里,所以可以直接执行
    let Tlist_Use_Right_Window=1 "让窗口显示在右边,0的话就是显示在左边
    let Tlist_Show_One_File=0 "让taglist可以同时展示多个文件的函数列表
    let Tlist_File_Fold_Auto_Close=1 "非当前文件,函数列表折叠隐藏
    let Tlist_Exit_OnlyWindow=1 "当taglist是最后一个分割窗口时,自动推出vim
    "是否一直处理tags.1:处理;0:不处理
    let Tlist_Process_File_Always=1 "实时更新tags
    let Tlist_Inc_Winwidth=0

打开VIM后输入命令":Tlist"打开/关闭taglist窗口

帮助文档

:help taglist


8、文件浏览和缓冲区管理器WinManager

vim-addons install winmanager

配置WinManager,在配置文件~/.vimrc中加入:

"-- WinManager setting --
    let g:winManagerWindowLayout='FileExplorer|TagList' " 设置我们要管理的插件
    "let g:persistentBehaviour=0 " 如果所有编辑文件都关闭了,退出vim
    nmap wm :WMToggle<cr> 

在VIM 中使用命令 :WMToggle 打开/关闭WinManager

<enter> 或者 双击 进入目录或打开文件

<tab> 进入目录 或者 在新窗口中打开文件

- 返回上一层

<F5>刷新列表

帮助 : help WinManager


9、提示函数原型echofunc

echofunc可以在命令行中提示当前输入的函数原型

下载地址:http://www.vim.org/scripts/script.php?script_id=1735

下载后把echofunc.vim放到~/.vim/plugin下,重启vim

在vim中键入函数名后,输入"("就会在命令行中出现提示,如果函数有多个原型,按"Alt + -" 和 "Alt + ="向前和向后翻

echofunc需要tags文件支持,并且在创建tags文件时要加选项“--fields=+ls": $ctags -R --fields=+ls


10、安装csope

sudo apt-get install cscope

在.vimrc中加入配置信息

"-- Cscope setting --
    if has("cscope")
        set csprg=/usr/bin/cscope        " 指定用来执行cscope的命令
        set csto=0                        " 设置cstag命令查找次序:0先找cscope数据库再找标签文件;1先找标签文件再找cscope数据库
        set cst                            " 同时搜索cscope数据库和标签文件
        set cscopequickfix=s-,c-,d-,i-,t-,e-    " 使用QuickFix窗口来显示cscope查找结果
        set nocsverb
        if filereadable("cscope.out")    " 若当前目录下存在cscope数据库,添加该数据库到vim
            cs add cscope.out
        elseif $CSCOPE_DB != ""            " 否则只要环境变量CSCOPE_DB不为空,则添加其指定的数据库到vim
            cs add $CSCOPE_DB
        endif
        set csverb
    endif
    map <F4> :cs add ./cscope.out .<CR><CR><CR> :cs reset<CR>
    imap <F4> <ESC>:cs add ./cscope.out .<CR><CR><CR> :cs reset<CR>
    " 将:cs find c等Cscope查找命令映射为<C-_>c等快捷键(按法是先按Ctrl+Shift+-, 然后很快再按下c)
    nmap <C-_>s :cs find s <C-R>=expand("<cword>")<CR><CR> :copen<CR><CR>
    nmap <C-_>g :cs find g <C-R>=expand("<cword>")<CR><CR>
    nmap <C-_>d :cs find d <C-R>=expand("<cword>")<CR><CR> :copen<CR><CR>
    nmap <C-_>c :cs find c <C-R>=expand("<cword>")<CR><CR> :copen<CR><CR>
    nmap <C-_>t :cs find t <C-R>=expand("<cword>")<CR><CR> :copen<CR><CR>
    nmap <C-_>e :cs find e <C-R>=expand("<cword>")<CR><CR> :copen<CR><CR>
    nmap <C-_>f :cs find f <C-R>=expand("<cfile>")<CR><CR>
    nmap <C-_>i :cs find i <C-R>=expand("<cfile>")<CR><CR> :copen<CR><CR>

使用之前要先做一些准备工作

在项目代码根目录下创建cscope库:

cscope -Rbq

将cscope库添加到vim:打开某个文件,在vim的命令模式下键入命令 :cs add cscope.out

cscope的主要功能是通过 find 子命令来实现的,通过如下命令使用

:cs find c|d|e|f|g|i|s|t name

0/s 查找这个符号

1/g 查找这个定义

2/d 查找这个函数调用的函数

3/c 查找调用过这个函数的函数

4/t 查找这个字符串

6/e 查找这个egrep模式

7/f 查找这个文件

8/i 查找包含这个文件的文件

帮助文档:    :help cscope

比如

cs find 0 flag
cs find s flag

这两个语句的功能是一样的,都是寻找变量flag。

接下来可以输入   :cw    在quickfix中会显示搜索的所有结果,双击某条结果光标就会做相应的跳转。

猜你喜欢

转载自blog.csdn.net/ZT0518/article/details/82079455
今日推荐