GO语言的开发工具有很多种,有人喜爱 emacs +spacemacs、有人喜欢atom+go-plus,而我却独爱vim-go,今天就大家介绍下linux如何配置go的开发利器vim-go,实现自动格式化代码、代码补齐、语法高亮等方便使用的功能。
$ git clone https://github.com/fatih/vim-go.git ~/.vim/bundle/vim-go
<1>、安装rpm包
# yum install -y git curl
<2>、先安装Vundle插件管理器
$ git clone https://github.com/VundleVim/Vundle.vim.git ~/.vim/bundle/Vundle.vim
<3>.参考官方的https://github.com/VundleVim/Vundle.vim,配置.vimrc。这个默认配置里面,不需要的插件可以注释掉。
$ vi ~/.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'}
" Install Vim-go
Plugin 'fatih/vim-go'
" " 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
在~/.vimrc的vundle配置里面加入vim-go
Plugin ‘fatih/vim-go’
保存退出后,再次启动vim。
a、用命令:PluginInstall安装vim-go。
Vundle.vim会在左侧打开一个Vundle Installer Preview子窗口,窗口下方会提示:”Processing ‘fatih/vim-go’”,等待安装完成后,会显示Done!这时可以看到~/.vim/bundle下面有个vim-go文件夹。
[app@golang-dev bundle] cd /.vim/bundle/[app@golang−devbundle] ll
总用量 8
drwxrwxr-x 16 app app 4096 9月 12 09:49 vim-go
drwxrwxr-x 8 app app 4096 9月 12 09:05 Vundle.vim
截图如下:
b、命令行安装
$ vim +PluginInstall +qall
编辑hello.go,语法高亮有了, 保存时自动format(利用$GOBIN/gofmt)也有了,但其他高级功能,比如自动import缺失的 package、自动补齐仍然没有,我们还要继续安装一些二进制工具。
在vim下输入:GoInstallBinaries 会自动安装相关工具,安装后会在GOPATH目录下的bin文件夹里面。
该安装相关文件在~/.vim/bundle/vim-go/plugin/go.vim,我们可以看到安装了下面几个工具
let s:packages = [
\ "github.com/nsf/gocode",
\ "github.com/alecthomas/gometalinter",
\ "golang.org/x/tools/cmd/goimports",
\ "golang.org/x/tools/cmd/guru",
\ "golang.org/x/tools/cmd/gorename",
\ "github.com/golang/lint/golint",
\ "github.com/rogpeppe/godef",
\ "github.com/kisielk/errcheck",
\ "github.com/jstemmer/gotags",
\ "github.com/klauspost/asmfmt/cmd/asmfmt",
\ "github.com/fatih/motion",
\ "github.com/fatih/gomodifytags",
\ "github.com/zmb3/gogetdoc",
\ "github.com/josharian/impl",
\ "github.com/dominikh/go-tools/cmd/keyify",
\ ]
其中有的网址访问不了,如果安装失败,可以到http://www.golangtc.com/download/package搜索下载,下载后放到GOPATH的src目录,用go install安装!安装后,会在GOPATH目录下的pkg和bin目录生成相应的文件!
vim-go默认代码补全是 + 。
再次编辑hello.go:
- 新起一行输入fmt.,然后ctrl+x, ctrl+o,Vim 会弹出补齐提示下拉框,不过并非实时跟随的那种补齐,这个补齐是由gocode提供的。
– 输入一行代码:time.Sleep(time.Second),执行:GoImports,Vim会自动导入time包。
– 将光标移到Sleep函数上,执行:GoDef或命令模式下敲入gd,Vim会打开$GOROOT/src/time/sleep.go中 的Sleep函数的定义。执行:b 1返回到hellogolang.go。
– 执行:GoLint,运行golint在当前Go源文件上。
– 执行:GoDoc,打开当前光标对应符号的Go文档。
– 执行:GoVet,在当前目录下运行go vet在当前Go源文件上。
– 执行:GoRun,编译运行当前main package。
– 执行:GoBuild,编译当前包,这取决于你的源文件,GoBuild不产生结果文件。
– 执行:GoInstall,安装当前包。
– 执行:GoTest,测试你当前路径下地_test.go文件。
– 执行:GoCoverage,创建一个测试覆盖结果文件,并打开浏览器展示当前包的情况。
– 执行:GoErrCheck,检查当前包种可能的未捕获的errors。
– 执行:GoFiles,显示当前包对应的源文件列表。
– 执行:GoDeps,显示当前包的依赖包列表。
– 执行:GoImplements,显示当前类型实现的interface列表。
– 执行:GoRename [to],将当前光标下的符号替换为[to]。
实现功能:实时跟随的代码补齐 + Code Snippet support
安装YCM(Your Complete Me) —– bundle安装
在~/.vimrc中添加一行:
Plugin ‘Valloric/YouCompleteMe’
保存退出后,再打开~/.vimrc并执行 :PluginInstall。
编译安装
# yum install build-essential cmake python-dev
$ cd ~/.vim/bundle/
$ git clone https://github.com/Valloric/YouCompleteMe.git
$ cd YouCompleteMe/
$ git submodule update --init --recursive
$ cd ~/.vim/bundle/YouCompleteMe
$ ./install.py --gocode-completer // 对 golang的支持
Q1:YouCompleteMe unavailable : requires Vim 7.4.143
升级vim版本:
# yum install -y ncurses-devel
# git clone https://github.com/vim/vim.git
# cd vim
# ./configure --enable-multibyte --enable-pythoninterp=yes --enable-python3interp=yes
# make && make install
Q2: YouCompleteMe unavailable: requires Vim compiled with Python (2.6+ or 3.3+) support
configure 参数加上--enable-pythoninterp=yes --enable-python3interp=yes 参数
# ./configure --enable-multibyte --enable-pythoninterp=yes --enable-python3interp=yes
Q3:vim 退格键(backspace)不能用
编辑~/.vimrc加入下面2行:
$ vi ~/.vimrc
set nocompatible
set backspace=indent,eol,start
便捷 .vimrc,前面讲到了vim-go有许多命令,在:xx模式下执行多显不便,于是你可以定义一些Mappings,比如:
$ vi ~/.vimrc
" set mapleader
let mapleader = ","
" vim-go custom mappings
au FileType go nmap <Leader>s <Plug>(go-implements)
au FileType go nmap <Leader>i <Plug>(go-info)
au FileType go nmap <Leader>gd <Plug>(go-doc)
au FileType go nmap <Leader>gv <Plug>(go-doc-vertical)
au FileType go nmap <leader>r <Plug>(go-run)
au FileType go nmap <leader>b <Plug>(go-build)
au FileType go nmap <leader>t <Plug>(go-test)
au FileType go nmap <leader>c <Plug>(go-coverage)
au FileType go nmap <Leader>ds <Plug>(go-def-split)
au FileType go nmap <Leader>dv <Plug>(go-def-vertical)
au FileType go nmap <Leader>dt <Plug>(go-def-tab)
au FileType go nmap <Leader>e <Plug>(go-rename)
" vim-go settings
let g:go_fmt_command = "goimports"
参考链接:
http://tonybai.com/2014/11/07/golang-development-environment-for-vim/
https://studygolang.com/articles/3289
https://segmentfault.com/a/1190000002662054