Ubuntu-14.0.1中将vim改装为python和c++的IDE

简介

  这篇博客主要记录了将ubuntu14中的vim配置成c++和python的IDE。有代码提示,补全,查错的功能。主要是使用了YouCompleteMe。在配置过程中遇到了不少问题。因此简单地记录下来,供后续的参考。

一、Vundle的安装

  按照官网安装即可:
  下载源码,输入以下命令

git clone https://github.com/VundleVim/Vundle.vim.git ~/.vim/bundle/Vundle.vim

  然后在用户的根目录下的.vimrc文件中加入以下内容(必须加在顶行),相对于官网,去掉了一些不必要的组件。

set nocompatible
filetype off 
set rtp+=~/.vim/bundle/Vundle.vim
call vundle#begin()
Plugin 'VundleVim/Vundle.vim'
call vundle#end()            " required
filetype plugin indent on    " required

  然后打开vim,输入以下命令

:PluginInstall

  在左下角显示done!后即安装完成

二、安装vim-8.0

 说明

  因为后续的YouCompleteMe的安装要求vim的版本最少为7.4.1578。而ubuntu14中的版本不够(vim –version查看版本),因此这里通过下载vim-8.0的源码来编译安装。
下载源码并设置安装参数(将源码下载到/home/wanghao/software-floder。安装到的位置为/home/wanghao/software/vim-8.0)。注意这些路径的提前创建好。

cd software-floder
git clone https://github.com/vim/vim.git
cd vim
./configure --with-features=huge \
            --enable-multibyte \
            --enable-rubyinterp=yes \
            --enable-pythoninterp=yes \
            --with-python-config-dir=/usr/lib/python2.7/config \
            --enable-python3interp=yes \
            --with-python3-config-dir=/usr/lib/python3.5/config \
            --enable-perlinterp=yes \
            --enable-luainterp=yes \
            --enable-gui=gtk2 \
            --enable-cscope \
            --prefix=/usr/local
make VIMRUNTIMEDIR=/usr/local/share/vim/vim80

  特别注意:在ubuntu14中,python版本要求为2.6+或者3.3+。两种版本只能配置一个。还有如果python是通过anaconda进行安装的话,注意相应的修改。这一步很重要,不然后面陆续报错,说YouCompleteMe不能用,当发生错误重新编译此步时,记得删除以前编译的,再重新编译。因此,我的配置中为如下。
  如果系统是刚刚安装,可能出现如下问题
checking –with-tlib argument… empty: automatic terminal library selection
checking for tgetent in -lncurses… no
checking for tgetent in -ltermlib… no
checking for tgetent in -ltermcap… no
checking for tgetent in -lcurses… no
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.

  解决如下:sudo apt-get install libncurses5-dev

cd software-floder
git clone https://github.com/vim/vim.git
cd vim
./configure --with-features=huge \
            --enable-multibyte \
            --enable-rubyinterp=yes \
            --enable-pythoninterp=yes \
            --with-python-config-dir=/home/wanghao/software/anaconda2/lib/python2.7/config \
            --enable-perlinterp=yes \
            --enable-luainterp=yes \
            --enable-gui=gtk2 \
            --enable-cscope \
            --prefix=/home/wanghao/software/vim-8.0
make VIMRUNTIMEDIR=/home/wanghao/software/vim-8.0/share/vim/vim80

  然后就是安装命令

make install

  安装完成,但是还得修改PATH路径,让系统可以找到vim的执行文件(在.bashrc下修改)

#在.bashrc文件中加入以下语句并保存
export PATH="/home/wanghao/software/vim-8.0/bin:$PATH"

  .bashrc修改完后使用以下命令使得该文件生效

source .bashrc

  此时在查看vim的版本发现为8.0(vim –version)

三、安装LLVM

 说明

  因为后续的YouCompleteMe在对c++进行语义解释时需要用到libclang,因此这里直接使用事先编译好的文件。下载指令为(还是下载在software-floder)

cd software-floder
wget http://releases.llvm.org/4.0.0/clang+llvm-4.0.0-x86_64-linux-gnu-ubuntu-14.04.tar.xz

  解压指令为

xz -d clang+llvm-4.0.0-x86_64-linux-gnu-ubuntu-14.04.tar.xz
tar -xvf clang+llvm-4.0.0-x86_64-linux-gnu-ubuntu-14.04.tar

  将解压的文件转移到software目录下(只是为了软件结构的统一而已,执行文件都在software下,源码的下载包在software-floder下),命令在software-floder下执行

cp -r clang+llvm-4.0.0-x86_64-linux-gnu-ubuntu-14.04 ~/software/

四、安装YouCompleteMe

 说明

  YouCompleteMe的下载包由Vundle管理,下载完后将其编译到software的ycm_build文件夹下。因此,事先建立ycm_build文件夹。

  通过Vundle下载YouCompleteMe
  在.vimrc下加入如下语句

Plugin 'Valloric/YouCompleteMe'

  然后打开vim,输入如下指令(时间稍微有点长,等待…….)

:PluginIntall

  直至为done!为止。
  然后输入以下命令进行编译安装(注意以下命令在ycm_build下执行)

cmake -G "Unix Makefiles" -DUSE_SYSTEM_BOOST=ON -DPATH_TO_LLVM_ROOT=/home/wanghao/software/clang+llvm-4.0.0-x86_64-linux-gnu-ubuntu-14.04/ .\
 ~/.vim/bundle/YouCompleteMe/third_party/ycmd/cpp

cmake --build . --target ycm_core

 YouCompleteMe的一些常用配置(在.vimrc中添加)

" 补全功能在注释中同样有效
let g:ycm_complete_in_comments=1
" 允许 vim 加载 .ycm_extra_conf.py 文件,不再提示
let g:ycm_confirm_extra_conf=0
" 开启 YCM 标签补全引擎
let g:ycm_collect_identifiers_from_tags_files=1
" 引入 C++ 标准库tags
set tags+=/data/misc/software/misc./vim/stdcpp.tags
" YCM 集成 OmniCppComplete 补全引擎,设置其快捷键
inoremap <leader>; <C-x><C-o>
" 补全内容不以分割子窗口形式出现,只显示补全列表
set completeopt-=preview
" 从第一个键入字符就开始罗列匹配项
let g:ycm_min_num_of_chars_for_completion=1
" 禁止缓存匹配项,每次都重新生成匹配项
let g:ycm_cache_omnifunc=0
" 语法关键字补全           
let g:ycm_seed_identifiers_with_syntax=1

 YouCompleteMe的项目配置文件

  文件名为.ycm_extra_conf.py 。将此文件放入到工程的文件夹或者上层文件夹里,系统会自动搜索。.ycm_extra_conf.py(注意下载后,文件名最前面的小数点可能被丢失,得加上前面的小数点)

import os 
import ycm_core 
flags = [ 
    '-std=c++11', 
    '-O0', 
    '-Werror', 
    '-Weverything', 
    '-Wno-documentation', 
    '-Wno-deprecated-declarations', 
    '-Wno-disabled-macro-expansion', 
    '-Wno-float-equal', 
    '-Wno-c++98-compat', 
    '-Wno-c++98-compat-pedantic', 
    '-Wno-global-constructors', 
    '-Wno-exit-time-destructors', 
    '-Wno-missing-prototypes', 
    '-Wno-padded', 
    '-Wno-old-style-cast',
    '-Wno-weak-vtables',
    '-x', 
    'c++', 
    '-I', 
    '.', 
    '-isystem', 
    '/usr/include/', 
] 
compilation_database_folder = '' 
if compilation_database_folder: 
  database = ycm_core.CompilationDatabase( compilation_database_folder ) 
else: 
  database = None 
SOURCE_EXTENSIONS = [ '.cpp', '.cxx', '.cc', '.c', '.m', '.mm' ] 
def DirectoryOfThisScript(): 
  return os.path.dirname( os.path.abspath( __file__ ) ) 
def MakeRelativePathsInFlagsAbsolute( flags, working_directory ): 
  if not working_directory: 
    return list( flags ) 
  new_flags = [] 
  make_next_absolute = False 
  path_flags = [ '-isystem', '-I', '-iquote', '--sysroot=' ] 
  for flag in flags: 
    new_flag = flag 
    if make_next_absolute: 
      make_next_absolute = False 
      if not flag.startswith( '/' ): 
        new_flag = os.path.join( working_directory, flag ) 
    for path_flag in path_flags: 
      if flag == path_flag: 
        make_next_absolute = True 
        break 
      if flag.startswith( path_flag ): 
        path = flag[ len( path_flag ): ] 
        new_flag = path_flag + os.path.join( working_directory, path ) 
        break 
    if new_flag: 
      new_flags.append( new_flag ) 
  return new_flags 
def IsHeaderFile( filename ): 
  extension = os.path.splitext( filename )[ 1 ] 
  return extension in [ '.h', '.hxx', '.hpp', '.hh' ] 
def GetCompilationInfoForFile( filename ): 
  if IsHeaderFile( filename ): 
    basename = os.path.splitext( filename )[ 0 ] 
    for extension in SOURCE_EXTENSIONS: 
      replacement_file = basename + extension 
      if os.path.exists( replacement_file ): 
        compilation_info = database.GetCompilationInfoForFile( replacement_file ) 
        if compilation_info.compiler_flags_: 
          return compilation_info 
    return None 
  return database.GetCompilationInfoForFile( filename ) 
def FlagsForFile( filename, **kwargs ): 
  if database: 
    compilation_info = GetCompilationInfoForFile( filename ) 
    if not compilation_info: 
      return None 
    final_flags = MakeRelativePathsInFlagsAbsolute( 
      compilation_info.compiler_flags_, 
      compilation_info.compiler_working_dir_ ) 
  else: 
    relative_to = DirectoryOfThisScript() 
    final_flags = MakeRelativePathsInFlagsAbsolute( flags, relative_to ) 
  return { 
    'flags': final_flags, 
    'do_cache': True 
  }

五、安装NERDTree

 说明

  直接在Vundle下安装即可

  在.vimrc文件中加入以下语句

Plugin 'scrooloose/nerdtree'

  然后打开vim,输入以下命令

:PluginInstall

  done!则表示安装完成。

NERDTree的一些常用配置(在.vimrc中添加)

  一般情况下在vim的命令行输入:NERDTree便可打开文件浏览功能。为了方便,给此功能增加快捷键

inoremap <C-z> <ESC>:NERDTree<CR><insert>
nnoremap <C-z> :NERDTree<CR>

六、给vim添加一个比较好看的主题

  资源下载space-vim-dark

syntax on
colorscheme space-vim-dark
set t_Co=256

七、最后的效果图如下

这里写图片描述

参考文献

像 IDE 一样使用 vim

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值