CentOS 8 搭建Vim golang环境
Vim搭建Golang IDE
1. Vundle安装
Vundle是vim的插件管理工具,用它可以自动管理安装后面用到的插件,从GitHub安装
cd
mkdir .vim/bundle
cd .vim/bundle
git clone https://github.com/Valloric/YouCompleteMe.git
安装完成后修改~/.vimrc启用
" .vimrc
" vundle
set rtp+=~/.vim/bundle/Vundle.vim
call vundle#begin()
Plugin 'VundleVim/Vundle.vim'
call vundle#end()
2. 安装vim插件
插件管理软件安装好了,接下来安装需要的插件:
- vim-go: https://github.com/fatih/vim-go
- YouCompleteMe: https://github.com/Valloric/YouCompleteMe
- Tagbar: https://github.com/majutsushi/tagbar
- NERDTree: https://github.com/scrooloose/nerdtree
使用vundle自动安装,修改.vimrc
" vundle
set rtp+=~/.vim/bundle/Vundle.vim
call vundle#begin()
Plugin 'VundleVim/Vundle.vim'
Plugin 'fatih/vim-go'
Plugin 'Valloric/YouCompleteMe'
Plugin 'Tagbar'
Plugin 'scrooloose/nerdtree'
call vundle#end()
保存,然后vim打开任意文件,输入:PluginInstall
自动开始安装
这个过程异常漫长,特别是YouCompleteMe,可以手动安装
cd ~/.vim/bundle
git clone https://github.com/fatih/vim-go
git clone https://github.com/majutsushi/tagbar
git clone https://github.com/scrooloose/nerdtree
git clone https://github.com/Valloric/YouCompleteMe
cd YouCompleteMe
git submodule update --init --recursive
YouCompleteMe递归安装很多依赖包,需很长时间,并且下载完成后还需要编译安装,见下一节
Tagbar需要ctags/gotags支持
yum install ctags -y
go get -u github.com/jstemmer/gotags
3. 安装完成后配置.vimrc
参考:
https://www.jianshu.com/p/2a0ccc86ee31
https://www.jianshu.com/p/8426cef1f4f5
https://blog.csdn.net/hsiffish/article/details/82313792
下面是部分个人使用的设置
" vim-go golang
let g:go_autodetect_gopath = 1
let g:go_list_type = "quickfix"
let g:go_version_warning = 1
let g:go_highlight_types = 1
let g:go_highlight_fields = 1
let g:go_highlight_functions = 1
let g:go_highlight_function_calls = 1
let g:go_highlight_operators = 1
let g:go_highlight_extra_types = 1
let g:go_highlight_methods = 1
let g:go_highlight_generate_tags = 1
let g:godef_split=2
" NERDTree
" 打开和关闭NERDTree快捷键
map <F10> :NERDTreeToggle<CR>
" 设置宽度
let NERDTreeWinSize=25
" Tagbar
nmap <F9> :TagbarToggle<CR>
let g:tagbar_width=25
let g:tagbar_type_go = {
\ 'ctagstype' : 'go',
\ 'kinds' : [
\ 'p:package',
\ 'i:imports:1',
\ 'c:constants',
\ 'v:variables',
\ 't:types',
\ 'n:interfaces',
\ 'w:fields',
\ 'e:embedded',
\ 'm:methods',
\ 'r:constructor',
\ 'f:functions'
\ ],
\ 'sro' : '.',
\ 'kind2scope' : {
\ 't' : 'ctype',
\ 'n' : 'ntype'
\ },
\ 'scope2kind' : {
\ 'ctype' : 't',
\ 'ntype' : 'n'
\ },
\ 'ctagsbin' : 'gotags',
\ 'ctagsargs' : '-sort -silent'
\ }
YouCompleteMe Golang安装支持
YouCompleteMe安装异常艰难,因此记录一些问题
1. 下载
mkdir -p ~/.vim/bundle
cd ~/.vim/bundle
git clone https://github.com/Valloric/YouCompleteMe
cd YouCompleteMe
git submodule update --init --recursive
2. 编译安装
需要预先安装好python, cmake
等工具
cd YouCompleteMe
python install.py --go-completer
安装命令很简单,问题很多
错误信息:
go: golang.org/x/net@v0.0.0-20190620200207-3b0461eec859: unrecognized import path "golang.org/x/net" (https fetch: Get https://golang.org/x/net?go-get=1: dial tcp 21
6.239.37.1:443: i/o timeout)
go: golang.org/x/xerrors@v0.0.0-20190717185122-a985d3407aa7: unrecognized import path "golang.org/x/xerrors" (https fetch: Get https://golang.org/x/xerrors?go-get=1:
dial tcp 216.239.37.1:443: i/o timeout)
go: golang.org/x/sync@v0.0.0-20190423024810-112230192c58: unrecognized import path "golang.org/x/sync" (https fetch: Get https://golang.org/x/sync?go-get=1: dial tcp
216.239.37.1:443: i/o timeout)
go: error loading module requirements
这是因为不能访问golang.org
导致下载失败,解决办法是手动下载,参考博文go get golang.org/x/tools 失败解决
下载好所有的依赖包,并拷贝到下面目录
.vim/bundle/YouCompleteMe/third_party/ycmd/third_party/go/src/golang.org/x/
并且把进行安装的shell窗口GOPATH设置为ycmd的go依赖包目录,后面还有很多go语言依赖包都需要下载到这个目录
cd .vim/bundle/YouCompleteMe/third_party/ycmd/third_party/go
export GOPATH='pwd'
只在当前窗口生效,重开窗口重新设置,安装完成后关闭窗口自动失效,不影响系统设置
下载并设置完后,重新进入YouCompleteMe目录执行安装,错误变成下面
错误信息:
Traceback (most recent call last):
File "/home/xxx/.vim/bundle/YouCompleteMe/third_party/ycmd/build.py", line 1133, in <module>
Main()
File "/home/xxx/.vim/bundle/YouCompleteMe/third_party/ycmd/build.py", line 1119, in Main
EnableGoCompleter( args )
File "/home/xxx/.vim/bundle/YouCompleteMe/third_party/ycmd/build.py", line 828, in EnableGoCompleter
go_dir, 'src', 'golang.org', 'x', 'tools', 'cmd', 'gopls' ) )
FileNotFoundError: [Errno 2] No such file or directory: '/home/xxx/.vim/bundle/YouCompleteMe/third_party/ycmd/third_party/go/src/golang.org/x/tools/cmd/gopls'
这个错误是说在.vim/bundle/YouCompleteMe/third_party/ycmd/third_party/go/src/golang.org/x/tools/cmd/
目录下找不到gopls
,前面已经下载了golang.org/x/tools/
目录所有文件,进入golang.org/x/tools/cmd
目录,确实没有gopls
,但是发现gopls
在golang.org/x/tools
目录,说明调整了目录结构,而编译代码比较老,还没有更新
找到出错的代码,.vim/bundle/YouCompleteMe/third_party/ycmd/build.py
文件的828行,去掉cmd
参数即可,修改如下
// 826 go_dir = p.join( DIR_OF_THIS_SCRIPT, 'third_party', 'go' )
// 827 os.chdir( p.join(
// 828 go_dir, 'src', 'golang.org', 'x', 'tools', 'cmd', 'gopls' ) )
// 修改后
go_dir = p.join( DIR_OF_THIS_SCRIPT, 'third_party', 'go' )
os.chdir( p.join(
go_dir, 'src', 'golang.org', 'x', 'tools', 'gopls' ) )
错误信息:
internal/hooks/diff.go:8:2: cannot find package "github.com/sergi/go-diff/diffmatchpatch" in any of:
/usr/lib/golang/src/github.com/sergi/go-diff/diffmatchpatch (from $GOROOT)
/home/xxx/.vim/bundle/YouCompleteMe/third_party/ycmd/third_party/go/src/github.com/sergi/go-diff/diffmatchpatch (from $GOPATH)
internal/hooks/analysis.go:9:2: cannot find package "honnef.co/go/tools/simple" in any of:
/usr/lib/golang/src/honnef.co/go/tools/simple (from $GOROOT)
/home/xxx/.vim/bundle/YouCompleteMe/third_party/ycmd/third_party/go/src/honnef.co/go/tools/simple (from $GOPATH)
这依然是没有找到相应的依赖包,并且自动下载失败,因为访问github太慢了,解决办法同上,手动下载
可以使用 go get命令,下载到系统 $GOPATH下,并拷贝到.vim/bundle/YouCompleteMe/third_party/ycmd/third_party/go/src
目录,也可以直接git clone
下载
最终下载完成后,go/src
目录如下
golang.org/x/tools
golang.org/x/net
golang.org/x/sync
golang.org/x/xerrors
golang.org/x/mod
github.com/haya14busa/goplay
github.com/karrick/godirwalk
github.com/sqs/goreturns
github.com/fatih/gomodifytags
github.com/godoctor/godoctor
github.com/josharian/impl
github.com/davidrjenni/reftools
github.com/mdempsky/gocode
github.com/ramya-rao-a/go-outline
github.com/skratchdot/open-golang
github.com/stamblerre/gocode
github.com/jstemmer/gotags
github.com/acroca/go-symbols
github.com/go-delve/delve
github.com/pkg/errors
github.com/sergi/go-diff
github.com/rogpeppe/godef
github.com/uudashr/gopkgs
github.com/BurntSushi/toml
honnef.co/go/tools
mvdan.cc/xurls
漫长的下载完成后,再进入YouCompleteMe
执行python install.py --go-completer
安装
最终编译成功安装完成,打开一个go语言文件测试一下,效果如下
3. 附YouCompleteMe C/C++/Python支持
python install.py --clang-completer
错误信息:
下载libclang-9.0.0-x86_64-unknown-linux-gnu.tar.bz2
失败
手动下载并复制到YouCompleteMe/third_party/ycmd/clang_archives
目录即可
下载地址:https://dl.bintray.com/ycm-core/libclang/libclang-9.0.0-x86_64-unknown-linux-gnu.tar.bz2
更多详情请参考YouCompleteMe文档
来源:CSDN
作者:yf夜风
链接:https://blog.csdn.net/Wind4study/article/details/104565482