使用 vim 开发 golang

326 阅读3分钟

前言

从 Android 开发转 go 开发之后有很长一段时间都是用的 vim,主要是因为公司 go 工程本身并不是很复杂,也一直想试试脱离鼠标写代码是怎样的感受。刚开始用的是 SpaceVim,开箱即用,集成了大部分常用的插件,支持的语言也非常的多。但是最近突然开始报错,有一些文件夹无法创建,自己切到相应的目录底下 mkdir 也不行,于是便想着自己整一套最简单的可以用来开发 vim 的配置,我集成的插件:

  • Vundle:插件管理
  • dracula/vim:主题
  • lightline:状态行
  • vim-go:go 的 vim 插件
  • YouCompleteMe:代码提示

完整配置

先直接放完整的配置文件,vim 对应的是 ~/.vimrc,neovim 对应的是 ~/.config/nvim/init.vim:

set number
set ignorecase

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()
" alternatively, pass a path where Vundle should install plugins
"call vundle#begin('~/some/path/here')

" let Vundle manage Vundle, required
Plugin 'VundleVim/Vundle.vim'

" The following are examples of different formats supported.
" Keep Plugin commands between vundle#begin/end.
" plugin on GitHub repo
" Plugin 'tpope/vim-fugitive'
" plugin from http://vim-scripts.org/vim/scripts.html
" Plugin 'L9'
" Git plugin not hosted on GitHub
" Plugin 'git://git.wincent.com/command-t.git'
" git repos on your local machine (i.e. when working on your own plugin)
" Plugin 'file:///home/gmarik/path/to/plugin'
" The sparkup vim script is in a subdirectory of this repo called vim.
" Pass the path to set the runtimepath properly.
" Plugin 'rstacruz/sparkup', {'rtp': 'vim/'}
" Install L9 and avoid a Naming conflict if you've already installed a
" different version somewhere else.
" Plugin 'ascenator/L9', {'name': 'newL9'}

" All of your Plugins must be added before the following line
"
" themes
Plugin 'dracula/vim', { 'name': 'dracula' }

" golang plugin
Plugin 'fatih/vim-go'
" code complete
Plugin 'ycm-core/YouCompleteMe'
" light line
Plugin 'itchyny/lightline.vim'

call vundle#end()            " required

filetype plugin indent on    " required
" To ignore plugin indent changes, instead use:
"filetype plugin on
"
" Brief help
" :PluginList       - lists configured plugins
" :PluginInstall    - installs plugins; append `!` to update or just :PluginUpdate
" :PluginSearch foo - searches for foo; append `!` to refresh local cache
" :PluginClean      - confirms removal of unused plugins; append `!` to auto-approve removal
"
" see :h vundle for more details or wiki for FAQ
" Put your non-Plugin stuff after this line

au BufReadPost * if line("'"") > 0 | if line("'"") <= line("$") | exe("norm '"") | else |exe "norm $"| endif | endif

colorscheme dracula
set encoding=utf-8

inoremap ( ()<ESC>i
inoremap [ []<ESC>i
inoremap { {}<ESC>i
inoremap < <><ESC>i

vnoremap <leader>y "+y 
  • set number:开启行号
  • set ignorecase:vim 中搜索忽略大小写
  • au BufReadPost * if line("'"") > 0 | if line("'"") <= line("") | exe("norm '"") | else |exe "norm "| endif | endif:vim 打开文件自动跳转到上一次光标所在位置
  • colorscheme dracula:指定主题
  • inoremap ( ()i inoremap [ []i inoremap { {}i inoremap < <>i:输入左边的括号自动补全右边的
  • vnoremap y "+y:vim 中的 leader 默认为 \,可以改,我这没改,visual 模式下复制选中内容

直接使用这套配置,在 vim 打开之后执行 :PluginInstall,待插件安装完成后即可,但是代码提示 YouCompleteMe 需要额外的准备工作。编译 YouCompleteMe 需要依赖一些工具:cmake、python、go、nodejs,我是 mac 直接执行:

$ brew install cmake python go nodejs

安装完成后进入 YouCompleteMe 的目录:

cd ~/.vim/bundle/YouCompleteMe

如果没有,先将 YouCompleteMe clone 到对应目录:

git clone git@github.com:ycm-core/YouCompleteMe.git ~/.vim/bundle/YouCompleteMe

安装 go support:

./install.py --go-completer

等待执行完毕,一个简单可用的 vim go 开发环境就搭建完成了。至于有些人可能习惯将 vim 当成 ide,装上项目内搜索、文件目录插件,而我只是将之作为代码编辑器,其他的操作都可以通过命令代替:ag、grep、tree、ls -lah、touch、mkdir 等,见仁见智。

image.png