Linux系统运维之linux下golang的vim环境配置
白羽 2018-07-11 来源 :网络 阅读 1361 评论 0

摘要:本文将带你了解Linux系统运维之linux下golang的vim环境配置,希望本文对大家学Linux有所帮助。


        准备条件,是已经安装好golang环境,还需要把golang的环境变量配置好,这里配置如下

[root@localhost bundle]# vim /etc/profile

在这个文件最后加上,GOPATH、GOBIN、GOROOT、PATH的环境变量

[plain] view plain copy

1. export GOPATH="/root/go"  

2. export GOBIN="$GOPATH/bin"  

3. export GOROOT="/usr/local/go"  

4. export PATH="$PATH:/usr/local/go/bin"  

5. export PATH="$PATH:/root/go/bin"  

[root@localhost bundle]# source /etc/profile

 

1. 安装git

[root@localhost ~]# yum install -y git

 

2. 安装Vundle.vim

mkdir ~/.vim/bundle

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

 

3. 安装vim-go

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

 

4. 修改~/.vimrc,如果没有就创建.vimrc文件

[root@localhost bundle]# vim ~/.vimrc

[plain] view plain copy

1. set nocompatible              " be iMproved, required  

2. filetype off                  " required  

3.   

4. " set the runtime path to include Vundle and initialize  

5. set rtp+=~/.vim/bundle/Vundle.vim  

6. call vundle#begin()  

7.   

8. " let Vundle manage Vundle, required  

9. Plugin 'gmarik/Vundle.vim'  

10. Plugin 'fatih/vim-go'  

11.   

12. " All of your Plugins must be added before the following line  

13. call vundle#end()            " required  

14. filetype plugin indent on    " required  

15.    

16. let g:go_version_warning = 0  

如果觉得高亮的地方太少,可在~/.vimrc下多添加如下配置

let g:go_highlight_functions = 1
let g:go_highlight_methods = 1
let g:go_highlight_structs = 1
let g:go_highlight_operators = 1
let g:go_highlight_build_constraints = 1

 

5. 创建一个测试文件test.go,将发现可以高亮了

[root@localhost bundle]# vim ./test.go

 

6. 下载nerdtree和nerdtree-git-plugin

[root@localhost bundle]# git clone https://github.com/scrooloose/nerdtree.git ~/.vim/bundle/nerdtree

[root@localhost bundle]# git clone https://github.com/Xuyuanp/nerdtree-git-plugin.git ~/.vim/bundle/nerdtree-git-plugin

 

7. 修改~/.vimrc

[plain] view plain copy

1. set nocompatible              " be iMproved, required  

2. filetype off                  " required  

3.   

4. " set the runtime path to include Vundle and initialize  

5. set rtp+=~/.vim/bundle/Vundle.vim  

6.   

7. call vundle#begin()  

8.   

9. " let Vundle manage Vundle, required  

10. Plugin 'gmarik/Vundle.vim'  

11. Plugin 'fatih/vim-go'  

12. Plugin 'git://github.com/scrooloose/nerdtree.git'  

13. Plugin 'git://github.com/Xuyuanp/nerdtree-git-plugin.git'  

14.   

15. " All of your Plugins must be added before the following line  

16. call vundle#end()            " required  

17.   

18. filetype plugin indent on    " required  

19.    

20. let g:go_version_warning = 0  

21.   

22. " NERDTree config  

23. map  <F7> :NERDTreeToggle<CR>    "快捷键F7,可以收放目录树的视图  

24. autocmd bufenter * if (winnr("$") == 1 && exists("b:NERDTreeType") &&b:NERDTreeType == "primary") | q | endif  

25. autocmd vimenter * NERDTree  

 

8. nerdtree的使用,这里不讲解,大家可使用?命令来自己了解,如下

[root@localhost bundle]# vim ~/.vimrc

 

注意:使用nerdtree的命令,需要光标在目录树的窗口下才可以,如上图,此时直接敲?即可

 

9. 安装 neocomplete

这个智能的提示功能需要很多限制条件,想要安装成功请先阅读这个网址:

https://blog.csdn.net/pei2215015/article/details/79813522

根据官网https://github.com/shougo/neosnippet.vim的提示安装,修改~/.vimrc

[root@localhost bundle]# vim ~/.vimrc

 

在如上位置添加3个插件Plugin

在~/.vimrc文件的最后添加

let g:neocomplete#enable_at_startup = 1

:wq保存退出

打开vim,执行:PluginInstall

[root@localhost .vim]# vim

:PluginInstall

出现Done即安装成功

 

10. 安装ctags、gotags和tarbar

[root@localhost bundle]# yum install -y ctags

如上面第9个步骤一样,在~/.vimrc中添加插件

Plugin 'jstemmer/gotags'

Plugin 'majutsushi/tagbar'

保存之后,打开vim,执行:PluginInstall,如下

[root@localhost .vim]# vim

:PluginInstall

出现Done即安装成功

在~/.vimrc文件最后加入以下内容:

[plain] view plain copy

1. let g:tagbar_type_go = {  

2.     \ 'ctagstype' : 'go',  

3.     \ 'kinds'     : [  

4.         \ 'p:package',  

5.         \ 'i:imports:1',  

6.         \ 'c:constants',  

7.         \ 'v:variables',  

8.         \ 't:types',  

9.         \ 'n:interfaces',  

10.         \ 'w:fields',  

11.         \ 'e:embedded',  

12.         \ 'm:methods',  

13.         \ 'r:constructor',  

14.         \ 'f:functions'  

15.     \ ],  

16.     \ 'sro' : '.',  

17.     \ 'kind2scope' : {  

18.         \ 't' : 'ctype',  

19.         \ 'n' : 'ntype'  

20.     \ },  

21.     \ 'scope2kind' : {  

22.         \ 'ctype' : 't',  

23.         \ 'ntype' : 'n'  

24.     \ },  

25.     \ 'ctagsbin'  : 'gotags',  

26.     \ 'ctagsargs' : '-sort -silent'  

27. \ }  

 

11. 安装mark.vim

地址:https://www.vim.org/scripts/script.php?script_id=2666,我这里演示中下载的是

 

直接使用vim打开

[root@localhost bundle]# vim ./mark-3.0.0.vmb.gz 

在vim的命令模式下输入

:so %

即安装成功

在~/.vimrc中添加

[plain] view plain copy

1. nmap ml <Plug>MarkSet #高亮或反高亮一个单词    

2. nmap md <Plug>MarkClear  

3. nmap mn <Plug>MarkSearchAnyNext #跳转到任一下一个高亮单词    

4. nmap mp <Plug>MarkSearchAnyPrev  

5. nmap mf <Plug>MarkSearchCurrentNext #跳转到当前高亮的下一个单词    

6. nmap mb <Plug>MarkSearchCurrentPrev  

 

12. 安装goimports

[root@localhost src]# cd /root/go/src
[root@localhost src]# mkdir golang.org
[root@localhost golang.org]# mkdir x
[root@localhost golang.org]# cd ./x
[root@localhost x]# git clone https://github.com/golang/tools.git
[root@localhost x]# cd ./tools/cmd/goimports/

[root@localhost goimports]# go install

使用方法:在vim的命令模式下输入:GoImports

注意:在tools/cmd下还有很多命令工具,可以使用同样的方法安装

13. 安装golint,对go代码进行lint检查

[root@localhost golang.org]# cd /root/go/src/golang.org/x/
[root@localhost x]# git clone https://github.com/golang/lint
[root@localhost x]# cd ./lint/

[root@localhost golint]# go install

使用方法:在vim的命令模式下输入:GoLint

 

14. 让vim支持gocode

[root@localhost vim]# cd /root/go/src/github.com/gocode/vim

[root@localhost vim]# ./update.sh

 

15. 如果还需要安装其他的第三方库,请按照下面地址去学习

https://www.golangtc.com/download/package

这里就不一一演示了,所有操作和上面的都是一样的了

 

最后的~/.vimrc的文件是这样子的

[plain] view plain copy

1. set nocompatible              " be iMproved, required                                                             

2. filetype off                  " required                                                                          

3.                                                                                                                 

4. " set the runtime path to include Vundle and initialize                                                           

5. set rtp+=~/.vim/bundle/Vundle.vim                                                                                 

6.                                                                                                                   

7. call vundle#begin()                                                                                               

8.                                                                                                                   

9. " let Vundle manage Vundle, required                                                                              

10. Plugin 'gmarik/Vundle.vim'                                                                                        

11. Plugin 'fatih/vim-go'                                                                                             

12. Plugin 'git://github.com/scrooloose/nerdtree.git'                                                                 

13. Plugin 'git://github.com/Xuyuanp/nerdtree-git-plugin.git'                                                         

14. Plugin 'Shougo/neocomplete'                                                                                       

15. Plugin 'Shougo/neosnippet'                                                                                        

16. Plugin 'Shougo/neosnippet-snippets'                                                                               

17. Plugin 'jstemmer/gotags'                                                                                          

18. Plugin 'majutsushi/tagbar'                                                                                        

19.                                                                                                                    

20. " All of your Plugins must be added before the following line                                                     

21. call vundle#end()            " required                                                                           

22.                                                                                                                  

23. filetype plugin indent on    " required  

24.    

25. let g:go_version_warning = 0  

26. let g:go_highlight_functions = 1  

27. let g:go_highlight_methods = 1  

28. let g:neocomplete#enable_at_startup = 1  

29.   

30. " NERDTree config  

31. map  <F7> :NERDTreeToggle<CR>  

32. autocmd bufenter * if (winnr("$") == 1 && exists("b:NERDTreeType") && b:NERDTreeType == "primary") | q | endif  

33. autocmd vimenter * NERDTree  

34.   

35. " mark config  

36. nmap ml <Plug>MarkSet  

37. nmap md <Plug>MarkClear  

38. nmap mn <Plug>MarkSearchAnyNex  

39. nmap mp <Plug>MarkSearchAnyPrev  

40. nmap mf <Plug>MarkSearchCurrentNext  

41. nmap mb <Plug>MarkSearchCurrentPrev  

42.   

43. nmap <F8> :TagbarToggle<CR>  

44. let g:tagbar_type_go = {  

45.     \ 'ctagstype' : 'go',  

46.     \ 'kinds'     : [  

47.         \ 'p:package',  

48.         \ 'i:imports:1',  

49.         \ 'c:constants',  

50.         \ 'v:variables',  

51.         \ 't:types',  

52.         \ 'n:interfaces',  

53.         \ 'w:fields',  

54.         \ 'e:embedded',  

55.         \ 'm:methods',  

56.         \ 'r:constructor',  

57.         \ 'f:functions'  

58.     \ ],  

59.     \ 'sro' : '.',  

60.     \ 'kind2scope' : {  

61.         \ 't' : 'ctype',  

62.         \ 'n' : 'ntype'  

63.     \ },  

64.     \ 'scope2kind' : {  

65.         \ 'ctype' : 't',  

66.         \ 'ntype' : 'n'  

67.     \ },  

68.     \ 'ctagsbin'  : 'gotags',  

69.     \ 'ctagsargs' : '-sort -silent'  

70. \ }  

有时间再补充下各个插件是怎么使用的吧。

 

16. 安装ack,这里是新补充的

[root@localhost kingshard]# yum install -y ack

[root@localhost bundle]# git clone https://github.com/mileszs/ack.vim.git ~/.vim/bundle/ack

在~/.vimrc中添加插件位置添加Ack和在文件最后添加快捷键F4

Plugin 'mileszs/ack'

map <F4> :Ack

光标停留在某函数或变量名上,按F4回车,即可全局搜索调用处

 

总结:可能会经常使用的快捷键

打开目录窗口:F7

打开符号窗口:F8

各窗口间的相互跳转:ctr+w+w

跳转到函数定义处:gd

向前:tab键

向后:ctr+o

高亮光标所在的单词:ml

取消光标所在单词的高亮:md

下一个高亮的单词:mf

上一个高亮的单词:mp

 


本文由职坐标整理并发布,希望对同学们有所帮助。了解更多详情请关注职坐标系统运维之Linux!


本文由 @白羽 发布于职坐标。未经许可,禁止转载。
喜欢 | 0 不喜欢 | 0
看完这篇文章有何感觉?已经有0人表态,0%的人喜欢 快给朋友分享吧~
评论(0)
后参与评论

您输入的评论内容中包含违禁敏感词

我知道了

助您圆梦职场 匹配合适岗位
验证码手机号,获得海同独家IT培训资料
选择就业方向:
人工智能物联网
大数据开发/分析
人工智能Python
Java全栈开发
WEB前端+H5

请输入正确的手机号码

请输入正确的验证码

获取验证码

您今天的短信下发次数太多了,明天再试试吧!

提交

我们会在第一时间安排职业规划师联系您!

您也可以联系我们的职业规划师咨询:

小职老师的微信号:z_zhizuobiao
小职老师的微信号:z_zhizuobiao

版权所有 职坐标-一站式IT培训就业服务领导者 沪ICP备13042190号-4
上海海同信息科技有限公司 Copyright ©2015 www.zhizuobiao.com,All Rights Reserved.
 沪公网安备 31011502005948号    

©2015 www.zhizuobiao.com All Rights Reserved

208小时内训课程