WSL2安装带有Tabnine自动补全功能的Vim

Created: April 20, 2022 3:56 PM
Created By: Shane Liu
Last Edited By: Shane Liu
Last Edited Time: April 20, 2022 4:38 PM

tabnine_logo.png

前言

想在WSL2下写bug,但是Vim没补全,尝试了pydiction,不好用,连pytorch的语法都补全不了。(纯纯的five)

想起来Vscode上一直在用Tabnine,其使用机器学习来进行语法补全的功能深得我心,我就在想能不能在Vim上弄个Tabnine插件帮助我写bug。

先上结果:

TextCNN

官方文档写的不是很完善,折腾了半天,最后还是配置好了。整理了一下遇到的坑,记录在这里,希望能帮到有同样问题的人。

Tabnine安装

Code Faster with AI Code Completions

在Tabnine官网提供了Vim安装的方式,我选择了YouCompleteMe(下文统一称YCM),因为之前装过YCM,用起来很顺手,就是不太好装。

进入Github Tabnine fork的YouCompleteMe之后,因为是在WSL里面,所以就顺着Linux 64-bit的安装方式来。

安装Vundle

因为想要安装YCM,需要通过Vundle来管理,所以我们先安装Vundle。

clone Vundle的项目到 ~/.vim/bundle/Vundle.vim

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

把下面的配置输入到 ~/.vimrc 中:(其中只有 Plugin 'VundleVim/Vundle.vim' 是必要插件,别的如果安装报错都可以注释掉。gmarik我装不上已经注释掉了,另外需要把 Plugin 'tabnine/YouCompleteMe'也加入到插件列表,这里我已经加完了,直接粘贴到 .vimrc就能用。)

set nocompatible              " be iMproved, required
filetype off                  " required

" set the runtime path to include Vundle and initialize
set rtp+=~/.vim/bundle/Vundle.vim
call vundle#begin()
" alternatively, pass a path where Vundle should install plugins
"call vundle#begin('~/some/path/here')

" let Vundle manage Vundle, required
Plugin 'VundleVim/Vundle.vim'

" The following are examples of different formats supported.
" Keep Plugin commands between vundle#begin/end.
" plugin on GitHub repo
Plugin 'tpope/vim-fugitive'
" plugin from http://vim-scripts.org/vim/scripts.html
" Plugin 'L9'
" Git plugin not hosted on GitHub
Plugin 'git://git.wincent.com/command-t.git'
" git repos on your local machine (i.e. when working on your own plugin)
" Plugin 'file:///home/gmarik/path/to/plugin'
" The sparkup vim script is in a subdirectory of this repo called vim.
" Pass the path to set the runtimepath properly.
Plugin 'rstacruz/sparkup', {'rtp': 'vim/'}
" Install L9 and avoid a Naming conflict if you've already installed a
" different version somewhere else.
" Plugin 'ascenator/L9', {'name': 'newL9'}
" YouCompleteMe
Plugin 'tabnine/YouCompleteMe'
" All of your Plugins must be added before the following line
call vundle#end()            " required
filetype plugin indent on    " required
" To ignore plugin indent changes, instead use:
"filetype plugin on
"
" Brief help
" :PluginList       - lists configured plugins
" :PluginInstall    - installs plugins; append `!` to update or just :PluginUpdate
" :PluginSearch foo - searches for foo; append `!` to refresh local cache
" :PluginClean      - confirms removal of unused plugins; append `!` to auto-approve removal
"
" see :h vundle for more details or wiki for FAQ
" Put your non-Plugin stuff after this line

先不用 :PluginInstall,因为我们还没有编译YCM。

安装YCM

  • 安装依赖(以Tabline在Github下的Repo为准)
sudo apt install build-essential cmake vim-nox python3-dev
sudo apt install mono-complete golang nodejs default-jdk npm
  • 编译YCM
cd ~/.vim/bundle/YouCompleteMe
python3 install.py --all

这里可能会提示File .vim/*/YouCompleteMe/third_party/ycmd/build.py does not exist ,其实我们按照Terminal给出的提示输入 git submodule update --init --recursive 检查依赖环境,之后就可以顺利完成安装了。这里等待的时间可能会比较长,并且不开代理一般是下不下来的。

编译结束后我们进入到Vim中,在Visual模式下输入 :PluginInstall

Plugin

Vim会展开两列,左侧是我们要安装的插件,可以看到 tabnine/YouCompleteMe 也是位列其中的。Terminal左下方会显示 Done! 表示我们已经安装成功。

Done

之后开始快乐写bug吧~

Last modification:April 20, 2022
恰饭环节