加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

支持GoLang,类似 Source Insight的vim编辑器搭建

发布时间:2020-12-16 19:11:30 所属栏目:大数据 来源:网络整理
导读:,曾经配置过一个类似source insight的vim编辑器,(http://www.jb51.cc/article/p-abnyxlim-bpt.html)默认对C,C++语言是支持的,只是不支持golang,下面介绍下如果打造一个vim-go的编辑器,类似source insight,自动补齐等等,我使用的是ubuntu16.04 LTS 32b

,曾经配置过一个类似source insight的vim编辑器,(http://www.52php.cn/article/p-abnyxlim-bpt.html)默认对C,C++语言是支持的,只是不支持golang,下面介绍下如果打造一个vim-go的编辑器,类似source insight,自动补齐等等,我使用的是ubuntu16.04 LTS 32bits。

下载安装GoLang

写博时,官方稳定版本是1.8,下载的是linux 32bits的。
https://golang.org/dl/
下载后直接解压到/usr/local/go下面即可,解压到其他path也可以,没有特别指定,只是习惯而已,比如解压到$HOME/go。

配置环境变量

可以将下面内容复制到/etc/profile,也可以复制到~/.profile

# go path
 export GOROOT=/usr/local/go
 export GOARCH=386
 export GOOS=linux
 export GOPATH=/home/jack/go
 export GOBIN=$GOPATH/bin
 export PATH=$PATH:$GOROOT/bin:$GOBIN

下面安装的一些go工具,如果配置了GOBIN这个变量,都会安装到这个目录下,如果没有配置这个变量,默认会安装在当前目录下的go目录下。

运行一下go命令,如果显示go命令的Usage,说明已经安装配置成功,或者go version看到版本信息,也说明安装成功。

hello GoLang

写一段程序测试下:摘抄于官网

// You can edit this code!
// Click here and start typing.
package main

import "fmt"

func main() {
    fmt.Println("Hello,世界")
}

$go run test.go
Hello,世界

准备配置vim-go

由于配置vim-go,需要安装一些go .tools binaries,这些工具默认会安装在$GOBIN下面。安装这些工具需要一些辅助工具,比如分布式版本控制系统Mercurial。
在 Ubuntu 下快速的最新版的安装Mercurial,其实只要几条命令就可以搞定了.

$sudo add-apt-repository ppa:tortoisehg-ppa/releases
$sudo add-apt-repository ppa:mercurial-ppa/releases
$sudo apt-get update
$sudo apt-get install mercurial python-nautilus tortoisehg

安装git, vim:

$sudo app-get install git
$sudo app-get install vim

安装ctags-exuberant,这个工具,后面很多插件会用到,tagbar,nerdtree,gotags等。

$sudo app-get install exuberant-ctags
$ctags-exuberant --version
Exuberant Ctags 5.9~svn20110310,Copyright (C) 1996-2009 Darren Hiebert
$ ctags-exuberant --list-languages //查看支持那些语言
......
Flex
Fortran
Go
HTML
Java
......

另外,配置YCM时,由于YCM用到了C++实现性能上的优化,所以需要安装C++的编译环境:

$sudo apt-get install build-essential cmake python-dev

插件管理工具vundle

vim-go官方提到两种插件管理工具,除vundle还有pathogen,只是选择vundle的比较多,我这里也以vundle为例介绍。

安装Vundle.vim

$mkdir ~/.vim/bundle
$git clone https://github.com/gmarik/Vundle.vim.git ~/.vim/bundle/Vundle.vim

后面安装的plugin都安装在~/.vim/bundle下面。

配置~/.vimrc文件以支持plugin的安装

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

保存.vimrc退出,在bash下输入vim命令回车,在命令行模式下输入 :PluginInstall回车,这时在窗口底部会看到Processing ‘gmarik/Vundle.vim’,表示正在从网络下载这个插件,等底部显示Done!即表示下载成功,这时退出vim,到~/.vim/bundle下可以看到多了一个vundle的path。

安装vim-go插件

编辑~/.vimrc,在vundle#begin和vundle#end间增加一行:

Plugin 'fatih/vim-go'

在Vim内执行 :PluginInstall,窗口下方会提示:Processing ‘fatih/vim-go’,待下载安装完毕后,提示信息变 成“Done!”

安装go.tools Binaries

vim-go安装说明中提到所有必要的binary需要先安装好,比如gocode、godef、goimports等.
通过:GoInstallBinaries,这些vim-go依赖的二进制工具将会自动被下载,并被安装到 GOBIN GOPATH/bin下。(这个工具需要依赖git或hg,需要提前安装到你的OS中。)

打vim,在命令行模式下输入:GoInstallBinaries
它的执行是交互式的,你需要回车确认,即开始下载安装。

:GoInstallBinaries
vim-go: gocode not found. Installing github.com/nsf/gocode to folder /home/jack/go/bin/
vim-go: gometalinter not found. Installing github.com/alecthomas/gometalinter to folder /home/jack
/go/bin/
vim-go: goimports not found. Installing golang.org/x/tools/cmd/goimports to folder /home/jack/go/b
in/
vim-go: guru not found. Installing golang.org/x/tools/cmd/guru to folder /home/jack/go/bin/
vim-go: gorename not found. Installing golang.org/x/tools/cmd/gorename to folder /home/jack/go/bin
/
vim-go: golint not found. Installing github.com/golang/lint/golint to folder /home/jack/go/bin/
vim-go: godef not found. Installing github.com/rogpeppe/godef to folder /home/jack/go/bin/
vim-go: errcheck not found. Installing github.com/kisielk/errcheck to folder /home/jack/go/bin/
vim-go: gotags not found. Installing github.com/jstemmer/gotags to folder /home/jack/go/bin/
vim-go: asmfmt not found. Installing github.com/klauspost/asmfmt/cmd/asmfmt to folder /home/jack/g
o/bin/
vim-go: motion not found. Installing github.com/fatih/motion to folder /home/jack/go/bin/
vim-go: gomodifytags not found. Installing github.com/fatih/gomodifytags to folder /home/jack/go/b
in/
vim-go: gogetdoc not found. Installing github.com/zmb3/gogetdoc to folder /home/jack/go/bin/
vim-go: impl not found. Installing github.com/josharian/impl to folder /home/jack/go/bin/
vim-go: keyify not found. Installing github.com/dominikh/go-tools/cmd/keyify to folder /home/jack/
go/bin/

注意下,这里会访问到google,由于我们的网络问题,需要翻下才行。

安装成功后GOBIN下面:

jack@jxes-VirtualBox:~$ ls $GOBIN -l
total 64412
-rwxrwxr-x 1 jack jack 2347772 719 17:29 asmfmt
-rwxrwxr-x 1 jack jack 4228667 719 17:28 errcheck
-rwxrwxr-x 1 jack jack 8656435 719 17:18 gocode
-rwxrwxr-x 1 jack jack 4556792 719 17:28 godef
-rwxrwxr-x 1 jack jack 4902326 719 17:29 gogetdoc
-rwxrwxr-x 1 jack jack 4024027 719 17:25 goimports
-rwxrwxr-x 1 jack jack 4322181 719 17:26 golint
-rwxrwxr-x 1 jack jack 5016936 719 17:25 gometalinter
-rwxrwxr-x 1 jack jack 3174321 719 17:29 gomodifytags
-rwxrwxr-x 1 jack jack 4551554 719 17:26 gorename
-rwxrwxr-x 1 jack jack 2276387 719 17:29 gotags
-rwxrwxr-x 1 jack jack 6897486 719 17:26 guru
-rwxrwxr-x 1 jack jack 3864326 719 17:29 impl
-rwxrwxr-x 1 jack jack 4486579 719 17:30 keyify
-rwxrwxr-x 1 jack jack 2623469 719 17:29 motion

这里的gotags在后tagbar里会用到。

安装 UltiSnips

Vim-go默认是用ultisnips引擎插件,但这个插件需要单独安装。

同样,我们利用vundle来安装它,在~/.vimrc中添加一行:

Plugin 'SirVer/ultisnips'

这个插件功能是,比如输入func后敲击tab键,会自动带出如下这样的:

func name(params) type{ }

安装YCM(Your Complete Me)

在~/.vimrc中添加一行:

Plugin 'Valloric/YouCompleteMe'

保存退出后,再打开~/.vimrc并执行PluginInstall

下载完成后:

cd ~/.vim/bundle/YouCompleteMe
./install.sh

构建(编译C++很慢,需要耐心的等一会)ok后,可以打开test.go看下效果,会有自动补齐功能,很cool!

需要注意:YCM与ultisnips会有快捷键上的冲突。
ultisnips默认是用Tab展开snippet的,而YCM中的Tab用来选择补齐项,我们可以通过设置来避免这些:

"================================ " YCM settings
" =============================== let g:ycm_key_list_select_completion = ['',''] let g:ycm_key_list_previous_completion = [''] let g:ycm_key_invoke_completion = '<C-Space>' "================================
" UltiSnips settings "================================
let g:UltiSnipsExpandTrigger = "<tab>"
let g:ULtiSnipsJumpForwardTrigger = "<c-b>"
let g:UltiSnipsJumpBackwardTrigger = "<c-z>"

这样让YCM通过回车和向下的箭头来做list item正向选择,通过向上箭头做反向选择。通过ctrl+space来原地触发补齐提示。
而ultisnips则是用tab做snippet展开,ctrl+b正向切换占位符,ctrl+z反向切换占位符。

安装molokai theme

Molokai theme是TextMate的theme的vim port,颜色不错,配置比较简单:

mkdir ~/.vim/colors
下载或复制https://github.com /fatih/molokai/blob/master/colors/molokai.vim到~/.vim /colors目录下
在.vimrc添加一行:colorscheme molokai

下面来张cool图展示下:

安装NERDTree插件

在.vimrc下面加上:
Plugin ‘scrooloose/nerdtree’
其他步骤与前面安装插件类似,略过。
下面主要看下.vimrc下面的配置:

"=============================== " NERDTree settings
" ============================== " F2 to open NERDTree
map <F7> :NERDTreeToggle<CR>

" modify the icon of the tree "let g:NERDTreeDirArrowExpandable = '+'
"let g:NERDTreeDirArrowCollapsible = '-' " show postion of the window
let g:NERDTreeWinPos = 'left'

" the size of window let g:NERDTreeSize = 30 " show line num in window
"let g:NERDTreeShowLineNumber = 1 " if show hidden file
"let g:NERDTreeHidden = 0 " open vim,if no file,open NERDTree automatically
" autocmd vimenter * if !argc()|NERDTree|endif " automatically close when the NERDTree is the only one window
" autocmd bufenter * if (winnr("$")==1 && exists("b:NERDTree") && " b:NERDTree.isTabTree())|q|endif

" open NERDTree automatically when open vim " autocmd vimenter * NERDTree</cr></f2>

安装Tagbar插件

在.vimrc下面加上:
Plugin ‘majutsushi/Tagbar’
其他步骤与前面安装插件类似,略过。
tagbar支持多种语言,可以参考:
https://github.com/majutsushi/tagbar/wiki#google-go
下面主要看下支持golang的配置,.vimrc下面的配置:

"=============================== " Tagbar settings
" ============================== " Set width of the Tagbar window
let g:tagbar_width = 30
" map shutcut key for Tagbar,press F8 to open automatically nmap <F8> :TagbarToggle<CR> " tagbar usr ctags plugin
" let g:tagbar_ctags_bin = 'ctags' " show on left,default is right
" let g:tagbar_left = 1 " set tagbar window width,default size is 40
" let g:tagbar_width = 30 " consur stays on tagbar window,default is on vim window
let g:tagbar_autofocus = 1

" set label not sorted,default is sorted let g:tagbar_sort = 0 " support golang
let g:tagbar_type_go = {
     'ctagstype': 'go', 'kinds' : [
        'p:package','f:function','v:variables','t:type','c:const'
    ]
}

注意:g:tagbar_type_go 这里的设置在官方网站上提供了两种方法,由于我安装了ctags,所以采用上面这种设置。

Google Go

Proper Go support is provided by the gotags project,complete with how to configure Tagbar to make use of it.

Alternatively,if you use the Go patch for exuberant-ctags or the Debian/Ubuntu package (which has it integrated),the following configuration provides simple support without scopes:

let g:tagbar_type_go = {
     'ctagstype': 'go','c:const'
    ]
}

另外一种是基于gotags,其官方配置:https://github.com/jstemmer/gotags
官方图片链接:

安装SrcExpl插件

在.vimrc下面加上:
Plugin ‘wesleyche/SrcExpl’
其他步骤与前面安装插件类似,略过。
官方网址:https://github.com/wesleyche/SrcExpl

"=============================== " SrcExpl settings
" ============================== " // The switch of the Source Explorer 
nmap <F9> :SrcExplToggle<CR>

" // Set the height of Source Explorer window let g:SrcExpl_winHeight = 8 " // Set 100 ms for refreshing the Source Explorer 
let g:SrcExpl_refreshTime = 100

" // Set "Enter" key to jump into the exact definition context let g:SrcExpl_jumpKey = "<ENTER>" " // Set "Space" key for back from the definition context 
let g:SrcExpl_gobackKey = "<SPACE>"

" // In order to avoid conflicts,the Source Explorer should know what plugins " // except itself are using buffers. And you need add their buffer names into
" // below listaccording to the command ":buffers!" let g:SrcExpl_pluginList = [  "__Tag_List__", "_NERD_tree_"  ] " // Enable/Disable the local definition searching,and note that this is not 
" // guaranteed to work,the Source Explorer doesn't check the syntax for now. " // It only searches for a match with the keyword according to command 'gd' 
let g:SrcExpl_searchLocalDef = 1

" // Do not let the Source Explorer update the tags file when opening let g:SrcExpl_isUpdateTags = 0 " // Use 'Exuberant Ctags' with '--sort=foldcase -R .' or '-L cscope.files' to 
" // create/update the tags file let g:SrcExpl_updateTagsCmd = "ctags --sort=foldcase -R ." " // Set "<F12>" key for updating the tags file artificially 
let g:SrcExpl_updateTagsKey = "<F12>"

" // Set "<F3>" key for displaying the previous definition in the jump list let g:SrcExpl_prevDefKey = "<F3>" " // Set "<F4>" key for displaying the next definition in the jump list 
let g:SrcExpl_nextDefKey = "<F4>"

Nerdtree,tagbar,SrcExpl三窗口之间切换可以按ctrl+w+w

Show下cool图

完整.vimrc可以去我的资源下载:
http://download.csdn.net/detail/js_gary/9905174

再发一个网络上大牛的配置,只是是针对PHP,CSS,XML,JAVASCRIPT,MARKDOWN,PHYTHON之类的开发, 对C/C++和GO的支持都不是太好,另外,对GVIM有一点冲突,片段显示窗口等小问题,不过可以使用。
https://github.com/humiaozuzu/dot-vimrc
如果喜欢这位大牛风格的配置,可以对其稍作修改就可以支持GO的开发,具体如下:
0. 安装go.tools Binaries,具体同上
1. 参考上面的配置,安装YCM插件,并在.vimrc中添加YCM相关配置。
2. 在tagbar里添加对go的支持,注意也选用ctags这种方式的配置。
3. 把下面这段关于vim-go的配置也添加上(具体配置内容在上面的配置下载包里也有)

281 " for vim-go
282 au FileType go nmap <leader> s <plug> (go-implements)
283 au FileType go nmap <leader> i <plug>(go-info)
284 au FileType go nmap <Leader> gd <plug> (go-doc)
285 au FileType go nmap <Leader> gv <plug> (go-doc-vertical)
286 au FileType go nmap <Leader> r <plug> (go-run)
287 au FileType go nmap <Leader> b <plug> (go-build)
288 au FileType go nmap <leader> t <plug> (go-test)
289 au FileType go nmap <Leader> c <plug> (go-coverage)
290 au FileType go nmap <Leader> ds <plug> (go-def-split)
291 au FileType go nmap <Leader> dv <plug> (go-def-vertical)
292 au FileType go nmap <leader> dt <plug> (go-def-tap)
293 au FileType go nmap <Leader> e <plug> (go-rename)
294  
295 let g:go_fmt_command = "goimports"
297 " for YCM
298 let g:ycm_key_list_select_completion = ['','']
299 let g:ycm_key_list_previous_completion = ['']
300 let g:ycm_key_invoke_completion = '<C-Space>'

4.插件包里有一个插件有点旧,可以换下。

29 "Bundle 'edsono/vim-matchit'
 30 Bundle 'tmhedberg/matchit'

最后说明一下,此配置里的几个常用快捷键: F5 -> Toggle Nerd-Tree file viewer F6 -> Toggle tagbar Ctrl + p -> Toggle ctrlp F3 -> Toggle Gundo viewer F4 -> Toggle Indent Guides F12 -> Toggle Mouse

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读