Golang Vim开发环境搭建
文章目录
1 升级vim
vim-go要求vim版本号大于7.4.2009,目前CentOS的版本自带的都不够,需要对vim进行升级,直接升到vim 8.0版本。网上大多数升级方案都没用,下面有个即简单又有用的:
rpm -Uvh http://mirror.ghettoforge.org/distributions/gf/gf-release-latest.gf.el7.noarch.rpm
rpm --import http://mirror.ghettoforge.org/distributions/gf/RPM-GPG-KEY-gf.el7
yum -y remove vim-minimal vim-common vim-enhanced
yum -y --enablerepo=gf-plus install vim-enhanced
2 安装Vundle.vim
新建目录:
mkdir -p ~/.vim/bundle
下载Vundle.vim包:
git clone https://github.com/gmarik/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()
" let Vundle manage Vundle, required
Plugin 'gmarik/Vundle.vim'
" All of your Plugins must be added before the following line
call vundle#end() " required
filetype plugin indent on " required
3 安装Vim-go
编辑.vimrc,在vundle#begin和vundle#end间增加一行:
Plugin 'fatih/vim-go'
执行安装,在vim内输入:
:PluginInstall
输入完后窗口最后放会显示“Processing ‘fatih/vim-go’”,当信息变成"Done"后表示安装完成。
4 安装YouCompleteMe
这个是有于在编程的时候自动补齐的,非常有用,但也是最难装的,在安装的时候可以像Vim-go这样,通过在.vimrc上通过PluginInstall安装,但不建议。主要原因是YouCompleteMe这个包非常大,下载包的过程非常长,而且安装时会出现各种错误,通过Plugin安装时感觉像卡着了一样,无法把握进程。下面是自已下载包,自已编译。
4.1 下载
通过git命令将包clone到 ~/.vim/bundle/ 目录下。因为YouCompleteMe的submodule很多,不能直接下载zip包,但可以把已经git下来的目录打包放到这里。
cd ~/.vim/bundle/
git clone https://github.com/Valloric/YouCompleteMe.git
4.2 相关报错
4.2.1 Python headers are missing …
报错信息:
[root@VM_0_13_centos YouCompleteMe]# python install.py
Searching Python 2.7 libraries...
ERROR: Python headers are missing in /usr/include/python2.7.
解决方案:
因为没有安装python-devel导致的问题。
yum install -y python-devel
4.2.2 “CMAKE_CXX_COMPILER-NOTFOUND” was not found
报错信息:
CMake Error: your CXX compiler: "CMAKE_CXX_COMPILER-NOTFOUND" was not found. Please set CMAKE_CXX_COMPILER to a valid compiler path or name.
-- NOTE: You appear to be on CentOS. In order to use this application, you require a more modern compiler than the default compiler on this platform. Please install the devtoolset-6 or greater. For example, see this link: https://www.softwarecollections.org/en/scls/rhscl/devtoolset-6/
CMake Error at CMakeLists.txt:239 (message):
Your C++ compiler does NOT fully support C++11.
-- Configuring incomplete, errors occurred!
解决方案:
因为没有安装g++导致的问题。
yum install -y gcc-c++
4.3 安装成功
安装成功相关信息:
[100%] Built target ycm_core
-- The C compiler identification is GNU 4.8.5
-- Check for working C compiler: /usr/bin/cc
-- Check for working C compiler: /usr/bin/cc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Found PythonLibs: /usr/lib64/python2.7/config/libpython2.7.so (found version "2.7.5")
-- Configuring done
-- Generating done
-- Build files have been written to: /tmp/regex_build_U6bamf
Scanning dependencies of target _regex
[ 50%] Building C object CMakeFiles/_regex.dir/regex_2/_regex.c.o
[100%] Building C object CMakeFiles/_regex.dir/regex_2/_regex_unicode.c.o
Linking C shared library /root/.vim/bundle/YouCompleteMe/third_party/ycmd/third_party/cregex/regex_2/_regex.so
[100%] Built target _regex
验证:
编写一个go程序,在编程的时候通过Ctrl + p会有补全信息。
5 安装goimports
在https://gopm.io/网站上获取golang.org/x/tools包,并把包放到GOPATH下。然后到cmd/goimports编译出goimports二进制。
cd $GOPATH/src/golang.org/x/tools/cmd/goimports
go build
将goimports二进制移到$GOROOT/bin下:
cp goimports $GOROOT/bin/
6 安装gocode
编辑.vimrc,在vundle#begin和vundle#end间增加一行,然后执行PluginInstall:
Plugin 'nsf/gocode', {'rtp': 'vim/'}
待vim窗口下放状态显示为"Done"后,到 ~/.vim/bundle/gocode目录下编译出gocode可执行程序,并移动到GOROOT/bin:
cd ~/.vim/bundle/gocode
go build
mv gocode $GOROOT/bin
先Ctrl+x,后Ctrl+o 就是可以补全了。
效果:
YouCompleteMe是补全单词,是一个通用的补全工具;gocode是补全函数定义等,是golang语言专用的。