一、安装
1)dnf安装
fedora22以下
sudo yum install vim
fedora22及以上
sudo dnf install vim
如果遇到下面类似的错误
file /usr/share/man/man1/vim.1.gz from install of vim-common-2:7.4.1718-1.fc22.x86_64 conflicts with file from package vim-minimal-2:7.4.640-4.fc22.x86_64
运行如下命令,升级vi
sudo dnf update vi
用这种方式安装,vim是在安装在/usr/share/vim
2)源码安装
ftp下载
wget ftp://ftp.vim.org/pub/vim/unix/vim-7.4.tar.bz2
下载到本地的是一个vim-7.4.tar.bz2,所以要解压
解压
tar -jxvf vim-7.4.tar.bz2
解压后会在当前目录得到vim74文件夹,切换到vim74目录
cd vim74
编译前的配置
./configure --prefix=/opt/vim --with-x --enable-gui=gnome2 --with-features=huge --disable-selinux --enable-multibyte
配置选项说明,可以运行./configure --help来查看全部配置选项
--prefix=/opt/vim 安装路径
--with-x --enable-gui=gnome2 生成gvim
--with-features=huge 支持最大特性
--disable-selinux 编译时不检查是否支持selinux
--enable-multibyte 支持多字节
这里会要求安装gcc编译器,如果没安装就安装
sudo dnf install gcc
配置时如果出现下面错误
no terminal library found checking for tgetent()... configure: error: NOT FOUND! You need to install a terminal library; for example ncurses. Or specify the name of the library with --with-tlib.
这是没安装ncurses库,运行如下命令安装
sudo dnf install ncurses-devel
需要重新配置可 运行如下命令,清理上一次编译生成的所有文件
make distclean
配置后,编译并且安装
sudo make && make install
建立软连接,即把刚刚安装路径加入到系统环境变量
mv /usr/bin/vim /usr/bin/vim_backup # 先将原来的备份 ln -s /opt/vim/bin/vim /usr/bin/vim # 建立软链接
git下载
git clone https://github.com/vim/vim.git
克隆之后会在当前目录得到vim文件夹,切换到vim目录后,就和ftp下载的编译一样
cd vim
二、配置
准备工作
先在home目录下,新建.vim目录和.vimrc文件,vim默认配置文件是/etc/vimrc,这个是作用整个系统的,所以一般不改这个
切换到当前用户的home目录
cd ~
新建.vim目录和.vimrc文件
mkdir .vim touch .vimrc
切换到.vim目录,然后新建bundle目录
cd .vim mkdir bundle
切换到bundle,从github克隆vundle到本地
cd bundle git clone https://github.com/gmarik/vundle.git
切换到home目录,编辑.vimrc配置文件
cd ~
vi .vimrc
我的配置文件
插件说明
(1)vim-airline
vim-airline这个插件要用到特定的字体才能显示特定的符号
运行下面命令下载字体
git clone https://github.com/powerline/fonts.git
克隆之后会在当前目录得到fonts文件夹,切换到fonts,运行./install.sh安装字体
cd fonts
./install.sh
打开终端,右键Profiles->Profiles Preferences,打开后就可以选择终端字体了
(2)cscope
先查看vim是否开启了cscope这个特性,运行如下命令,看到+cscope就表示开启了,看到-cscope就表示没开启
vim --version | grep cscope
如果没开启就重新编译vim,切换到下载vim源码目录,然后运行如下命令
./configure --enable-cscope sudo make && make install
cscope的安装,如果没安装的可以用下面命令安装
sudo dnf install cscope
或者到https://sourceforge.net/projects/cscope/files/latest/download?source=files下载,下载后会得到cscope-15.8b.tar.gz压缩包,解压
tar -zxvf cscope-15.8b.tar.gz
解压后得到cscope-15.8b文件夹,切换到cscope-15.8b目录,然后配置,编译,安装
cd cscope-15.8b ./configure --prefix=/opt/cscope
sudo make && make install
cscope默认只解析C语言文件(即后缀为.c和.h文件)、lex文件(即后缀为.l文件)和yacc文件(即后缀为.y文件),所以要解析其他语言的文件,需要把这些文件名和完整路径保存在cscope.files这个文件,我们可以在项目的根目录运行下面命令来生成cscope.files
find . -type f > cscope.files
然后在生成cscope数据库文件
cscope -bq
运行上面命令后,会在当前目录生成cscope.out,cscope.in.out,cscope.po.out这3个文件
cscope常用的选项
-R: 递归生成数据库文件
-b: 只生成数据库文件,不进入cscope的界面
-q: 生成cscope.in.out和cscope.po.out文件,加快cscope的索引速度
-k: 在生成数据库文件时,不搜索/usr/include目录
-i: 如果保存文件列表的文件名不是cscope.files时,需要加此选项告诉cscope到哪儿去找源文件列表。可以使用”–“,表示由标准输入获得文件列表。
-I incdir: 在-I选项指出的目录中查找头文件
-u: 扫描所有文件,重新生成交叉数据库文件
-C: 在搜索时忽略大小写
-P path: 在以相对路径表示的文件前加上的path,这样,你不用切换到你数据库文件所在的目录也可以使用它了
我的vim配置文件
"使用vundle来管理插件,要求filetype off,set nocompatible " This is not stage " 侦测文件类型 filetype off "关闭兼容模式 set nocompatible "定义<leader> let mapleader="," "vundle的配置必须要放在vimrc文件的最前面,只有vundle插件运行之后, "安装vundle "git clone https://github.com/gmarik/vundle.git ~/.vim/bundle/vundle "这样才能加载其他插件 "运行时路径,runtimepath set rtp+=~/.vim/bundle/vundle/ "vundle开始配置 call vundle#rc() "让vundle管理插件 Bundle 'gmarik/vundle' "自定义的插件 "github上其他用户的插件,按下面格式写,github用户名/插件名 "Bundle 'tpope/vim-fugitive' Bundle 'altercation/vim-colors-solarized' Bundle 'kien/ctrlp.vim' Bundle 'vim-airline/vim-airline' Bundle 'vim-airline/vim-airline-themes' Bundle 'majutsushi/tagbar' Bundle 'Valloric/YouCompleteMe' Bundle 'SirVer/ultisnips' Bundle 'tomasr/molokai' Bundle 'scrooloose/nerdtree' Bundle 'scrooloose/syntastic' Bundle 'ervandew/supertab' Bundle 'honza/vim-snippets' Bundle 'mattn/emmet-vim' Bundle 'tpope/vim-fugitive' Bundle 'spf13/PIV' Bundle 'tpope/vim-surround' Bundle 'Raimondi/delimitMate' Bundle 'scrooloose/nerdcommenter' Bundle 'godlygeek/tabular' Bundle 'easymotion/vim-easymotion' Bundle 'joonty/vdebug' Bundle 'airblade/vim-gitgutter' Bundle 'Shougo/neocomplete.vim' Bundle 'kien/rainbow_parentheses.vim' Bundle 'sjl/gundo.vim' "aaaaa,github上的vim-scripts用户的插件,按下面格式写,直接写插件名 Bundle 'taglist.vim' Bundle 'TaskList.vim' "不是github上的,按下面格式写 "Bundle 'git://git.wincent.com/command-t.git' "Brief help ":BundleList -显示vimrc里面填写的所有插件名称 ":BundleInstall(!) -自动下载安装或更新插件 ":BundleSearch(!) foo -搜索插件foo ":BundleClean(!) -移出插件 ":help vundle -查看帮助 "End brief help """""""""""""""" """"配置插件"""" """""""""""""""" "ctrlp插件,g:代表全局变量,l:代表本地变量,s:代表脚本变量 let g:ctrlp_map='<c-p>' let g:ctrlp_cmd='CtrlP' let g:ctrlp_working_path_mode='ra' "vim-airline插件 set laststatus=2 set t_Co=256 let g:airline_theme="luna" let g:airline_powerline_fonts=1 let g:airline#extension#tabline#enabled=1 let g:airline#extension#tabline#left_sep=' ' let g:airline#extension#tabline#left_alt_sep='|' "nerdtree插件 map <F2> :NERDTreeToggle<CR> "taglist插件 let Tlist_Auto_update=1 let Tlist_Auto_open=0 let Tlist_Use_Right_Window=1 let Tlist_Show_One_File=0 let Tlist_File_Fold_Auto_Close=0 let Tlist_Exit_OnlyWindow=1 map <F4> :TlistToggle<CR> "TaskList插件 "map <leader>td <Plug>TaskList "tagbar插件 map <F3> :TagbarToggle<CR> "ctags set tags=tags; "syntastic插件 set statusline+=%#warningmsg# set statusline+=%{SyntasticStatuslineFlag()} set statusline+=%* let g:syntastic_always_populate_loc_list=1 let g:syntastic_auto_loc_list=1 let g:syntastic_check_on_open=1 let g:syntastic_check_on_wq=0 "vim-fugitive插件 ":help fugitive查看帮助 "PIV插件 "shift+k查看PHP手册 "nerdcommenter插件 "一些快捷键 "<leader>cc 注释 "<leader>cu 反注释 "<leader>c<space> 注释开关 "<leader>cs 块注释 "rainbow_parentheses插件 let g:rbpt_colorpairs = [ \['brown','RoyalBlue3'], \['Darkblue','SeaGreen3'], \['darkgray','DarkOrchid3'], \['darkgreen','firebrick3'], \['darkcyan','RoyalBlue3'], \['darkred','SeaGreen3'], \['darkmagenta','DarkOrchid3'], \['brown','firebrick3'], \['gray','RoyalBlue3'], \['black','SeaGreen3'], \['darkmagenta','DarkOrchid3'], \['Darkblue','firebrick3'], \['darkgreen','RoyalBlue3'], \['darkcyan','SeaGreen3'], \['darkred','DarkOrchid3'], \['red','firebrick3'], \] let g:rbpt_max=16 let g:rbpt_loadcmd_toggle=1 au VimEnter * RainbowParenthesesToggle au Syntax * RainbowParenthesesLoadRound au Syntax * RainbowParenthesesLoadSquare au Syntax * RainbowParenthesesLoadBraces "cscope插件 "cs find c|d|e|f|g|i|s|t name "0 或 s 查找本 C 符号(可以跳过注释) "1 或 g 查找本定义 "2 或 d 查找本函数调用的函数 "3 或 c 查找调用本函数的函数 "4 或 t 查找本字符串 "6 或 e 查找本 egrep 模式 "7 或 f 查找本文件 "8 或 i 查找包含本文件的文件 if has("cscope") ":tag,ctrl+],vim -t等命令会始终使用:cstag而不是:tag set cst "是否使用quickfix窗口来显示cscope结果 set cscopequickfix=s-,c-,d-,i-,t-,e- "指定cscope的路径 set csprg=/usr/bin/cscope "查找顺序,0为先找cscope数据,找不到再找tag文件 set csto=0 "增加cscope数据库时会提示信息 set nocsverb "显示查找结果的路径的最后3个部分,包含本文件名 set cspc=3 "add any database in current directory if filereadable("cscope.out") cs add cscope.out "else add database pointed to by environment elseif $CSCOPE_DB != "" cs add $CSCOPE_DB endif set csverb "定义一些快捷键 nmap <C-\>s :cs find s <C-R>=expand("<cword>")<CR><CR> nmap <C-\>g :cs find g <C-R>=expand("<cword>")<CR><CR> nmap <C-\>c :cs find c <C-R>=expand("<cword>")<CR><CR> nmap <C-\>t :cs find t <C-R>=expand("<cword>")<CR><CR> nmap <C-\>e :cs find e <C-R>=expand("<cword>")<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> nmap <C-\>d :cs find d <C-R>=expand("<cword>")<CR><CR> "查找结果,水平分割窗口 nmap <C-@>s :scs find s <C-R>=expand("<cword>")<CR><CR> nmap <C-@>g :scs find g <C-R>=expand("<cword>")<CR><CR> nmap <C-@>c :scs find c <C-R>=expand("<cword>")<CR><CR> nmap <C-@>t :scs find t <C-R>=expand("<cword>")<CR><CR> nmap <C-@>e :scs find e <C-R>=expand("<cword>")<CR><CR> nmap <C-@>f :scs find f <C-R>=expand("<cfile>")<CR><CR> nmap <C-@>i :scs find i ^<C-R>=expand("<cfile>")<CR>$<CR> nmap <C-@>d :scs find d <C-R>=expand("<cword>")<CR><CR> "查找结果,竖直分割窗口 nmap <C-@><C-@>s :vert scs find s <C-R>=expand("<cword>")<CR><CR> nmap <C-@><C-@>g :vert scs find g <C-R>=expand("<cword>")<CR><CR> nmap <C-@><C-@>c :vert scs find c <C-R>=expand("<cword>")<CR><CR> nmap <C-@><C-@>t :vert scs find t <C-R>=expand("<cword>")<CR><CR> nmap <C-@><C-@>e :vert scs find e <C-R>=expand("<cword>")<CR><CR> nmap <C-@><C-@>f :vert scs find f <C-R>=expand("<cfile>")<CR><CR> nmap <C-@><C-@>i :vert scs find i ^<C-R>=expand("<cfile>")<CR>$<CR> nmap <C-@><C-@>d :vert scs find d <C-R>=expand("<cword>")<CR><CR> endif "YouCompleteMe插件 let g:ycm_key_list_select_completion=['<C-n>','<Down>'] let g:ycm_key_list_previous_completion=['<C-p>','<Up>'] let g:ycm_complete_in_comments=1 "补全功能在注释中同样有效 let g:ycm_confirm_extra_conf=0 "允许vim加载.ycm_extra_conf.py文件,不再提示 let g:ycm_collect_identifiers_from_tags_files=1 "开启ycm基于标签引擎 let g:ycm_min_num_of_chars_for_completion=1 "从第一个键入字符就开始罗列匹配项 let g:ycm_cache_omnifunc=0 "禁止缓存匹配项,每次都重新生成匹配项 let g:ycm_seed_identifiers_wity_syntax=1 "语法关键字补全 let g:ycm_error_symbol='>>' "错误提示符 let g:ycm_warning_symbol='>*' "警告提示符 set completeopt-=preview "补全内容不以分割子窗口形式出现,只显示补全列表 nnoremap <leader>jd :YcmCompleter GoToDefinitionElseDeclaration<CR> "ultisnips插件 let g:UltiSnipsExpandTrigger="<tab>" let g:UltiSnipsJumpForwardTrigger="<tab>" let g:UltiSnipsJumpBackwardTrigger="<s-tab>" "supertab插件 let g:SuperTabDefaultCompleteType='<C-n>' "neocomplete插件 "Note: This option must set it in .vimrc(_vimrc). NOT IN .gvimrc(_gvimrc)! "" Disable AutoComplPop. let g:acp_enableAtStartup = 0 " Use neocomplete. let g:neocomplete#enable_at_startup = 1 " Use smartcase. let g:neocomplete#enable_smart_case = 1 " Set minimum syntax keyword length. let g:neocomplete#sources#syntax#min_keyword_length = 3 let g:neocomplete#lock_buffer_name_pattern = '\*ku\*' " Define dictionary. let g:neocomplete#sources#dictionary#dictionaries = { \ 'default' : '', \ 'vimshell' : $HOME.'/.vimshell_hist', \ 'scheme' : $HOME.'/.gosh_completions' \ } " Define keyword. if !exists('g:neocomplete#keyword_patterns') let g:neocomplete#keyword_patterns = {} endif let g:neocomplete#keyword_patterns['default'] = '\h\w*' " Plugin key-mappings. inoremap <expr><C-g> neocomplete#undo_completion() inoremap <expr><C-l> neocomplete#complete_common_string() " Recommended key-mappings. " <CR>: close popup and save indent. inoremap <silent> <CR> <C-r>=<SID>my_cr_function()<CR> function! s:my_cr_function() return (pumvisible() ? "\<C-y>" : "" ) . "\<CR>" " For no inserting <CR> key. " return pumvisible() ? "\<C-y>" : "\<CR>" endfunction " <TAB>: completion. inoremap <expr><TAB> pumvisible() ? "\<C-n>" : "\<TAB>" " <C-h>, <BS>: close popup and delete backword char. inoremap <expr><C-h> neocomplete#smart_close_popup()."\<C-h>" inoremap <expr><BS> neocomplete#smart_close_popup()."\<C-h>" " Close popup by <Space>. "inoremap <expr><Space> pumvisible() ? "\<C-y>" : "\<Space>" " AutoComplPop like behavior. let g:neocomplete#enable_auto_select = 1 " Shell like behavior(not recommended). "set completeopt+=longest "let g:neocomplete#enable_auto_select = 1 "let g:neocomplete#disable_auto_complete = 1 "inoremap <expr><TAB> pumvisible() ? "\<Down>" : "\<C-x>\<C-u>" " Enable omni completion. autocmd FileType css setlocal omnifunc=csscomplete#CompleteCSS autocmd FileType html,markdown setlocal omnifunc=htmlcomplete#CompleteTags autocmd FileType javascript setlocal omnifunc=javascriptcomplete#CompleteJS autocmd FileType python setlocal omnifunc=pythoncomplete#Complete autocmd FileType xml setlocal omnifunc=xmlcomplete#CompleteTags autocmd FileType php setlocal omnifunc=phpcomplete#CompletePHP " Enable heavy omni completion. if !exists('g:neocomplete#sources#omni#input_patterns') let g:neocomplete#sources#omni#input_patterns = {} endif "let g:neocomplete#sources#omni#input_patterns.php = '[^. \t]->\h\w*\|\h\w*::' "let g:neocomplete#sources#omni#input_patterns.c = '[^.[:digit:] *\t]\%(\.\|->\)' "let g:neocomplete#sources#omni#input_patterns.cpp = '[^.[:digit:] *\t]\%(\.\|->\)\|\h\w*::' " For perlomni.vim setting. " https://github.com/c9s/perlomni.vim let g:neocomplete#sources#omni#input_patterns.perl = '\h\w*->\h\w*\|\h\w*::' """""""""""""""" """"公共设置"""" """""""""""""""" "基于缩进或语法进行代码缩进 set foldenable "set foldmethod=indent "set foldmethod=syntax "set foldmethod=manual set foldmethod=indent "设置历史记录数 set history=1000 "在状态行上显示光标所在位置的行号和列号 set ruler "显示行号 set number "输入的命令显示出来 set showcmd "在状态栏显示当前的模式 set showmode "命令行的高度 set cmdheight=2 "光标移动到buffer的顶部和底部时保持3行距离 set scrolloff=3 "在被分割的窗口间显示空白,便于阅读 "set fillchars=vert:/,stl:/,stlnc:/ "关闭滴滴答答的声音 set noerrorbells "不要闪烁 set novisualbell "命令错误时,去掉响声 set t_vb= "启动时不显示援助乌干达儿童的提示 set shortmess=atI " 语法高亮 syntax enable syntax on "配色方案为molokai colorscheme molokai "背景颜色 set background=dark "突出显示当前行 "set cursorline "突出显示当前列 "set cursorcolumn "回格键删除2个空格 set backspace=2 "允许backspace和光标键跨越行边界 set whichwrap+=<,>,h,l "按一次backspace就删除4个空格了 set smarttab "程序自动缩进的空白宽度,4个空格 set shiftwidth=4 "tab键的宽度,4个空格 set tabstop=4 "输入tab键时实际占有的宽度 set softtabstop=4 "输入tab键时替换为空格键 set expandtab "自动缩进 set autoindent "使用C/C++时自动缩进 set cindent "智能缩进 set smartindent "不生成备份文件,如index.html~ set nobackup "不生成临时文件,如index.swap set noswapfile "设置文件被修改时自动载入 set autoread "自动保存 set autowrite "根据编辑的文件自动切换工作目录 set autochdir "当前编辑的文件编码 set fileencoding=urf-8 "gvim打开支持编码的文件 set fileencodings=ucs-bom,utf-8,gbk,cp936,gb2312,big5,euc-jp,euc-kr,latin1 "gvim内部编码 set encoding=utf-8 "设置终端编码为gvim内部编码encoding let &termencoding=&encoding "设置文件格式 set fileformats=unix,dos,mac " 载入文件类型插件 filetype plugin on " 为特定文件类型载入相关缩进文件 filetype indent on "高亮显示匹配的括号 set showmatch "匹配括号高亮的时间,单位是十分之一秒 set matchtime=2 "搜索内容高亮显示 set hlsearch "实时匹配搜索内容 set incsearch "搜索时忽略大小写 set ignorecase "搜索时智能忽略大小写 set smartcase "搜索时可以用正则表达式 set magic "当运行宏时,不重绘 set lazyredraw "搜索到文件两端时不重新搜索 set nowrapscan "带有下面字符的单词不要被换行分割 set iskeyword+=_,$,@,%,#,-,. "gvim 配置 if has("gui_running") colorscheme solarized endif "设置字体 "set guifont=Bitstream_Vera_Sans_Mono:h12:cANSI "set guifontwide=Yahei_Mono:h12:cGB2312 set guifont=Source\ Code\ Pro\ for\ Powerline\ Semibold\ 12 "设置gvim工具栏 "set guioptions=aegic "使用中文帮助文档 set helplang=cn "解决consle输出乱码 language messages zh_CN.utf-8 "防止特殊符号无法正常显示 set ambiwidth=double "关闭上侧工具栏 "set guioptions-=T "关闭右侧滚动条 "set guioptions-=r