Centos7下将vim打造成Golang的IDE

前言

后端开发绕不过的就是Linux,想在Linux下编写Go语言进行开发,我想最好用的IDE应该就是Vim了吧。今天费点时间,将自己的Vim打造成Golang专用的IDE,Let do it!

环境准备

  • 将你的vim升级到8.0以上
    因为要使用vim-go 的原因,它必须要求vim是8.0以上。

删除旧版本:yum remove vim

Centos7编译安装vim8
1.先下载依赖

yum install gcc ncurses ncurses-devel

yum -y install git ncurses-devel ruby ruby-devel lua lua-devel perl perl-devel python3 python3-devel python2-devel perl-ExtUtils-Embed lrzsz cmake wget gcc gcc-c++ unzi

2.访问https://github.com/vim/vim/releases,下载最新的release版本,然后传到linux上解压,我下载的是vim-8.2.1471.tar.gz

tar zxvf vim-8.2.1471.tar.gz
cd vim-8.2.1471

./configure --with-features=huge
–enable-multibyte
–enable-rubyinterp=yes
–enable-pythoninterp=yes
–with-python-config-dir=/usr/lib64/python2.7/config
–enable-perlinterp=yes
–enable-luainterp=yes
–enable-cscope
–prefix=/usr/local

make VIMRUNTIMEDIR=/usr/local/share/vim/vim82
make install

安装好了之后使用vim --verison查看版本
在这里插入图片描述如果你一不小心把VIMRUNTIMEDIR的版本值设置错了,会出现cannot open file …,这个时候只需要在/etc/profile中加入这个正确的值就好。

export VIMRUNTIMEDIR=/usr/local/share/vim/vim82;

  • 安装vim插件
    环境准备:
    前提:安装git
  1. mkdir .vim
  2. touch .vimrc
    创建完成之后,进入到 .vim 中,下载插件 Vim 的插件管理器bundle:
  3. mkdir bundle
    然后进入到 bundle 目录中,安装Vundle.vim:
  4. git clone https://github.com/VundleVim/Vundle.vim.git
    下载完成之后,会在当前目录下生成 Vundle.vim 目录
    同样进入到 bundle 目录中,安装Vim-go:
    5.git clone https://github.com/fatih/vim-go.git
    下载完成之后,会在当前目录下生成 vim-go 目录
    6.配置.vimrc
    我在这里直接给出我的配置
  1 " 关闭兼容模式
  2 set nocompatible   "be iMproved ,required
  3 filetype off       "required
  4 
  5 set rtp+=~/.vim/bundle/Vundle.vim
  6 call vundle#begin()
  7 
  8 Plugin 'VundleVim/Vundle.vim'
  9 Plugin 'fatih/vim-go',{'do':'GoUpdateBinaries'}
 10 
 11 call vundle#end()
 12 
 13 filetype plugin indent on  "required
 14 syntax on
 15 
 16 "------------------------------------------
 17 syntax enable   "语法高亮
 18 set nu          " 设置行号
 19 set cursorline  "突出显示当前行
 20 "set cursorcolumn " 突出显示当前列
 21 set showmatch   " 显示括号匹配
 22 set background=dark "设置背景色
 23 set t_Co=256
 24 set nocompatible "关闭与vi的兼容
 25 
 26 " tab 缩进
 27 set softtabstop=4 
 28 set tabstop=4 " 设置Tab长度为4空格
 29 set shiftwidth=4 " 设置自动缩进长度为4空格
 30 set backspace=2   "设置回退键长度,不设置backspace会失灵
 31 set autoindent " 继承前一行的缩进方式,适用于多行注释
 32 set encoding=utf-8
 33 
 34 " 定义快捷键的前缀,即
 35 let mapleader=";"
 36 "
 37 " " ==== 系统剪切板复制粘贴 ====
 38  " v 模式下复制内容到系统剪切板
 39 vmap c "+yy
 40 " " n 模式下复制一行到系统剪切板
 41 nmap <Leader>c "+yy
 42 " " n 模式下粘贴系统剪切板的内容
 43 nmap v "+p
 44 "
 45 " " 开启实时搜索
 46 set incsearch
 47 " " 搜索时大小写不敏感
 48 set ignorecase
 49 "call plug#end()
 50 "syntax on                    " 开启文件类型侦测
 51 "filetype plugin indent on    " 启用自动补全
 52 "
 53 " " 退出插入模式指定类型的文件自动保存
 54  au InsertLeave *.go,*.sh,*.php write

所有的插件配置都要放在vundle#begin()和call vundle#end()之间。现在打开就可以出现Go语言语法高亮了。

  • 安装go.tools Binaries
    进入vim中,只需要输入:

:GoInstallBinaries

这些vim-go依赖的二进制工具将会自动被下载,并被安装到GOBIN下或$GOPATH/bin下。(这个工具需要依赖git或hg,需要提前安装到你的OS中。)

如果安装失败,可以git clone github.com/nsf/gocode.git,然后在你的GOPATH下的src/gitbub中找到,然后go install github.com/nsf/gocode/,这时候目录下会生成一个 pkg 目录,里面有编译好的 .a 文件,就可以在 go 文件里使用这个包了。

由于有些命令文件是无法自动安装完成,这就需要手动进行安装啦。首先对比目录,看缺少哪些命令没有安装完成,然后去gitHub上搜索,找到对应的命令源码之后,使用git clone 下载到本地,然后使用 go install命令进行安装即可。前面已经讲解过如何安装啦,这里就不再赘述。

相关工具的gitHub路径:

golint: https://github.com/golang/lint.git

gocode: https://github.com/nsf/gocode.git

errcheck: https://github.com/kisielk/errcheck.git

gotags: https://github.com/jstemmer/gotags.git

(缺少什么工具,基本都可以找到的,这里就不一一列举了)

-安装代码补全工具YouConpleteMe
在~/.vimrc中添加 Plugin 'Valloric/YouCompleteMe’
打开vim 输入:PluginInstall(这个过程比较慢 会下载大约225MB文件)

在这里插入图片描述出现Done!之后就下载完成了。

YMC安装完成后,需要编译才能使用,编译时要选择支持的语言,这里选择Golang

cd ~/.vim/bundle/YouCompleteMe/
yum install cmake python-devel gcc-c++
./install.sh --go-completer

如不成功:
手动安装方法
因为安装过程中ycm一直安装不成功所以采用手动安装
git clone https://github.com/Valloric/YouCompleteMe.git
手动下载完后检查仓库的完整性,切换到 YouCompleteMe 目录下,输入如下命令:

# 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

问题:
YouCompleteMe unavailable: requires Vim compiled with Python (3.6.0+) suppor
解决方法:

./configure --with-features=huge --enable-pythoninterp=yes --enable-cscope --enable-fontset --with-python-config-dir=/usr/lib64/python3.6/config-3.6m-x86_64-linux-gnu --enable-pythoninterp=yes --enable-python3interp=yes
make
make install

重新编译即可。

  • 安装主题
    进入到 .vim目录下,使用git clone 下载主题

git clone https://github.com/tomasr/molokai.git

下载完成之后,拷贝 molokai/colors/molokai.vim 文件到 ~/.vim/colors 目录下

在.vimrc中添加:

colorscheme molokai
set t_Co=256
set background=dark

  • 安装 UltiSnips
    Vim-go默认是用ultisnips引擎插件,但这个插件需要单独安装。
    同样,我们利用vundle来安装它,在~/.vimrc中添加一行:
    Plugin ‘SirVer/ultisnips’
    snippet和snippet引擎是分开的。ultisnips是引擎,vim-go的go snippet定义在这里
    https://github.com/fatih/vim-go/blob/master/gosnippets/snippets/go.snip
    编辑hello.go,按照go.snip中的说明,我们输入func后敲击tab键,我们发现期待的:
    func name(params) type {

}
并没有出现。反倒是YCM的下拉提示显示在那里让你选择。似乎是ultisnips和YCM的键组合冲突了。ultisnips官方说明也的确如 此。ultisnips默认是用Tab展开snippet的,而YCM中的Tab用来选择补齐项,我们可以通过设置来避免这些。
我们在.vimrc中添加如下setting:

" YCM settings
let g:ycm_key_list_select_completion = ['', '']
let g:ycm_key_list_previous_completion = ['']
let g:ycm_key_invoke_completion = ''
" UltiSnips setting
let g:UltiSnipsExpandTrigger=""
let g:UltiSnipsJumpForwardTrigger=""
let g:UltiSnipsJumpBackwardTrigger=""

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

  • 安装NERDTree插件
    在.vimrc下面加上:
    Plugin ‘scrooloose/nerdtree’
    其他步骤与前面安装插件类似,略过。
    .vimrc的配置如下:
map <F7> :NERDTreeToggle<CR>
let g:NERDTreeWinPos = 'left'

配置好之后按F7或者输入NERDTree就可以出现列表了。

  • ​ delimitMate
    delimitMate是自动补全引号(单引号/双引号/反引号), 括号(()[]{})的插件。

​ 在 .vimrc中的配置如下:

Plugin ‘Raimondi/delimitMate’

你可能感兴趣的:(计算机语言---go语言,vim,golang)