Linux 下 golang 的 vim 环境配置

良许Linux

共 9065字,需浏览 19分钟

 · 2021-10-21


链接:blog.csdn.net/pei2215015/article/details/79802351

转载:Go开发大全

vim 做 golang 开发环境,多么炫酷!还没尝试过用 vim 做开发环境的同学可以跟着这篇文档把环境搭建起来了!

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

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

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

export GOPATH="/root/go"
export GOBIN="$GOPATH/bin"
export GOROOT="/usr/local/go"
export PATH="$PATH:/usr/local/go/bin"
export PATH="$PATH:/root/go/bin"
[root@localhost bundle]# source /etc/profile
  1. 安装 git
[root@localhost ~]# yum install -y git
  1. 安装 Vundle.vim
mkdir ~/.vim/bundle

git clone https://github.com/gmarik/Vundle.vim.git ~/.vim/bundle/Vundle.vim
  1. 安装 vim-go
git clone https://github.com/fatih/vim-go.git ~/.vim/bundle/vim-go
  1. 修改~/.vimrc,如果没有就创建。vimrc 文件
[root@localhost bundle]# 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()

"
 let Vundle manage Vundle, required
Plugin 'gmarik/Vundle.vim'
Plugin 'fatih/vim-go'

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

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
  1. 创建一个测试文件 test.go,将发现可以高亮了
[root@localhost bundle]# vim ./test.go
  1. 下载 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
  1. 修改~/.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()

"
 let Vundle manage Vundle, required
Plugin 'gmarik/Vundle.vim'
Plugin 'fatih/vim-go'
Plugin 'git://github.com/scrooloose/nerdtree.git'
Plugin 'git://github.com/Xuyuanp/nerdtree-git-plugin.git'

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

filetype plugin indent on    " required

let g:go_version_warning = 0

"
 NERDTree config
map   :NERDTreeToggle    "快捷键 F7,可以收放目录树的视图
autocmd bufenter * if (winnr("
$") == 1 && exists("b:NERDTreeType") &&b:NERDTreeType == "primary") | q | endif
autocmd vimenter * NERDTree
  1. nerdtree 的使用,这里不讲解,大家可使用?命令来自己了解,如下
[root@localhost bundle]# vim ~/.vimrc

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

  1. 安装 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 即安装成功

  1. 安装 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文件最后加入以下内容:

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'
\ }
  1. 安装 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 中添加

nmap ml MarkSet #高亮或反高亮一个单词  
nmap md MarkClear
nmap mn MarkSearchAnyNext #跳转到任一下一个高亮单词  
nmap mp MarkSearchAnyPrev
nmap mf MarkSearchCurrentNext #跳转到当前高亮的下一个单词  
nmap mb MarkSearchCurrentPrev
  1. 安装 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 下还有很多命令工具,可以使用同样的方法安装

  1. 安装 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

  1. 让 vim 支持 gocode
[root@localhost vim]# cd /root/go/src/github.com/gocode/vim

[root@localhost vim]# ./update.sh
  1. 如果还需要安装其他的第三方库,请按照下面地址去学习

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

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

最后的~/.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()                                                                                             
                                                                                                                
"
 let Vundle manage Vundle, required                                                                            
Plugin 'gmarik/Vundle.vim'                                                                                      
Plugin 'fatih/vim-go'                                                                                           
Plugin 'git://github.com/scrooloose/nerdtree.git'                                                               
Plugin 'git://github.com/Xuyuanp/nerdtree-git-plugin.git'                                                       
Plugin 'Shougo/neocomplete'                                                                                     
Plugin 'Shougo/neosnippet'                                                                                      
Plugin 'Shougo/neosnippet-snippets'                                                                             
Plugin 'jstemmer/gotags'                                                                                        
Plugin 'majutsushi/tagbar'                                                                                      
                                                                                                                 
" All of your Plugins must be added before the following line                                                   
call vundle#end()            "
 required                                                                         
                                                                                                               
filetype plugin indent on    " required

let g:go_version_warning = 0
let g:go_highlight_functions = 1
let g:go_highlight_methods = 1
let g:neocomplete#enable_at_startup = 1

"
 NERDTree config
map   :NERDTreeToggle
autocmd bufenter * if (winnr("$") == 1 && exists("b:NERDTreeType") && b:NERDTreeType == "primary") | q | endif
autocmd vimenter * NERDTree

" mark config
nmap ml MarkSet
nmap md MarkClear
nmap mn MarkSearchAnyNex
nmap mp MarkSearchAnyPrev
nmap mf MarkSearchCurrentNext
nmap mb MarkSearchCurrentPrev

nmap  :TagbarToggle
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'
\ }

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

  1. 安装 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  :Ack

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

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

打开目录窗口:F7

打开符号窗口:F8

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

跳转到函数定义处:gd

向前:tab 键

向后:ctr+o

高亮光标所在的单词:ml

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

下一个高亮的单词:mf

上一个高亮的单词:mp




本公众号全部博文已整理成一个目录,请在公众号里回复「m」获取!

推荐阅读:

“脸书” 回应全网 宕机 7小时:误发一条指令!网友:这程序员不得祭天?

什么是 “千年虫” 问题?

精品 IDEA 插件大汇总!值得收藏


5T技术资源大放送!包括但不限于:C/C++,Linux,Python,Java,PHP,人工智能,单片机,树莓派,等等。在公众号内回复「1024」,即可免费获取!!


浏览 21
点赞
评论
收藏
分享

手机扫一扫分享

举报
评论
图片
表情
推荐
点赞
评论
收藏
分享

手机扫一扫分享

举报