CentOS 7.4 用VIM搭建GOIDE

首先需要安装go

省略…

升级VIM为VIM8

cat /etc/redhat-release 
CentOS Linux release 7.4.1708 (Core)

安装ycm过程中会提示:YouCompleteMe unavailable : YouCompleteMe unavailable: requires Vim 7.4.1578+

升级vim

# 移除旧版本
sudo yum remove vim -y
# 安装必要组件
sudo yum install ncurses-devel python-devel -y
yum -y install lua lua-devel
# 下载源码编译安装
git clone https://github.com/vim/vim.git
cd vim/src
# 根据自己实际情况设置编译参数
./configure --with-features=huge --enable-pythoninterp=yes --enable-cscope --enable-fontset --with-python-config-dir=/usr/lib64/python2.7/config --enable-luainterp
make
sudo make install

编译参数说明:
–with-features=huge:支持最大特性
–enable-rubyinterp:打开对ruby编写的插件的支持
–enable-pythoninterp:打开对python编写的插件的支持
–enable-python3interp:打开对python3编写的插件的支持
–enable-luainterp:打开对lua编写的插件的支持
–enable-perlinterp:打开对perl编写的插件的支持
–enable-multibyte:打开多字节支持,可以在Vim中输入中文
–enable-cscope:打开对cscope的支持
–with-python-config-dir=/usr/lib64/python2.7/config 指定python 路径
–with-python-config-dir=/usr/lib64/python3.5/config 指定python3路径

注意:必须带上Python编写插件支持,最好带上Python路径,否则使用时会报这个错误:YouCompleteMe unavailable: requires Vim compiled with Python (2.6+ or 3.3+) support

编译安装好Vim后,默然是安装在/usr/local/bin目录下,所以把该目录加入到PATH方便terminal全局使用:

sudo nano /etc/profile

在尾部添加:

export PATH=$PATH:/usr/local/bin/vim

然后使环境变量生效:

source /etc/profile

安装vim-pathogen

https://github.com/tpope/vim-pathogen#installation

安装YCM

Vundle是Vim的插件管理工具,官方文档:https://github.com/VundleVim/Vundle.vim

git clone https://github.com/VundleVim/Vundle.vim.git ~/.vim/bundle/Vundle.vim
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'}

Bundle 'altercation/vim-colors-solarized'
Plugin 'vim-scripts/OmniCppComplete'
Plugin 'Valloric/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

syntax enable
colorscheme monokai

let g:ycm_global_ycm_extra_conf='~/.vim/bundle/YouCompleteMe/third_party/ycmd/cpp/ycm/.ycm_extra_conf.py'

执行插件安装

vim +PluginInstall +qall
sudo yum install cmake -y
cd ~/.vim/bundle/YouCompleteMe  
./install.py --go-completer

安装GO-IDE

git clone https://github.com/farazdagi/vim-go-ide.git ~/.vim_go_runtime
sh ~/.vim_go_runtime/bin/install

vim -u ~/.vimrc.go测试

mkdir -p /usr/local/goextra
export GOPATH=/usr/local/goextra

启动vim,执行

:GoInstallBinaries

/usr/local/goextra加入环境变量

git clone https://github.com/fatih/vim-go.git ~/.vim/bundle/vim-go

执行,组合ycm和vimgo,因为ycm可以自动补全,vimgo需要ctrl+x,ctrl+o

 cp .vimrc .vim_go_runtime/custom_config.vim
 sh ~/.vim_go_runtime/bin/install 

其他探索

最后,加一些快捷键:vim ~/.vimrc.go
在最后添加一些常用快捷键映射

let mapleader = ","
map ,g :YcmCompleter GoTo <Esc>//这个无法跟踪vendor目录里的
map ,d :GoDef <Esc>//这个可以跟踪vendor目录的
map ,r :GoRun <Esc> //执行
map ,t :GoTest <Esc> //测试
map ,f :GoTestFunc <Esc> //执行光标所在测试函数
map <C-n> :cnext<CR> //显示下一个错误
map <C-m> :cprevious<CR> //显示上一个错误
nnoremap <leader>a :cclose<CR> //关闭错误飘窗
set autowrite //自动保存
autocmd FileType go nmap <Leader>c <Plug>(go-coverage) //生成覆盖率,用默认浏览器打开结果

这样就可以在定位到函数的时候,直接 ,gg 就跳转到定义,看完后ctrl+o退回来。
此处还有vimgo的一些其他命令,很强大,例如vafdafyafdifvifyif 自己测试下吧。

export GOPATH=/usr/local/go/extra/
gometalinter --install //安装gometalinter

vim ~/.vimrc.go
//添加
map ,l :GoMetaLinter <Esc> //快捷键,l即可执行GoMetaLinter
vim ~/.bashrc
//添加
alias gp='export GOPATH=`pwd`' //这样每次在所需要的工程目录,直接gp就可以设置gopath为当前目录

经过一段时间使用,发现每次保存都卡半天,实际上是在做语法检查,体验不太好。完全可以把这个动作映射到快捷键上,每次保存就只做一次go fmt就行了。

vim ~/.vimrc.go
let g:syntastic_go_checkers = [] #关闭语法检查
map ,k :GoErrCheck<Esc> #映射,k为语法检查。

更多文档参考:
http://dreamlikes.cn/archives/940
https://farazdagi.com/2015/vim-as-go-language-ide/
https://github.com/fatih/vim-go
https://github.com/Valloric/YouCompleteMe

阅读更多
想对作者说点什么?

博主推荐

换一批

没有更多推荐了,返回首页