当前位置: 首页 > 工具软件 > Powerline Go > 使用案例 >

打造超级舒适的Go开发环境之VIM配置

程振濂
2023-12-01

最近博主已经从VIM迁移到了EMACS作为主要生产工具,目的是为了学习Lisp和体验EMACS操作系统之美,哈哈,不要被我弄的技术焦虑,没错,我就是换了个吃饭的盘子。写下这篇博客也是为了纪念我顺利的“从VIM毕业了”。

在阅读本文之前,你需要:

  • 准备一个UNIX like的操作系统
  • 一个可以跨过山和大海的代-理

话不多说,开始吧~

(1)下载必要的Go工具链

go get -v github.com/mdempsky/gocode
go get -v github.com/ramya-rao-a/go-outline
go get -v github.com/acroca/go-symbols
go get -v github.com/uudashr/gopkgs
go get -v golang.org/x/tools/cmd/guru
go get -v golang.org/x/tools/cmd/gorename
go get -v github.com/sqs/goreturns
go get -v github.com/rogpeppe/godef
go get -v golang.org/x/tools/cmd/godoc
go get -v golang.org/x/lint/golint
go get -v github.com/derekparker/delve/tree/master/cmd/dlv
go get -v github.com/fatih/gomodifytags
go get -v github.com/haya14busa/goplay
go get -v github.com/josharian/impl
go get -v github.com/tylerb/gotype-live
go get -v github.com/cweill/gotests
go get -v github.com/sourcegraph/go-langserver
go get -v github.com/davidrjenni/reftools/tree/master/cmd/fillstruct

(2)安装VIM

macOS用户可能需要安装一下macvim:

brew install macvim

(3)主题配置

为了实现类似于Atom的Onedark和Onelight模式,我们可能需要安装两个主题:

  • onedark.vim(只支持黑暗模式):https://github.com/joshdick/onedark.vim.git
  • one.vim(同时支持):https://github.com/rakr/vim-one.git

.vim目录下创建三个目录:autoload,bundle,colors,将两个仓库克隆到本地,然后将autoload目录的one.vim或者onedark.vim拷贝到autoload目录,将colors目录的one.vim或者onedark.vim拷贝到colors目录。

配置主题,修改~/.vimrc文件:

colorscheme one
set background=dark

当你想要将主题更改为Onelight的时候,只要将dark替换为light即可。

(4)插件配置

首先要安装插件管理器Vundle:

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

然后配置~/.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()
" 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
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

为了支持多种编程语言,我们采用YouCompleteMe插件,macOS用户使用如下命令:

brew install cmake python python3 mono go nodejs

如果你已经使用nvm作为Node.js版本管理器,或者已经通过PKG方式安装了Python3.x版本,那么只需要PATH可以识别到命令,另外nvm用户需要安装npm:

brew install npm

接下来需要进行编译:

cd ~/.vim/bundle/YouCompleteMe
python3 install.py --all

由于YouCompleteMe插件使用Vundle安装可能导致失败,因此通过手动安装的方式实现,其他插件均可以通过Vundle安装:

Plugin 'fatih/vim-go'
Plugin 'Tagbar'
Plugin 'scrooloose/nerdtree'
Plugin 'Xuyuanp/nerdtree-git-plugin'
Plugin 'vim-airline/vim-airline'
Plugin 'vim-airline/vim-airline-themes'

写到.vimrc文件中,然后打开vim,进入命令模式,使用如下命令安装:

:PluginInstall

vim-go插件需要下载一些工具包,如果使用Vundle安装失败,可以进入vim使用如下命令:

:GoInstallBinaries

配置完成后的整个.vimrc文件如下:

set nocompatible              " be iMproved, required
filetype off                  " required
syntax on
colorscheme one
let g:one_allow_italics = 1
set background=dark
set nu
let NERDTreeWinSize=50
let NERDTreeShowLineNumbers=1
let NERDTreeAutoCenter=1
let NERDTreeShowHidden=1
let g:go_gopls_enabled = 0

set laststatus=2 "始终显示状态栏
set tabstop=2 "一个制表符的长度
set softtabstop=2 "一个制表符的长度(可以大于tabstop)
set shiftwidth=2 "一个缩进的长度
set expandtab "使用空格替代制表符
set smarttab "智能制表符
set autoindent "自动缩进
set smartindent "只能缩进
set ruler "显示位置指示器
set ignorecase "检索时忽略大小写
set hls "检索时高亮显示匹配项
set helplang=cn "帮助系统设置为中文
set foldmethod=syntax "代码折叠

set guifont=PowerlineSymbols\ for\ Powerline
set nocompatible
set t_Co=256
let g:Powerline_symbols = 'fancy'

" let g:nerdtree_tabs_open_on_console_startup=1

"Use 24-bit (true-color) mode in Vim/Neovim when outside tmux.
"If you're using tmux version 2.2 or later, you can remove the outermost $TMUX check and use tmux's 24-bit color support
"(see < http://sunaku.github.io/tmux-24bit-color.html#usage > for more information.)
if (empty($TMUX))
  if (has("nvim"))
    "For Neovim 0.1.3 and 0.1.4 < https://github.com/neovim/neovim/pull/2198 >
    let $NVIM_TUI_ENABLE_TRUE_COLOR=1
  endif
  "For Neovim > 0.1.5 and Vim > patch 7.4.1799 < https://github.com/vim/vim/commit/61be73bb0f965a895bfb064ea3e55476ac175162 >
  "Based on Vim patch 7.4.1770 (`guicolors` option) < https://github.com/vim/vim/commit/8a633e3427b47286869aa4b96f2bfc1fe65b25cd >
  " < https://github.com/neovim/neovim/wiki/Following-HEAD#20160511 >
  if (has("termguicolors"))
    set termguicolors
  endif
endif

" 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'
Plugin 'fatih/vim-go'
Plugin 'Valloric/YouCompleteMe',{'do':'python3 install.py --all'}
Plugin 'Tagbar'
Plugin 'scrooloose/nerdtree'
Plugin 'Xuyuanp/nerdtree-git-plugin'
Plugin 'vim-airline/vim-airline'
Plugin 'vim-airline/vim-airline-themes'

" All of your Plugins must be added before the following line
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

let g:go_highlight_types = 1
let g:go_highlight_fields = 1
let g:go_highlight_functions = 1
let g:go_highlight_function_calls = 1
let g:go_highlight_operators = 1
let g:go_highlight_extra_types = 1
 类似资料: