溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

golang如何配置vim環(huán)境

發(fā)布時間:2020-05-23 14:49:19 來源:億速云 閱讀:247 作者:鴿子 欄目:編程語言

一、環(huán)境準備:

系統環(huán)境說明:

[root@docker golang]# cat /etc/redhat-release 
CentOS Linux release 7.2.1511 (Core) 
[root@docker golang]# uname -a
Linux docker 3.10.0-229.el7.x86_64 #1 SMP Fri Mar 6 11:36:42 UTC 2015 x86_64 x86_64 x86_64 GNU/Linux
[root@docker golang]#

準備一個go文件,用于觀察配置插件過程中的變化:

//hellogolang.go
package main
import "fmt"
func main() {
        fmt.Println("Hello Golang!")
}

二、插件配置之路:

1、Vundle.vim:

#mkdir ~/.vim/bundle
#git clone https://github.com/gmarik/Vundle.vim.git ~/.vim/bundle/Vundle.vim

配置vimrc:創(chuàng)建~/.vimrc文件(如果你沒有這個文件的話),在文件頂部添加有關Vundle.vim的配置:

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()

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

" All of your Plugins must be added before the following line
call vundle#end()            " required
filetype plugin indent on    " required

此時Vim僅安裝了Vundle.vim這一個插件。編輯hellogolang.go時與編輯普通文本文件無異,一切都還是Vim的默認屬性

2、vim-go

Vim-go是當前使用最為廣泛的用于搭建Golang開發(fā)環(huán)境的vim插件,這里我同樣使用vim-go作為核心和基礎進行環(huán)境搭建的。vim-go利 用開源Vim插件管理器安裝,gmarik/Vundle.vim是目前被推薦次數更多的Vim插件管理器,超過了pathogen。

這里我們 就用vundle來作為Vim的插件管理工具。

編輯~/.vimrc,在vundle#begin和vundle#end間增加一行:

Plugin 'fatih/vim-go'

在Vim內執(zhí)行:PluginInstall,

Vundle.vim會在左側打開一個Vundle Installer Preview子窗口,窗口下方會提示:“Processing 'fatih/vim-go'”,待安裝完畢后,提示信息變 成“Done!”。

這時,我們可以看到.vim/bundle下多了一個vim-go文件夾:

$ ls .vim/bundle/
vim-go/  Vundle.vim/

此時,再次編輯hellogolang.go,語法高亮有了, 保存時自動format(利用$GOBIN/gofmt)也有了,但其他高級功能,比如自動import缺失的 package、自動補齊仍然沒有,我們還要繼續(xù)安裝一些東東。

3、安裝go.tools Binaries 

vim-go安裝說明中提到所有必要的binary需要先安裝好,比如gocode、godef、goimports等。

通過:GoInstallBinaries,這些vim-go依賴的二進制工具將會自動被下載,并被安裝到$GOBIN下或$GOPATH/bin下。(這個工具需要依賴git或hg,需要提前安裝到你的OS中。)

:GoInstallBinaries的執(zhí)行是交互式的,你需要回車確認:

vim-go: gocode not found. Installing github.com/nsf/gocode to folder /home/tonybai/go/bin
vim-go: goimports not found. Installing code.google.com/p/go.tools/cmd/goimports to folder /home/tonybai/go/bin/
vim-go: godef not found. Installing code.google.com/p/rog-go/exp/cmd/godef to folder /home/tonybai/go/bin/
vim-go: oracle not found. Installing code.google.com/p/go.tools/cmd/oracle to folder /home/tonybai/go/bin/
vim-go: gorename not found. Installing code.google.com/p/go.tools/cmd/gorename to folder /home/tonybai/go/bin/
vim-go: golint not found. Installing github.com/golang/lint/golint to folder /home/tonybai/go/bin/
vim-go: errcheck not found. Installing github.com/kisielk/errcheck to folder /home/tonybai/go/bin/

不過這些代碼多在code.google.com上托管,因此由于眾所周知的原因,vim-go的自動安裝很可能以失敗告終,這樣就需要你根據上 面日志中提到的各個工具的源碼地址逐一去下載并本地安裝。無法搭梯子的,可以通過http://gopm.io 下載相關包。

安裝后,$GOBIN下的新增Binaries如下:

-rwxr-xr-x  1 tonybai tonybai  5735552 11??  7 11:03 errcheck*
-rwxr-xr-x  1 tonybai tonybai  9951008 11??  7 10:33 gocode*
-rwxr-xr-x  1 tonybai tonybai  5742800 11??  7 11:07 godef*
-rwxr-xr-x  1 tonybai tonybai  4994120 11??  7 11:00 goimports*
-rwxr-xr-x  1 tonybai tonybai  5750152 11??  7 11:03 golint*
-rwxr-xr-x  1 tonybai tonybai  6381832 11??  7 11:01 gorename*
-rwxr-xr-x  1 tonybai tonybai  2954392 11??  7 10:38 gotags*
-rwxr-xr-x  1 tonybai tonybai  9222856 11??  7 11:01 oracle*

安裝好這些Binaries后,我們來看看哪些特性被支持了。

再次編輯hellogolang.go:

- 新起一行輸入fmt.,然后ctrl+x, ctrl+o,Vim 會彈出補齊提示下拉框,不過并非實時跟隨的那種補齊,這個補齊是由gocode提供的。

– 輸入一行代碼:time.Sleep(time.Second),執(zhí)行:GoImports,Vim會自動導入time包。

– 將光標移到Sleep函數上,執(zhí)行:GoDef或命令模式下敲入gd,Vim會打開$GOROOT/src/time/sleep.go中 的Sleep函數的定義。執(zhí)行:b 1返回到hellogolang.go。

– 執(zhí)行:GoLint,運行golint在當前Go源文件上。

– 執(zhí)行:GoDoc,打開當前光標對應符號的Go文檔。

– 執(zhí)行:GoVet,在當前目錄下運行go vet在當前Go源文件上。

– 執(zhí)行:GoRun,編譯運行當前main package。

– 執(zhí)行:GoBuild,編譯當前包,這取決于你的源文件,GoBuild不產生結果文件。

– 執(zhí)行:GoInstall,安裝當前包。

– 執(zhí)行:GoTest,測試你當前路徑下地_test.go文件。

– 執(zhí)行:GoCoverage,創(chuàng)建一個測試覆蓋結果文件,并打開瀏覽器展示當前包的情況。

– 執(zhí)行:GoErrCheck,檢查當前包種可能的未捕獲的errors。

– 執(zhí)行:GoFiles,顯示當前包對應的源文件列表。

– 執(zhí)行:GoDeps,顯示當前包的依賴包列表。

– 執(zhí)行:GoImplements,顯示當前類型實現的interface列表。

– 執(zhí)行:GoRename [to],將當前光標下的符號替換為[to]。

三、其他插件

到目前為止,我們還有若干特性沒能實現,重點是:

– 實時跟隨的代碼補齊
– Code Snippet support

1、安裝YCM(Your Complete Me)

在~/.vimrc中添加一行:

Plugin 'Valloric/YouCompleteMe'

保存退出后,再打開~/.vimrc并執(zhí)行:PluginInstall

安裝完后,下面的提示欄提示:

ycm_client_support.[so|pyd|dll] and ycm_core.[so|pyd|dll] not detected; you need to compile YCM before using it. Read the docs!

YCM是用了C++編寫的模塊對性能進行優(yōu)化了,于是需要手工編譯YCM的support庫。步驟如下:

sudo yum install build-essential cmake python-dev
cd ~/.vim/bundle/YouCompleteMe
./install.sh

構建(編譯C++很慢,需要耐心的等一會)ok后,再打開hellogolang.go,逐字的實時補全功能就具備了!Cool!

2、安裝 UltiSnips

Vim-go默認是用ultisnips引擎插件,但這個插件需要單獨安裝。同樣,我們利用vundle來安裝它,在~/.vimrc中添加一行:

Plugin 'SirVer/ultisnips'

保存,退出vim

打開vim,執(zhí)行:PluginInstall

編輯hellogolang.go,按照go.snip中的說明,我們輸入func后敲擊tab鍵,我們發(fā)現期待的:

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 = '<C-Space>'

" UltiSnips setting
let g:UltiSnipsExpandTrigger="<tab>"
let g:UltiSnipsJumpForwardTrigger="<c-b>"
let g:UltiSnipsJumpBackwardTrigger="<c-z>"

這樣讓YCM通過回車和向下的箭頭來做list item正向選擇,通過向上箭頭做反向選擇。通過ctrl+space來原地觸發(fā)補齊提示。

而ultisnips則是用tab做snippet展開,ctrl+b正向切換占位符,ctrl+z反向切換占位符。

四、.vimrc

前面講到了vim-go有許多命令,在:xx模式下執(zhí)行多顯不便,于是你可以定義一些Mappings,比如:

 " set mapleader
  let mapleader = ","

  " vim-go custom mappings
  au FileType go nmap <Leader>s <Plug>(go-implements)
  au FileType go nmap <Leader>i <Plug>(go-info)
  au FileType go nmap <Leader>gd <Plug>(go-doc)
  au FileType go nmap <Leader>gv <Plug>(go-doc-vertical)
  au FileType go nmap <leader>r <Plug>(go-run)
  au FileType go nmap <leader>b <Plug>(go-build)
  au FileType go nmap <leader>t <Plug>(go-test)
  au FileType go nmap <leader>c <Plug>(go-coverage)
  au FileType go nmap <Leader>ds <Plug>(go-def-split)
  au FileType go nmap <Leader>dv <Plug>(go-def-vertical)
  au FileType go nmap <Leader>dt <Plug>(go-def-tab)
  au FileType go nmap <Leader>e <Plug>(go-rename)  

這樣我們在命令模式下,輸入<,>+<r>就是運行 當前main包,以此類推。

另外下面這個配置使得我們在save file時既可以格式化代碼,又可以自動插入包導入語句(或刪除不用的包導入語句)。

 " vim-go settings
let g:go_fmt_command = "goimports"

到這里,我們的Vim Golang開發(fā)環(huán)境就基本搭建好了。snippet+實時補齊讓你Coding如飛!

五、.vimrc文件

下面是截至目前為止全量.vimrc文件的內容:

set nocompatible              " be iMproved, required
filetype off                  " required
colorscheme molokai

" set the runtime path to include Vundle and initialize
set rtp+=~/.vim/bundle/Vundle.vim
call vundle#begin()

" let Vundle manage Vundle, required
Plugin 'gmarik/Vundle.vim'
Plugin 'fatih/vim-go'
Plugin 'Valloric/YouCompleteMe'

Plugin 'SirVer/ultisnips'

" All of your Plugins must be added before the following line
call vundle#end()            " required
filetype plugin indent on    " required

" set mapleader
let mapleader = ","

" vim-go custom mappings
au FileType go nmap <Leader>s <Plug>(go-implements)
au FileType go nmap <Leader>i <Plug>(go-info)
au FileType go nmap <Leader>gd <Plug>(go-doc)
au FileType go nmap <Leader>gv <Plug>(go-doc-vertical)
au FileType go nmap <leader>r <Plug>(go-run)
au FileType go nmap <leader>b <Plug>(go-build)
au FileType go nmap <leader>t <Plug>(go-test)
au FileType go nmap <leader>c <Plug>(go-coverage)
au FileType go nmap <Leader>ds <Plug>(go-def-split)
au FileType go nmap <Leader>dv <Plug>(go-def-vertical)
au FileType go nmap <Leader>dt <Plug>(go-def-tab)
au FileType go nmap <Leader>e <Plug>(go-rename)

" vim-go settings
let g:go_fmt_command = "goimports"

" YCM settings
let g:ycm_key_list_select_completion = ['', '']
let g:ycm_key_list_previous_completion = ['', '']
let g:ycm_key_invoke_completion = '<C-Space>'

" UltiSnips settings
let g:UltiSnipsExpandTrigger="<tab>"
let g:UltiSnipsJumpForwardTrigger="<c-b>"
let g:UltiSnipsJumpBackwardTrigger="<c-z>"

以上就是go語言環(huán)境vim配置詳解的詳細內容,更多請關注億速云其它相關文章!

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內容(圖片、視頻和文字)以原創(chuàng)、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI