Published on

vim安装go自动补全

Authors

插件

选用YouCompeleteMevim-go已经两年没有更新了。

安装Vundle

在安装YouCompeleteMe需要先安装Vundle,一个vim插件包管理工具。

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

把下面的配置写入~/.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'}

" 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

上面的一些示例可以删除,required的不能删除。

安装YCM

在~/.vimrc里加入Plugin 'ycm-core/YouCompleteMe'这行,在vim界面下输入:PluginInstall等待显示done完成。

安装 cmake, vim-nox python3-dev

apt install build-essential cmake vim-nox python3-dev

安装golang,如果已安装可跳过

apt install go

编译YCM

cd ~/.vim/bundle/YouCompeleteMe
python3 install.py --go-completer

过程可能需要10分钟。。视机器性能而定

我2C2G的阿里云大概10分钟不到搞定,出现下面的就ok了。

Generating ycmd build configuration...OK
Compiling ycmd target: ycm_core...OK
Building regex module...OK
Building watchdog module...OK
Building gopls for go completion...OK

打开go文件就有智能提示的效果了,不过好像很卡,可能服务器配置不太行。后面使用感觉还挺流畅,可能编译YCM占用太多资源没有释放。

跳转

nnoremap <leader>go :YcmCompleter GoTo<CR> "跳转
nnoremap <leader>gd :YcmCompleter GoToDefinitionElseDeclaration<CR> "跳转到定义或声明
nnoremap <leader>gt :YcmCompleter GetType<CR> "get类型
nmap gi :YcmCompleter GoToInclude<CR> "跳转到include、声明或定义
nmap gm :YcmCompleter GoToImprecise<CR> "跳转到实现
nmap gr :YcmCompleter GoToReferences<CR> "跳转到引用
nmap fi :YcmCompleter FixIt<CR> "根据Ycm的建议修复错误
nmap <F4> :YcmDiags<CR> "F4进行诊断

go好像只有gr,跳转引用有效果。按fi会给出修补建议。

Ondark主题

" OneDark
Plugin 'joshdick/onedark.vim'

" 在最下方添加以下配置
" 语法主题覆盖
syntax enable
syntax on

" 使能OneDark主题
set background=dark
colorscheme onedark