左边是代码目录树,中间是代码编辑区域,右边是代码类结构等信息,底部是状态栏。

2.依赖环境

2.1 安装go环境

GOROOTGOPATH

vim-go插件需要vim使用8.0以上的版本,而YouCompleteMe需要python2.7.1+或3.5.1+。如果你系统的vim和python版本满足条件,可以忽略下面两个步骤。

2.2. 安装vim8.0+

推荐使用源码方式安装,先从github上下载vim源代码:

git clone https://github.com/vim/vim.git
src/INSTALL
>vim --version | grep python
+cmdline_hist      +langmap          -python            +visual
+cmdline_info      +libcall          -python3           +visualextra
-./configure--enable-pythoninterp=yes--enable-python3interp=yes

2.3. 安装python2.7.1+

# Python2.7.1:
wget http://python.org/ftp/python/2.7.14/Python-2.7.14.tar.xz
tar xf Python-2.7.14.tar.xz
cd Python-2.7.14
./configure --prefix=/usr/local/python27 --enable-unicode=ucs4 --enable-shared 
make && make install

需要注意的是:

  • make install需要管理员权限进行安装
  • 编译python的时候加上--enable-unicode和--enable-shared,主要是vim打开python支持后需要python动态库支持,并且unicode需要支持ucs4。

3.环境搭建

3.1.vim基本础配置

3.2.vim插件安装

我使用Vundle管理vim插件。插件安装比较简单,先打开~/.vimrc,进行需要的插件配置,在配置前建议阅读Vundle的README.md。安装比较简单,如下:

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

2.配置插件

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 'VundleVim/Vundle.vim'

Plugin 'fatih/vim-go'

Plugin 'majutsushi/tagbar'

Plugin 'scrooloose/nerdtree'

Plugin 'Valloric/YouCompleteMe'

" All of your Plugins must be added before the following line
call vundle#end()            " required
filetype plugin indent on    " required

配置完后,在vim命令模式下执行:

:PluginInstall

插件会自动下载安装,看见上面显示 Finishing ... Done 的内容,插件安装成功。

3.3安装vim-go

安装完vim-go插件后,vim-go本身依赖一些包,在vim命令模块下执行:

:GoInstallBinaries

在安装过程中,不出意外,你会遇到安装失败的情况,请在下面遇到的问题中找解决方案。

3.4.编译YouCompleteMe

安装完YouCompleteMe后,还需要单独编译才能运行YouaCompleteMe:

cd ~/.vim/plugged/YouCompleteMe
sh install.py --go-completer 

YouCompleteMe支持多语言,其它语言支持请参考YouCompleteMe源代码目录下的README.md文件。

3.5.插件的配置

我对NERDTree、YouCompleteMe以及tagbar的配置比较简单,需要更多配置请参考各个插件的README.md

3.5.1 vim-go插件

let g:go_fmt_command = "goimports" " 格式化将默认的 gofmt 替换
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

3.5.2 NERDTree插件

" 打开和关闭NERDTree快捷键
map <F10> :NERDTreeToggle<CR>
" 设置宽度
" let NERDTreeWinSize=25

3.5.3 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'
 }

4.遇到的问题

4.1.vim-go执行:GoInstallBinaries失败

表现为:

vim-go: guru not found. Installing golang.org/x/tools/cmd/guru to folder /home/xxx/repos/gopath/bin/
vim-go: Error downloading golang.org/x/tools/cmd/guru: Fetching https://golang.org/x/tools/cmd/guru?go-get=1
vim-go: https fetch failed: Get https://golang.org/x/tools/cmd/guru?go-get=1: dial tcp 216.239.37.1:443: i/o timeout
vim-go: package golang.org/x/tools/cmd/guru: unrecognized import path "golang.org/x/tools/cmd/guru" (https fetch: Get https://gola
ng.org/x/tools/cmd/guru?go-get=1: dial tcp 216.239.37.1:443: i/o timeout)
vim-go: Error installing golang.org/x/tools/cmd/guru: can't load package: package golang.org/x/tools/cmd/guru: cannot find package "golang.org/x/tools/cmd/guru"
~/.vim/bundle/vim-go/plugin/go.vimhttps://golang.org
# 先在$GOPATH目录下本地建立golang.orgx
mkdir -p %GOPATH%/src/golang.org/x

# 将tools代码库clone到本地
git clone https://github.com/golang/tools.git %GOPATH%/src/golang.org/x/tools

# 安装guru
go install golang.org/x/tools/cmd/guru

# 安装成功后就可以在$GOPATH/bin 目录下看到guru的二进制文件了

安装其它依赖包方法同上,不再累赘。

4.2.YouCompleteMe安装后不可用

安装完YouaCompleteMe插件后,打开vim提示:

YouCompleteMe unavailable: requires Vim compiled with Python (2.7.1+ or 3.5.1+) support
--enable-pythoninterp=yes--enable-python3interp=yes

4.3.vim加载libpython2.7.so.1.0动态库失败

表现为:

/usr/local/bin/vim: error while loading shared libraries: libpython2.7.so.1.0: cannot open shared object file: No such file or directory

当vim开启python支持后,需要依赖python共享库,使用ldd命令可以查看vim依赖的动态库:

> ldd /usr/local/bin/vim
libpython2.7.so.1.0--enable-shared/usr/local/python27/usr/local/python27/liblibpython2.7.so.1.0/usr/lib64//etc/ld.so.conf

4.4.undefined symbol pyunicodeucs4_asencodedstring

原因是源码编译python的时候没有开启asencodedstring支持,在编译python的时候加上以下参数就行:

./configure --prefix=/usr/local/python27 --enable-shared 

5.更多参考

1.https://learnku.com/articles/24924
2.https://github.com/fatih/vim-go
3.https://github.com/VundleVim/Vundle.vim/blob/master/CONTRIBUTING.md
4.https://github.com/majutsushi/tagbar
5.https://github.com/ycm-core/YouCompleteMe
6.https://github.com/vim/vim
7.https://ephrain.net/python-出現-undefined-symbol-pyunicodeucs4_asencodedstring-錯誤/
8.https://github.com/scrooloose/nerdtree
9.https://danieleriksson.net/2017/02/08/how-to-install-latest-python-on-centos/