0
  • 聊天消息
  • 系统消息
  • 评论与回复
登录后你可以
  • 下载海量资料
  • 学习在线课程
  • 观看技术视频
  • 写文章/发帖/加入社区
创作中心

完善资料让更多小伙伴认识你,还能领取20积分哦,立即完善>

3天内不再提示

Linux中如何用vim做golang开发环境

马哥Linux运维 来源:CSDN博客 作者:少年已不再年少年 2021-10-08 17:20 次阅读

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

安装 git

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

安装 Vundle.vim

mkdir ~/.vim/bundle

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

安装 vim-go

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

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

[root@localhost bundle]# vim ~/.vimrcset 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

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

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

下载 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

修改~/.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 《F7》 :NERDTreeToggle《CR》 ”快捷键 F7,可以收放目录树的视图

autocmd bufenter * if (winnr(“$”) == 1 && exists(“b:NERDTreeType”) &&b:NERDTreeType == “primary”) | q | endif

autocmd vimenter * NERDTree

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

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

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

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

安装 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’,

‘i1’,

‘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’

}

安装 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 《Plug》MarkSet #高亮或反高亮一个单词

nmap md 《Plug》MarkClear

nmap mn 《Plug》MarkSearchAnyNext #跳转到任一下一个高亮单词

nmap mp 《Plug》MarkSearchAnyPrev

nmap mf 《Plug》MarkSearchCurrentNext #跳转到当前高亮的下一个单词

nmap mb 《Plug》MarkSearchCurrentPrev

安装 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 下还有很多命令工具,可以使用同样的方法安装

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

让 vim 支持 gocode

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

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

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

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 《F7》 :NERDTreeToggle《CR》

autocmd bufenter * if (winnr(“$”) == 1 && exists(“b:NERDTreeType”) && b:NERDTreeType == “primary”) | q | endif

autocmd vimenter * NERDTree

“ mark config

nmap ml 《Plug》MarkSet

nmap md 《Plug》MarkClear

nmap mn 《Plug》MarkSearchAnyNex

nmap mp 《Plug》MarkSearchAnyPrev

nmap mf 《Plug》MarkSearchCurrentNext

nmap mb 《Plug》MarkSearchCurrentPrev

nmap 《F8》 :TagbarToggle《CR》

let g:tagbar_type_go = {

‘ctagstype’ : ‘go’,

‘kinds’ : [

‘p:package’,

‘i1’,

‘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’

}

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

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

转自:blog.csdn.net/pei2215015/article/details/79802351

责任编辑:haq

声明:本文内容及配图由入驻作者撰写或者入驻合作网站授权转载。文章观点仅代表作者本人,不代表电子发烧友网立场。文章及其配图仅供工程师学习之用,如有内容侵权或者其他违规问题,请联系本站处理。 举报投诉
  • Linux
    +关注

    关注

    87

    文章

    10991

    浏览量

    206736
  • VIM
    VIM
    +关注

    关注

    0

    文章

    129

    浏览量

    15180

原文标题:Linux 下 golang 的 vim 环境配置

文章出处:【微信号:magedu-Linux,微信公众号:马哥Linux运维】欢迎添加关注!文章转载请注明出处。

收藏 人收藏

    评论

    相关推荐

    Golang接口的作用和应用场景

    Golang(Go)作为一门现代的静态类型编程语言,提供了许多强大的特性,其中之一便是接口(interface)。接口是Golang中的一个核心概念,它具有广泛的应用场景,可以帮助开发者实现
    的头像 发表于 12-05 10:44 630次阅读

    linux怎么执行vim编辑的程序

    Linux系统中执行vim编辑的程序非常简单,只需要依次执行以下步骤: 打开终端:在Linux系统中,按下Ctrl+Alt+T组合键可以快速打开终端。 输入vim命令并指定文件:在终
    的头像 发表于 11-28 15:23 371次阅读

    linux怎么进入vim命令窗口

    进入 Vim 命令窗口可以通过以下几种方法实现: 在普通模式下使用冒号命令(:):在 Vim 的普通模式下按下冒号键,即可在底部输入栏中输入命令。你可以输入不同的命令来执行不同的操作,如保存文件
    的头像 发表于 11-28 15:21 1231次阅读

    linux使用vim新建并编辑文件

    Linux系统下,Vim是一款功能强大的文本编辑器。它可以用于新建并编辑文件,具有很多高级功能和快捷键。下面是关于如何在Linux上使用Vim新建并编辑文件的细致的说明: 一、什么是
    的头像 发表于 11-28 15:17 1023次阅读

    linux怎么保存退出vim

    Linux系统中,使用vim作为编辑器是非常常见的。vim是一个强大的文本编辑器,拥有丰富的功能和快捷键。保存并退出vim可以通过以下步骤完成: 打开
    的头像 发表于 11-27 14:19 3605次阅读

    linux怎么执行vim编辑的程序

    Linux中,你可以通过以下步骤执行Vim编辑器中编写的程序: 打开终端或命令行界面。 使用Vim编辑器打开你的程序文件。假设你的程序文件名为"program.c",可以使用以下命令打开文件并进
    的头像 发表于 11-26 15:45 521次阅读

    linux怎样进入vim编辑模式

    Linux中,要进入Vim编辑器的编辑模式,可以按照以下步骤进行操作: 打开终端或命令行界面。 输入以下命令来启动Vim编辑器: vim 文件名 其中,文件名是要编辑的文件的名称。如
    的头像 发表于 11-26 15:43 1761次阅读

    linuxvim三种模式切换

    Linux中,Vim编辑器具有三种模式,分别是命令模式、插入模式和末行模式。这三种模式有着不同的作用和功能,用户可以在它们之间进行切换以完成不同的操作。 命令模式 命令模式是Vim的默认模式,启动
    的头像 发表于 11-26 15:39 1452次阅读

    linux开发环境搭建

    ESP32教程-linux开发环境搭建
    发表于 10-09 06:53

    Ai-WB2系列模组linux开发环境搭建方案

    Linux下编译速度比windows快很多,一般推荐优先使用linux环境开发。Ubuntu是非常常见Linux操作系统。这里介绍下如何快
    发表于 09-09 09:55 324次阅读
    Ai-WB2系列模组<b class='flag-5'>linux</b><b class='flag-5'>开发</b><b class='flag-5'>环境</b>搭建方案

    Vim之父去世后,Vim项目谁来接管?

    一直以来,Bram Moolenaar 被称为 Vim 项目的 BDFL(Benevolent Dictator For Life,终身仁慈独裁者),管理着 Vim 项目的各个方面,他的去世让无数使用过 Vim 的用户感到痛心,
    的头像 发表于 08-15 15:07 431次阅读
    <b class='flag-5'>Vim</b>之父去世后,<b class='flag-5'>Vim</b>项目谁来接管?

    Linux系统开发环境搭建

    今天给大家讲一讲Linux系统开发环境搭建。
    发表于 07-12 14:51 688次阅读
    <b class='flag-5'>Linux</b>系统<b class='flag-5'>开发</b><b class='flag-5'>环境</b>搭建

    你会使用 Linux 编辑器 vim 吗?

    vim:是一款编辑器,只负责写代码;相当于 windows 的记事本;
    的头像 发表于 05-10 18:21 541次阅读
    你会使用 <b class='flag-5'>Linux</b> 编辑器 <b class='flag-5'>vim</b> 吗?

    Ai-WB2系列模组linux开发环境搭建教程

    Linux下编译速度比windows快很多,一般推荐优先使用linux环境开发。Ubuntu是非常常见Linux操作系统。这里介绍下如何快
    的头像 发表于 05-08 15:12 763次阅读
    Ai-WB2系列模组<b class='flag-5'>linux</b><b class='flag-5'>开发</b><b class='flag-5'>环境</b>搭建教程

    Linux开发工具资料之Vim、Vi

    本帖最后由 卿小小_9e6 于 2023-5-5 18:44 编辑 这是一本比较经典的Vim、Vi手册: 《Learning the vi and Vim Editors Power
    发表于 05-05 18:41