@Dubyoo
2014-06-17T14:02:12.000000Z
字数 10611
阅读 9057
Linux
作为一个c++程序员,要想尽可能地发挥YCM的功能,推荐安装以下软件:
sudo apt-get install vim
sudo apt-get install git
sudo apt-get install cmake
sudo apt-get install pylint python-dev
Vundle是vim的一款进行插件管理的插件。它需要git的支持,git可以在各发行版软件源内找到并安装。除了安装时的一个命令,日常vundle的使用并不需要了解git命令。
以下安装流程参考自vundle的github简介。
1.使用git下载vundle
运行命令:
git clone https://github.com/gmarik/vundle.git ~/.vim/bundle/vundle
这样插件将被下载到 ~/.vim/bundle/vundle
2.编辑 .vimrc
在~/.vimrc末尾添加以下内容:
""""""""""""""""""""" Vundleset nocompatiblefiletype offset rtp+=~/.vim/bundle/vundlecall vundle#rc()Bundle 'gmarik/vundle'Bundle 'Valloric/YouCompleteMe'Bundle 'Valloric/ListToggle'Bundle 'scrooloose/syntastic'filetype plugin indent on""""""""""""""""""""" Vundle
其中以Bundle开头的行,每一个Bundle代表一个vim插件,这些省略完整URL插件都是托管在https://github.com上的。如果想要安装新的插件,在“call vundle#rc()" 和 ”filetype plugin indent on"之间添加新的Bundle ‘插件名称’即可。
编辑完成后,在vim下运行下面的命令进行插件安装,请确保你的网络连接正常。
:BundleInstall
如果安装成功,你将看到Done!的提示字样。在本例中,插件:YouCompleteMe, syntastic和ListToggle被安装进你的vim中。
为了让YCM实现语法补全,还需要编译语法补全模块和编辑一个配置文件。
在终端下执行以下命令:
$HOME/.vim/bundle/YouCompleteMe //切换到YCM主目录./install.sh --clang-completer //编译耗时比较久
即可安装语法补全模块。
.ycm_extra_conf.py [重要]这个文件决定了YCM在进行c系语言(c,c++,etc.) 语法补全时的行为。默认的样板配置文件在
$HOME/.vim/bundle/YouCompleteMe/cpp/ycm/.ycm_extra_conf.py
对于YCM来说,当打开一个代码文件时,插件将顺着文件所在的路径一直向上查找,如果搜索到第一个配置文件将立刻读入。如果一直搜索到根目录依旧无法找到配置文件,语法补全将不被启用。由此可知,文件所在目录的配置文件优先级最高,根目录的优先级最低。
"一种比较好的使用方法是:在每个项目中创建一个配置文件,或者将项目根据语言进行分类,在每个语言文件夹下建立一个配置文件。"
默认配置文件是支持c++的,但是需要修改一处地方。可以将该文件拷贝出来并编辑:
cp $HOME/.vim/bundle/YouCompleteMe/third_party/ycmd/cpp/ycm/.ycm_extra_conf.py ~vim ~/.ycm_extra_conf.py
找到以下内容:
# NOTE: This is just for YouCompleteMe; it's highly likely that your project# does NOT need to remove the stdlib flag. DO NOT USE THIS IN YOUR# ycm_extra_conf IF YOU'RE NOT 100% YOU NEED IT.try:final_flags.remove( '-stdlib=libc++' )except ValueError:pass
将之删除后YCM才会补全c++标准库的内容。
这样,一个最小能用的配置文件就出炉了。
在日常使用中,有一些建议修改的变量。本人的.vimrc中对YCM的配置如下:
" YouCompleteMennoremap <leader>jd :YcmCompleter GoToDefinitionElseDeclaration<CR>nnoremap <F5> :YcmForceCompileAndDiagnostics<CR>"let g:ycm_global_ycm_extra_conf = '~/.ycm_extra_conf.py'"Do not ask when starting vimlet g:ycm_confirm_extra_conf = 0let g:syntastic_always_populate_loc_list = 1
其中,g:ycm_global_ycm_extra_conf 定义了全局配置文件,对于懒人来说是个好用的功能。当YCM在向上搜索的路径中找不到配置文件后,YCM将导入全局配置文件。
g:ycm_confirm_extra_conf 决定了在导入配置时是否需要手动确认。设置为0后YCM就不会老是在启动vim的时候来烦你了。
本示例中安装了syntastic插件进行语法检查,因此g:syntastic_always_populate_loc_list设置为1将更方便地使用其功能。又因为本示例安装了ListToggle,打开错误/警告提示面板将变成简单的一个快捷键。
六、我的.vimrc配置备份
""""""""""""""""""""" Vundleset nocompatible " be iMprovedfiletype off " required!set rtp+=~/.vim/bundle/vundle/call vundle#rc()" let Vundle manage Vundle" required!Bundle 'gmarik/vundle'" My bundles here:"" original repos on GitHubBundle 'tpope/vim-fugitive'Bundle 'Lokaltog/vim-easymotion'Bundle 'rstacruz/sparkup', {'rtp': 'vim/'}Bundle 'tpope/vim-rails.git'Bundle 'molokai'" vim-scripts reposBundle 'Valloric/YouCompleteMe'Bundle 'Valloric/ListToggle'"Bundle 'scrooloose/syntastic'Bundle 'L9'Bundle 'FuzzyFinder'" non-GitHub reposBundle 'git://git.wincent.com/command-t.git'" Git repos on your local machine (i.e. when working on your own plugin)" ...filetype plugin indent on " required!""""""""""""""""""""" Vundle" YouCompleteMennoremap <leader>jd :YcmCompleter GoToDefinitionElseDeclaration<CR>" "nnoremap <F5> :YcmForceCompileAndDiagnostics<CR>let g:ycm_global_ycm_extra_conf = '~/.ycm_extra_conf.py'" "Do not ask when starting vimlet g:ycm_confirm_extra_conf = 0let g:syntastic_always_populate_loc_list = 1map <F9> :call SaveInputData()<CR>func! SaveInputData()exec "tabnew"exec 'normal "+gP'exec "w! /tmp/input_data"endfunccolorscheme desert"colorscheme molokai"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""" 显示相关"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""set go= " 不要图形按钮syntax on " 语法高亮autocmd InsertLeave * se nocul " 用浅色高亮当前行autocmd InsertEnter * se cul " 用浅色高亮当前行set showcmd " 输入的命令显示出来,看的清楚些set novisualbell " 不要闪烁(不明白)set statusline=%F%m%r%h%w\ [FORMAT=%{&ff}]\ [TYPE=%Y]\ [POS=%l,%v][%p%%]\ %{strftime(\"%d/%m/%y\ -\ %H:%M\")} "状态行显示的内容set laststatus=1 " 启动显示状态行(1),总是显示状态行(2)set foldenable " 允许折叠set foldmethod=manual " 手动折叠set nocompatible "去掉讨厌的有关vi一致性模式,避免以前版本的一些bug和局限" 显示中文帮助if version >= 603set helplang=cnset encoding=utf-8endif"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""function! s:insert_gates()let gatename = substitute(toupper(expand("%:t")), "\\.", "_", "g")call setline(1, "/*************************************************************************")call append(line("."), " > File Name: ".expand("%"))call append(line(".")+1, " > Author: Dubyoo")call append(line(".")+2, " > Mail:wangwei1543@gmail.com ")call append(line(".")+3, " > Created Time: ".strftime("%c"))call append(line(".")+4, " ************************************************************************/")call append(line(".")+5, "")execute "normal! Go__"execute "normal! Gi#ifndef __" .gatenameexecute "normal! Go__"execute "normal! Gi#define __" .gatenameexecute "normal! Go"execute "normal! Go"execute "normal! Go#endif"normal! kkendfunctionautocmd BufNewFile *.{h,hpp,H} call <SID>insert_gates()""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""新文件标题"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""新建.c,.h,.sh,.java文件,自动插入文件头"autocmd BufNewFile *.cpp,*.[ch],*.sh,*.java exec ":call SetTitle()"autocmd BufNewFile *.cpp,*.c,*.sh,*.java exec ":call SetTitle()"""定义函数SetTitle,自动插入文件头func SetTitle()"如果文件类型为.sh文件if &filetype == 'sh'call setline(1,"\#########################################################################")call append(line("."), "\# File Name: ".expand("%"))call append(line(".")+1, "\# Author: Dubyoo")call append(line(".")+2, "\# mail: wangwei1543@gmail.com")call append(line(".")+3, "\# Created Time: ".strftime("%c"))call append(line(".")+4, "\#########################################################################")call append(line(".")+5, "\#!/bin/bash")call append(line(".")+6, "")elsecall setline(1, "/*************************************************************************")call append(line("."), " > File Name: ".expand("%"))call append(line(".")+1, " > Author: Dubyoo")call append(line(".")+2, " > Mail:wangwei1543@gmail.com ")call append(line(".")+3, " > Created Time: ".strftime("%c"))call append(line(".")+4, " ************************************************************************/")call append(line(".")+5, "")endifif &filetype == 'cpp'call append(line(".")+6, "#include <iostream>")call append(line(".")+7, "using namespace std;")call append(line(".")+8, "")endifif &filetype == 'c'call append(line(".")+6, "#include <stdio.h>")call append(line(".")+7, "")endif" if &filetype == 'java'" call append(line(".")+6,"public class ".expand("%"))" call append(line(".")+7,"")" endif"新建文件后,自动定位到文件末尾autocmd BufNewFile * normal Gendfunc"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""键盘命令""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""nmap <leader>w :w!<cr>nmap <leader>f :find<cr>" 映射全选+复制 ctrl+amap <C-A> ggVGYmap! <C-A> <Esc>ggVGYmap <F12> gg=G" 选中状态下 Ctrl+c 复制vmap <C-c> "+y"去空行nnoremap <F2> :g/^\s*$/d<CR>"比较文件nnoremap <C-F2> :vert diffsplit"新建标签map <M-F2> :tabnew<CR>"列出当前目录文件map <F3> :tabnew .<CR>"打开树状文件目录map <C-F3> \be"C,C++ 按F5编译运行map <F5> :call CompileRunGcc()<CR>func! CompileRunGcc()exec "w"if &filetype == 'c'exec "!g++ % -o %<"exec "! ./%<"elseif &filetype == 'cpp'exec "!g++ % -o %<"exec "! ./%<"elseif &filetype == 'java'exec "!javac %"exec "!java %<"elseif &filetype == 'sh':!./%elseif &filetype == 'py'exec "!python %"exec "!python %<"endifendfunc"C,C++的调试map <F8> :call Rungdb()<CR>func! Rungdb()exec "w"exec "!g++ % -g -o %<"exec "!gdb ./%<"endfunc""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""实用设置"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""" 设置当文件被改动时自动载入set autoread" quickfix模式autocmd FileType c,cpp map <buffer> <leader><space> :w<cr>:make<cr>"代码补全set completeopt=preview,menu"允许插件filetype plugin on"共享剪贴板set clipboard+=unnamed"从不备份set nobackup"make 运行:set makeprg=g++\ -Wall\ \ %"自动保存set autowriteset ruler " 打开状态栏标尺set cursorline " 突出显示当前行set magic " 设置魔术set guioptions-=T " 隐藏工具栏set guioptions-=m " 隐藏菜单栏"set statusline=\ %<%F[%1*%M%*%n%R%H]%=\ %y\ %0(%{&fileformat}\ %{&encoding}\ %c:%l/%L%)\" 设置在状态行显示的信息set foldcolumn=0set foldmethod=indentset foldlevel=3set foldenable " 开始折叠" 不要使用vi的键盘模式,而是vim自己的set nocompatible" 语法高亮set syntax=on" 去掉输入错误的提示声音set noeb" 在处理未保存或只读文件的时候,弹出确认set confirm" 自动缩进set autoindentset cindent" Tab键的宽度set tabstop=4" 统一缩进为4set softtabstop=4set shiftwidth=4" 不要用空格代替制表符set noexpandtab" 在行和段开始处使用制表符set smarttab" 显示行号set number" 历史记录数set history=1000"禁止生成临时文件set nobackupset noswapfile"搜索忽略大小写set ignorecase"搜索逐字符高亮set hlsearchset incsearch"行内替换set gdefault"编码设置set enc=utf-8set fencs=utf-8,ucs-bom,shift-jis,gb18030,gbk,gb2312,cp936"语言设置set langmenu=zh_CN.UTF-8set helplang=cn" 我的状态行显示的内容(包括文件类型和解码)"set statusline=%F%m%r%h%w\ [FORMAT=%{&ff}]\ [TYPE=%Y]\ [POS=%l,%v][%p%%]\ %{strftime(\"%d/%m/%y\ -\ %H:%M\")}"set statusline=[%F]%y%r%m%*%=[Line:%l/%L,Column:%c][%p%%]" 总是显示状态行set laststatus=2" 命令行(在状态行下)的高度,默认为1,这里是2set cmdheight=2" 侦测文件类型filetype on" 载入文件类型插件filetype plugin on" 为特定文件类型载入相关缩进文件filetype indent on" 保存全局变量set viminfo+=!" 带有如下符号的单词不要被换行分割set iskeyword+=_,$,@,%,#,-" 字符间插入的像素行数目set linespace=0" 增强模式中的命令行自动完成操作set wildmenu" 使回格键(backspace)正常处理indent, eol, start等set backspace=2" 允许backspace和光标键跨越行边界set whichwrap+=<,>,h,l" 可以在buffer的任何地方使用鼠标(类似office中在工作区双击鼠标定位)"""""""""""""""""""??????????????"set mouse=a"set selection=exclusive"set selectmode=mouse,key"""""""""""""""""""??????????????" 通过使用: commands命令,告诉我们文件的哪一行被改变过set report=0" 在被分割的窗口间显示空白,便于阅读set fillchars=vert:\ ,stl:\ ,stlnc:\" 高亮显示匹配的括号set showmatch" 匹配括号高亮的时间(单位是十分之一秒)set matchtime=1" 光标移动到buffer的顶部和底部时保持3行距离set scrolloff=3" 为C程序提供自动缩进set smartindent" 高亮显示普通txt文件(需要txt.vim脚本)au BufRead,BufNewFile * setfiletype txt"自动补全:inoremap ( ()<ESC>i:inoremap ) <c-r>=ClosePair(')')<CR>":inoremap { {<CR>}<ESC>O":inoremap } <c-r>=ClosePair('}')<CR>:inoremap [ []<ESC>i:inoremap ] <c-r>=ClosePair(']')<CR>:inoremap " ""<ESC>i:inoremap ' ''<ESC>ifunction! ClosePair(char)if getline('.')[col('.') - 1] == a:charreturn "\<Right>"elsereturn a:charendifendfunctionfiletype plugin indent on"打开文件类型检测, 加了这句才可以用智能补全set completeopt=longest,menu""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""NERDtee设定let NERDChristmasTree=1let NERDTreeAutoCenter=1let NERDTreeBookmarksFile=$VIM.'\Data\NerdBookmarks.txt'let NERDTreeMouseMode=2let NERDTreeShowBookmarks=1let NERDTreeShowFiles=1let NERDTreeShowHidden=1let NERDTreeShowLineNumbers=1let NERDTreeWinPos='left'let NERDTreeWinSize=31nnoremap f :NERDTreeTogglemap <F7> :NERDTree<CR>