Ubuntu16.04安装vim8并支持python3.7

Ubuntu16.04安装vim8并支持python3.7

在Ubuntu16.04下编译安装vim8,并配置vim-plug插件管理器,以及安装 YouCompleteMe 等插件。

安装依赖

这一步根据个人情况安装:

sudo apt-get install libncurses5-dev libgnome2-dev libgnomeui-dev \
    libgtk2.0-dev libatk1.0-dev libbonoboui2-dev \
    libcairo2-dev libx11-dev libxpm-dev libxt-dev python-dev \
    python3-dev git

如果需要 ruby、lua、perl 支持则还需要:

ruby-dev liblua5.1 lua5.1-dev libperl-dev

需要注意的是在Ubuntu16.04中Lua应该为liblua5.1-dev,而在其它版本中应为lua5.1-dev

删除已有vim相关包(可选)

如果是多人使用的机器,最好还是不要轻易apt删除vim,其他人可能受到影响。如果是只有自己一个用户,可以随性:

dpkg -l | grep vim
sudo apt remove vim vim-common vim-runtime vim-tiny

下载源码

git clone https://gitee.com/vim/vim.git ~/work/github/vim/vim
cd $_
git checkout -b v8.2.2365 v8.2.2365

简易编译

这里安装到个人路径;考虑到远程xshell使用,没有gui,则编译时需要--disable-gui以及--without-x;简单起见,用的系统自带的Python2
(此条记录是2018年的,仅作参考)

INSTALL_PREFIX=/usr/local/vim8

./configure --with-features=huge \
            --enable-multibyte \
            --enable-rubyinterp=yes \
            --enable-pythoninterp=yes \
            --with-python-config-dir=/usr/lib/python2.7/config \
            --enable-perlinterp=yes \
            --enable-luainterp=yes \
            --enable-cscope \
            --disable-gui \
            --without-x \
            --prefix=$INSTALL_PREFIX

make -j$(nproc) VIMRUNTIMEDIR=$INSTALL_PREFIX/share/vim/vim82
sudo make install

带python3.7的编译

上述配置参数,没指定Python3支持,会导致最新版 YouCompleteMe 无法使用;如果是 PPA 安装的 vim8.2 用的 Python 是 3.5 也导致 YCM 无法使用。

安装python3.7

从 PPA 安装 Python 3.7:

sudo add-apt-repository ppa:deadsnakes/ppa
sudo apt-get update

sudo apt install -y python3.7-dev
sudo apt install -y libpython3.7-dev

顺带把系统python和python3更新为3.7:

sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.5 1
sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.7 2
sudo update-alternatives --install /usr/bin/python python /usr/bin/python3.7 2
sudo update-alternatives --install /usr/bin/python python /usr/bin/python3.5 1

清理编译缓存

sudo make distclean

带python3.7支持的vim编译

#!/bin/bash

INSTALL_PREFIX=/usr/local/vim8

./configure --with-features=huge \
            --enable-multibyte \
            --enable-pythoninterp=yes \
            --enable-pythoninterp=no \
            --enable-python3interp=yes \
            --with-python3-command=/usr/bin/python3 \
            --with-python3-config-dir=/usr/lib/python3.7/config-3.7m-x86_64-linux-gnu \
            --enable-rubyinterp=no \
            --enable-perlinterp=no \
            --enable-luainterp=no \
            --enable-cscope \
            --disable-gui \
            --without-x \
            --enable-fail-if-missing \
            --prefix=$INSTALL_PREFIX


make -j8 VIMRUNTIMEDIR=$INSTALL_PREFIX/share/vim/vim82
sudo make install

其中--enable-python3interp=yes开启了python3支持,--with-python3-command=/usr/bin/python3指定了python3解释器路径,--with-python3-config-dir=/usr/lib/python3.7/config-3.7-x86_64-linux-gnu指定了Python3的config目录。当然,最重要的细节有两个:

  1. configure时指定--enable-fail-if-missing用于显示错误信息,借助它,才发现先前Python的配置没成功,导致即使编译安装了,vim --version | ag 'python' 有结果的情况下也没法用 vim 的 Python 支持 的问题;
  2. make时候指定VIMRUNTIMEDIR变量,否则编译安装的vim仍然不能正常用

配置vim

不配置vim的话会比较难用,增加一些插件和个人习惯性的定制能提升编码效率。

在下载安装vim插件这件事儿上,Vundle很慢,基于异步的vim-plug则非常快。上手吧:vim ~/.vimrc, 输入vim指令:set paste,将如下内容粘贴到.vimrc中:

" Specify a directory for plugins
" - For Neovim: ~/.local/share/nvim/plugged
" - Avoid using standard Vim directory names like 'plugin'
call plug#begin('~/.vim/plugged')

" Make sure you use single quotes

" Shorthand notation; fetches https://github.com/junegunn/vim-easy-align
Plug 'junegunn/vim-easy-align'

" Any valid git URL is allowed
Plug 'https://github.com/junegunn/vim-github-dashboard.git'

" Multiple Plug commands can be written in a single line using | separators
Plug 'SirVer/ultisnips' | Plug 'honza/vim-snippets'

" On-demand loading
Plug 'scrooloose/nerdtree', { 'on':  'NERDTreeToggle' }
Plug 'tpope/vim-fireplace', { 'for': 'clojure' }

" Using a non-master branch
Plug 'rdnetto/YCM-Generator', { 'branch': 'stable' }

" Using a tagged release; wildcard allowed (requires git 1.9.2 or above)
Plug 'fatih/vim-go', { 'tag': '*' }

" Plugin options
Plug 'nsf/gocode', { 'tag': 'v.20150303', 'rtp': 'vim' }

" Plugin outside ~/.vim/plugged with post-update hook
Plug 'junegunn/fzf', { 'dir': '~/.fzf', 'do': './install --all' }
Plug 'Valloric/YouCompleteMe', { 'do': './install.py' }

" Unmanaged plugin (manually installed and updated)
Plug '~/my-prototype-plugin'

" Initialize plugin system
call plug#end()

"" 个人定制的一些配置,不喜欢、不习惯可以自行修改
set nu
set tags=./.tags;.tags
set cursorline
set showmatch
set hlsearch
set incsearch
hi CursorLine cterm=NONE ctermbg=black ctermfg=green guibg=NONE guifg=NONE

关闭vim,再次vim ~/.vimrc,敲入:PlugInstall,开始下载插件:可以看到各个插件是同时在下载的,速度很快。

配置 YouCompleteMe

YouCompleteMe 因为有很多submodule,如果网络不好,很容易失败。不过我在 https://gitee.com/ycm-core 上拉取了 YouComplete 的所有依赖,改掉 .gitmodules 里面的内容后,拉取即可:

cd ~/.vim/plugged/YouCompleteMe

# 修改 .gitmodules,把url改成 https://gitee.com/ycm-core开头的

git submodule update --init

cd third_party/ycmd

# 再次修改 .gitmodules,把url改成 https://gitee.com/ycm-core开头的

/usr/bin/python3 install.py --clangd  # 用系统的 Python3执行的安装,而不是base环境的python3,避免冲突

然后编辑 ~/.vimrc (或它间接加载的配置文件),启用 YouCompleteMe:

Plug 'ycm-core/YouCompleteMe', { 'do': './install.py'  }

参考

posted @ 2018-05-15 00:24  ChrisZZ  阅读(3571)  评论(0编辑  收藏  举报