使用 Vim 开发 golang

前言

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

  • Vundle:插件管理

  • dracula/vim:主题

  • lightline:状态行

  • vim-go:go 的 vim 插件,装了之后 control + ] 可以跳转代码了

  • 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 
let g:ycm_confirm_extra_conf = 0
  • 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 等,见仁见智。代码片段其实也很有必要,很多模板代码可以快捷生成,自己敲确实也挺麻烦的,比如 go 中常见的 if err != nil,完全可以用 ierr + tab 直接生成出来,这里我没有安装,可以自己去装:https://github.com/honza/vim-snippets,代码提示还有 ale、coc.nvim 等,觉得编 YouCompleteMe 太麻烦的话可以尝试一下别的代码提示。

效果:
效果

提到的插件地址:

  • dracula/vim:https://github.com/dracula/vim
  • vim-go:https://github.com/fatih/vim-go
  • lightline:https://github.com/itchyny/lightline.vim
  • YouCompleteMe:https://github.com/ycm-core/YouCompleteMe
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值