备份.vimrc # cd ~ # cp .vimrc .vimrc_$(date +%F) 安装vim-nox, nox版的vim支持lua,避免后面lua不支持的问题 # apt install vim-nox 安装插件管理工具 # curl -fLo ~/.vim/autoload/plug.vim --create-dirs https://raw.githubusercontent.com/junegunn/vim-plug/master/plug.vim 在~/.vimrc中添加: call plug#begin() Plug 'fatih/vim-go' 然后在vim中执行下面命令安装vim-go :PlugInstall 后面的命令需要红杏出墙: export http_proxy=红杏出墙主机:红杏出墙端口号 export https_proxy=红杏出墙主机:红杏出墙端口号 安装go tools 二进制,执行下面vim命令 :GoInstallBinaries
至此,编辑go源码文件时,vim已经具备如下功能:
- 新起一行输入fmt.,然后ctrl+x, ctrl+o,Vim 会弹出补齐提示下拉框,不过并非实时跟随的那种补齐,这个补齐是由gocode提供的。
– 输入一行代码:time.Sleep(time.Second),执行:GoImports,Vim会自动导入time包。
– 将光标移到Sleep函数上,执行:GoDef或命令模式下敲入gd,Vim会打开$GOROOT/src/time/sleep.go中 的Sleep函数的定义。执行:b 1返回到hellogolang.go。
– 执行:GoLint,运行golint在当前Go源文件上。
– 执行:GoDoc,打开当前光标对应符号的Go文档。
– 执行:GoVet,在当前目录下运行go vet在当前Go源文件上。
– 执行:GoRun,编译运行当前main package。
– 执行:GoBuild,编译当前包,这取决于你的源文件,GoBuild不产生结果文件。
– 执行:GoInstall,安装当前包。
– 执行:GoTest,测试你当前路径下地_test.go文件。
– 执行:GoCoverage,创建一个测试覆盖结果文件,并打开浏览器展示当前包的情况。
– 执行:GoErrCheck,检查当前包种可能的未捕获的errors。
– 执行:GoFiles,显示当前包对应的源文件列表。
– 执行:GoDeps,显示当前包的依赖包列表。
– 执行:GoImplements,显示当前类型实现的interface列表。
– 执行:GoRename [to],将当前光标下的符号替换为[to]。
其他插件:
实时补齐功能 在 .vimrc文件的call plug#begin() 和 call plug#end()之间添加: Plug 'Valloric/YouCompleteMe' 执行vim命令: :PlugInstall 执行上面命令会报错,需要自己编译: apt install build-essential cmake python-dev cd ~/.vim/plugged/YouCompleteMe ./install.sh
安装molokai主题(类似我们平常ide的高亮效果):
cd /tmp
git clone git@github.com:tomasr/molokai.git
cp -r molokai/colors ~/.vim
然后将.vimrc添加:
colorscheme molokai set t_Co=256 set background=dark