1. 程式人生 > >支援GoLang,類似 Source Insight的vim編輯器搭建

支援GoLang,類似 Source Insight的vim編輯器搭建

,曾經配置過一個類似source insight的vim編輯器,(http://blog.csdn.net/linuxandroidwince/article/details/74202412)預設對C,C++語言是支援的,只是不支援golang,下面介紹下如果打造一個vim-go的編輯器,類似source insight,自動補齊等等, 我使用的是ubuntu16.04 LTS 32bits。

下載安裝GoLang

寫博時,官方穩定版本是1.8,下載的是linux 32bits的。
https://golang.org/dl/
下載後直接解壓到/usr/local/go下面即可,解壓到其他path也可以,沒有特別指定,只是習慣而已,比如解壓到$HOME/go。

配置環境變數

可以將下面內容複製到/etc/profile,也可以複製到~/.profile

 # go path
 export GOROOT=/usr/local/go
 export GOARCH=386
 export GOOS=linux
 export GOPATH=/home/jack/go
 export GOBIN=$GOPATH/bin
 export PATH=$PATH:$GOROOT/bin:$GOBIN

下面安裝的一些go工具,如果配置了GOBIN這個變數,都會安裝到這個目錄下,如果沒有配置這個變數,預設可執行檔案放在各自GOPATH目錄的bin資料夾中。

執行一下go命令,如果顯示go命令的Usage,說明已經安裝配置成功,或者go version看到版本資訊,也說明安裝成功。

hello GoLang

寫一段程式測試下:摘抄於官網

// You can edit this code!
// Click here and start typing.
package main

import "fmt"

func main() {
    fmt.Println("Hello, 世界")
}

$go run test.go
Hello, 世界

準備配置vim-go

由於配置vim-go,需要安裝一些go .tools binaries,這些工具預設會安裝在$GOBIN下面。安裝這些工具需要一些輔助工具,比如分散式版本控制系統Mercurial。
在 Ubuntu 下快速的最新版的安裝Mercurial, 其實只要幾條命令就可以搞定了.

$sudo add-apt-repository ppa:tortoisehg-ppa/releases
$sudo add-apt-repository ppa:mercurial-ppa/releases
$sudo apt-get update
$sudo apt-get install mercurial python-nautilus tortoisehg

安裝git, vim:

$sudo app-get install git
$sudo app-get install vim

安裝ctags-exuberant,這個工具,後面很多外掛會用到,tagbar, nerdtree, gotags等。

$sudo app-get install exuberant-ctags
$ctags-exuberant --version
Exuberant Ctags 5.9~svn20110310, Copyright (C) 1996-2009 Darren Hiebert
$ ctags-exuberant --list-languages //檢視支援那些語言
......
Flex
Fortran
Go
HTML
Java
......

另外,配置YCM時,由於YCM用到了C++實現效能上的優化,所以需要安裝C++的編譯環境:

$sudo apt-get install build-essential cmake python-dev

外掛管理工具vundle

vim-go官方提到兩種外掛管理工具,除vundle還有pathogen,只是選擇vundle的比較多,我這裡也以vundle為例介紹。

安裝Vundle.vim

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

後面安裝的plugin都安裝在~/.vim/bundle下面。

配置~/.vimrc檔案以支援plugin的安裝

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

儲存.vimrc退出,在bash下輸入vim命令回車,在命令列模式下輸入 :PluginInstall回車,這時在視窗底部會看到Processing ‘gmarik/Vundle.vim’,表示正在從網路下載這個外掛,等底部顯示Done!即表示下載成功,這時退出vim,到~/.vim/bundle下可以看到多了一個vundle的path。

安裝vim-go外掛

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

Plugin 'fatih/vim-go'

在Vim內執行 :PluginInstall, 視窗下方會提示:Processing ‘fatih/vim-go’, 待下載安裝完畢後,提示資訊變 成“Done!”

安裝go.tools Binaries

vim-go安裝說明中提到所有必要的binary需要先安裝好,比如gocode、godef、goimports等.
通過:GoInstallBinaries,這些vim-go依賴的二進位制工具將會自動被下載,並被安裝到GOBINGOPATH/bin下。(這個工具需要依賴git或hg,需要提前安裝到你的OS中。)

打vim,在命令列模式下輸入:GoInstallBinaries
它的執行是互動式的,你需要回車確認,即開始下載安裝。

:GoInstallBinaries
vim-go: gocode not found. Installing github.com/nsf/gocode to folder /home/jack/go/bin/
vim-go: gometalinter not found. Installing github.com/alecthomas/gometalinter to folder /home/jack
/go/bin/
vim-go: goimports not found. Installing golang.org/x/tools/cmd/goimports to folder /home/jack/go/b
in/
vim-go: guru not found. Installing golang.org/x/tools/cmd/guru to folder /home/jack/go/bin/
vim-go: gorename not found. Installing golang.org/x/tools/cmd/gorename to folder /home/jack/go/bin
/
vim-go: golint not found. Installing github.com/golang/lint/golint to folder /home/jack/go/bin/
vim-go: godef not found. Installing github.com/rogpeppe/godef to folder /home/jack/go/bin/
vim-go: errcheck not found. Installing github.com/kisielk/errcheck to folder /home/jack/go/bin/
vim-go: gotags not found. Installing github.com/jstemmer/gotags to folder /home/jack/go/bin/
vim-go: asmfmt not found. Installing github.com/klauspost/asmfmt/cmd/asmfmt to folder /home/jack/g
o/bin/
vim-go: motion not found. Installing github.com/fatih/motion to folder /home/jack/go/bin/
vim-go: gomodifytags not found. Installing github.com/fatih/gomodifytags to folder /home/jack/go/b
in/
vim-go: gogetdoc not found. Installing github.com/zmb3/gogetdoc to folder /home/jack/go/bin/
vim-go: impl not found. Installing github.com/josharian/impl to folder /home/jack/go/bin/
vim-go: keyify not found. Installing github.com/dominikh/go-tools/cmd/keyify to folder /home/jack/
go/bin/

注意下,這裡會訪問到google,由於我們的網路問題,需要翻下才行。

安裝成功後GOBIN下面:

[email protected]-VirtualBox:~$ ls $GOBIN -l
total 64412
-rwxrwxr-x 1 jack jack 2347772 719 17:29 asmfmt
-rwxrwxr-x 1 jack jack 4228667 719 17:28 errcheck
-rwxrwxr-x 1 jack jack 8656435 719 17:18 gocode
-rwxrwxr-x 1 jack jack 4556792 719 17:28 godef
-rwxrwxr-x 1 jack jack 4902326 719 17:29 gogetdoc
-rwxrwxr-x 1 jack jack 4024027 719 17:25 goimports
-rwxrwxr-x 1 jack jack 4322181 719 17:26 golint
-rwxrwxr-x 1 jack jack 5016936 719 17:25 gometalinter
-rwxrwxr-x 1 jack jack 3174321 719 17:29 gomodifytags
-rwxrwxr-x 1 jack jack 4551554 719 17:26 gorename
-rwxrwxr-x 1 jack jack 2276387 719 17:29 gotags
-rwxrwxr-x 1 jack jack 6897486 719 17:26 guru
-rwxrwxr-x 1 jack jack 3864326 719 17:29 impl
-rwxrwxr-x 1 jack jack 4486579 719 17:30 keyify
-rwxrwxr-x 1 jack jack 2623469 719 17:29 motion

這裡的gotags在後tagbar裡會用到。

安裝 UltiSnips

Vim-go預設是用ultisnips引擎外掛,但這個外掛需要單獨安裝。

同樣,我們利用vundle來安裝它,在~/.vimrc中新增一行:

Plugin 'SirVer/ultisnips'

這個外掛功能是,比如輸入func後敲擊tab鍵,會自動帶出如下這樣的:

func name(params) type{

}

安裝YCM(Your Complete Me)

在~/.vimrc中新增一行:

Plugin 'Valloric/YouCompleteMe'

儲存退出後,再開啟~/.vimrc並執行PluginInstall

下載完成後:

cd ~/.vim/bundle/YouCompleteMe
./install.sh

構建(編譯C++很慢,需要耐心的等一會)ok後,可以開啟test.go看下效果,會有自動補齊功能,很cool!

需要注意:YCM與ultisnips會有快捷鍵上的衝突。
ultisnips預設是用Tab展開snippet的,而YCM中的Tab用來選擇補齊項,我們可以通過設定來避免這些:

"================================
" 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>"

這樣讓YCM通過回車和向下的箭頭來做list item正向選擇,通過向上箭頭做反向選擇。通過ctrl+space來原地觸發補齊提示。
而ultisnips則是用tab做snippet展開,ctrl+b正向切換佔位符,ctrl+z反向切換佔位符。

安裝molokai theme

Molokai theme是TextMate的theme的vim port, 顏色不錯,配置比較簡單:

mkdir ~/.vim/colors
下載或複製https://github.com /fatih/molokai/blob/master/colors/molokai.vim到~/.vim /colors目錄下
在.vimrc新增一行:colorscheme molokai

下面來張cool圖展示下:

這裡寫圖片描述

安裝NERDTree外掛

在.vimrc下面加上:
Plugin ‘scrooloose/nerdtree’
其他步驟與前面安裝外掛類似,略過。
下面主要看下.vimrc下面的配置:

"===============================
" NERDTree settings
" ==============================
" F2 to open NERDTree
map <F7> :NERDTreeToggle<CR>

" modify the icon of the tree
"let g:NERDTreeDirArrowExpandable = '+'
"let g:NERDTreeDirArrowCollapsible = '-'

" show postion of the window
let g:NERDTreeWinPos = 'left'

" the size of window
let g:NERDTreeSize = 30

" show line num in window
"let g:NERDTreeShowLineNumber = 1

" if show hidden file
"let g:NERDTreeHidden = 0

" open vim, if no file, open NERDTree automatically
" autocmd vimenter * if !argc()|NERDTree|endif

" automatically close when the NERDTree is the only one window
" autocmd bufenter * if (winnr("$")==1 && exists("b:NERDTree") &&
" b:NERDTree.isTabTree())|q|endif

" open NERDTree automatically when open vim
" autocmd vimenter * NERDTree</cr></f2>

安裝Tagbar外掛

在.vimrc下面加上:
Plugin ‘majutsushi/Tagbar’
其他步驟與前面安裝外掛類似,略過。
tagbar支援多種語言,可以參考:
https://github.com/majutsushi/tagbar/wiki#google-go
下面主要看下支援golang的配置,.vimrc下面的配置:

"===============================
" Tagbar settings
" ==============================
" Set width of the Tagbar window
let g:tagbar_width = 30
" map shutcut key for Tagbar, press F8 to open automatically
nmap <F8> :TagbarToggle<CR>

" tagbar usr ctags plugin
" let g:tagbar_ctags_bin = 'ctags'

" show on left, default is right
" let g:tagbar_left = 1

" set tagbar window width, default size is 40
" let g:tagbar_width = 30

" consur stays on tagbar window, default is on vim window
let g:tagbar_autofocus = 1

" set label not sorted, default is sorted
let g:tagbar_sort = 0

" support golang
let g:tagbar_type_go = {
    \ 'ctagstype': 'go',
    \ 'kinds' : [
        \'p:package',
        \'f:function',
        \'v:variables',
        \'t:type',
        \'c:const'
    \]
\}

注意:g:tagbar_type_go 這裡的設定在官方網站上提供了兩種方法,由於我安裝了ctags,所以採用上面這種設定。

Google Go

Proper Go support is provided by the gotags project, complete with how to configure Tagbar to make use of it.

Alternatively, if you use the Go patch for exuberant-ctags or the Debian/Ubuntu package (which has it integrated), the following configuration provides simple support without scopes:

let g:tagbar_type_go = {
    \ 'ctagstype': 'go',
    \ 'kinds' : [
        \'p:package',
        \'f:function',
        \'v:variables',
        \'t:type',
        \'c:const'
    \]
\}

安裝SrcExpl外掛

在.vimrc下面加上:
Plugin ‘wesleyche/SrcExpl’
其他步驟與前面安裝外掛類似,略過。
官方網址:https://github.com/wesleyche/SrcExpl

"===============================
" SrcExpl settings
" ==============================
" // The switch of the Source Explorer 
nmap <F9> :SrcExplToggle<CR>

" // Set the height of Source Explorer window 
let g:SrcExpl_winHeight = 8

" // Set 100 ms for refreshing the Source Explorer 
let g:SrcExpl_refreshTime = 100

" // Set "Enter" key to jump into the exact definition context 
let g:SrcExpl_jumpKey = "<ENTER>"

" // Set "Space" key for back from the definition context 
let g:SrcExpl_gobackKey = "<SPACE>"

" // In order to avoid conflicts, the Source Explorer should know what plugins
" // except itself are using buffers. And you need add their buffer names into
" // below listaccording to the command ":buffers!"
let g:SrcExpl_pluginList = [
        \ "__Tag_List__",
        \ "_NERD_tree_"
        \ ]

" // Enable/Disable the local definition searching, and note that this is not 
" // guaranteed to work, the Source Explorer doesn't check the syntax for now. 
" // It only searches for a match with the keyword according to command 'gd' 
let g:SrcExpl_searchLocalDef = 1

" // Do not let the Source Explorer update the tags file when opening 
let g:SrcExpl_isUpdateTags = 0

" // Use 'Exuberant Ctags' with '--sort=foldcase -R .' or '-L cscope.files' to 
" // create/update the tags file 
let g:SrcExpl_updateTagsCmd = "ctags --sort=foldcase -R ."

" // Set "<F12>" key for updating the tags file artificially 
let g:SrcExpl_updateTagsKey = "<F12>"

" // Set "<F3>" key for displaying the previous definition in the jump list 
let g:SrcExpl_prevDefKey = "<F3>"

" // Set "<F4>" key for displaying the next definition in the jump list 
let g:SrcExpl_nextDefKey = "<F4>"

Nerdtree, tagbar, SrcExpl三視窗之間切換可以按ctrl+w+w

Show下cool圖

這裡寫圖片描述

再發一個網路上大牛的配置,只是是針對PHP, CSS,XML, JAVASCRIPT, MARKDOWN, PHYTHON之類的開發, 對C/C++和GO的支援都不是太好,另外,對GVIM有一點衝突, 片段顯示視窗等小問題,不過可以使用。
https://github.com/humiaozuzu/dot-vimrc
如果喜歡這位大牛風格的配置,可以對其稍作修改就可以支援GO的開發,具體如下:
0. 安裝go.tools Binaries,具體同上
1. 參考上面的配置,安裝YCM外掛,並在.vimrc中新增YCM相關配置。
2. 在tagbar裡新增對go的支援,注意也選用ctags這種方式的配置。
3. 把下面這段關於vim-go的配置也新增上(具體配置內容在上面的配置下載包裡也有)

281 " for vim-go
282 au FileType go nmap <leader> s <plug> (go-implements)
283 au FileType go nmap <leader> i <plug>(go-info)
284 au FileType go nmap <Leader> gd <plug> (go-doc)
285 au FileType go nmap <Leader> gv <plug> (go-doc-vertical)
286 au FileType go nmap <Leader> r <plug> (go-run)
287 au FileType go nmap <Leader> b <plug> (go-build)
288 au FileType go nmap <leader> t <plug> (go-test)
289 au FileType go nmap <Leader> c <plug> (go-coverage)
290 au FileType go nmap <Leader> ds <plug> (go-def-split)
291 au FileType go nmap <Leader> dv <plug> (go-def-vertical)
292 au FileType go nmap <leader> dt <plug> (go-def-tap)
293 au FileType go nmap <Leader> e <plug> (go-rename)
294  
295 let g:go_fmt_command = "goimports"
297 " for YCM
298 let g:ycm_key_list_select_completion = ['','']
299 let g:ycm_key_list_previous_completion = ['']
300 let g:ycm_key_invoke_completion = '<C-Space>' 

4.外掛包裡有一個外掛有點舊,可以換下。

 29 "Bundle 'edsono/vim-matchit'
 30 Bundle 'tmhedberg/matchit'   

最後說明一下,此配置裡的幾個常用快捷鍵:
F5 -> Toggle Nerd-Tree file viewer
F6 -> Toggle tagbar
Ctrl + p -> Toggle ctrlp
F3 -> Toggle Gundo viewer
F4 -> Toggle Indent Guides
F12 -> Toggle Mouse

相關推薦

支援GoLang類似 Source Insight的vim編輯搭建

,曾經配置過一個類似source insight的vim編輯器,(http://blog.csdn.net/linuxandroidwince/article/details/74202412)預設對C,C++語言是支援的,只是不支援golang,下面介紹下如果

關於Linux用戶權限文本處理工具正則表達式vim文本編輯

rtx 元字符 否則 權限 tdi 行編輯 directory e2fs 登錄 一、 用戶 ??在Linux系統中,可以創建多個用戶,每一個用戶都有一個與其對應的ID號,就像每一個人都有一個×××號一樣,這就是用戶的UID,??在Linux中管理員 root的默認UID

為了讓開發者寫MaxCompute SQL更爽DataWorks 增強SQL 編輯功能

sql摘要: 眾所周知,數據開發和分析的同學每天都要花大量時間寫MaxCompute SQL;Dataworks作為數據開發的IDE直接影響著大家的開發效率,這次新上線的Dataworks我們在編輯體驗上做了很多工作,在前端實現MaxCompute SQL和編輯器參數等擴展語法的AST解析,並實現更好更智能的

生命不息折騰不止~Vim編輯配置(2018)

學習網 https net ins 折騰 mil 找到 你們 路徑 ps:打算記錄windows下gvim和linux下vim的配置。省的每次意外都到處找博客,重新配置。 一個古老強大的編輯器。單純的喜歡在酷炫的終端下默默寫著代碼。 (用過sublime, vsc

unity 編輯 ---獲取unity編輯中的所有視窗並開啟顯示以備後面編輯擴充套件

獲取原理:unity 所有視窗介面都繼承自編輯器UnityEditor程式集下的EditorWindow。而所有的編輯器視窗都在UnityEditor程式集裡定義,所以,我們通過反射獲取UnityEditor程式集獲取所有視窗就可以了。 直接上程式碼: using System; using

你見過最牛逼的程式設計師是什麼樣的?拳打回車鍵腳踩Emacs編輯

我自己是一名大資料架構師,目前辭職在做線上教育大資料講師,每天都會直播分享免費公開課,大家可以加群參加。以及我自己整理了一套最新的大資料學習系統教程,包括Hadoop,資料探勘,資料分析。送給正在學習大資料的小夥伴!這裡是大資料學習者聚集地,歡迎初學和進階中的小夥伴!加QQ群:5849001

正則化與萬用字元便於查詢和替換批量處理使用在wordnotepad++等文字編輯

我們常常使用查詢替換的方式來處理相關資料,可是當你要批量替換隻用一些相同字元分文字時,就會顯得很笨拙。 比如:     Line 5974: DI 10.13182/NT96-A15844     Line 6078: DI 10.1109/ISIC.1996.55623

Darktable 2.6.0 釋出跨平臺 RAW 影象編輯

   開源、跨平臺的 RAW 影象編輯器 Darktable 釋出了 2.6.0 版本,更新亮點包括: 一個新的 retouch 模組,它基本上可以取代原來的 spot removal 模組,能夠單獨處理每個細節層次。 一個新的  filmic&nbs

NKeditor v5.0.2 釋出線上富文字編輯

NKeditor 5.0.2 已釋出。NKedtior 是基於 kindeditor 進行二次開發的專案,kindeditor 是一款優秀的開源線上編輯器。輕量級且功能強大,程式碼量卻不到百度的 ueditor 編輯器的一半。我們在 kindeditor 的基礎上做了很多優化,包括優化了部分樣式,以及刪除和

科室管理系統中運用百度編輯(ueditor1_4_3-utf8-jsp)出現的一系列問題!

1.首先下載ueditor1_4_3-utf8-jsp並解壓,下載地址:http://ueditor.baidu.com/website/download.html 2,在MyEclipse裡新建一個

Linux入門學習怎麼使用Vim編輯編輯儲存檔案?以及檢視瀏覽檔案的幾種方式介紹——(四)

引言:上期回顧(想要學習的童鞋可以點選看看) 上一章講述了在linux系統當中怎麼建立檔案以及對檔案的基本操作,這章主要是說檢視檔案的幾種方式,順便簡單簡單說下vim編輯器的使用。我們不管在使用什麼系

史上最優秀的最快的編輯VIM(上古神器)

給大家介紹一下非常優秀的一款編輯器vim,它誕生於視覺化介面之前,的一款非常古老的編輯神器,那會兒都是黑乎乎的命令視窗,滑鼠的都沒有 vim可以解放我們一直抓著滑鼠的手,因為vim提供了足夠多的快捷鍵,可以實現滑鼠的所有功能,而且比滑鼠更快,更靈

source insight編輯和Keil 編譯器的程式碼對齊問題的解決方法

第一步:在source insight 中設定TAB符轉換位空格:1)在options->Document options中將Expand Tabs選項選中打鉤;2)TAB符寬度設定,在TAB width 中填入期望數值,一般為4個空格,即填4。如下圖1:第二步:在Ke

spring4.0之後自定義屬性編輯的配置變化

問題:spring中注入時間日期(java.util.Date)型別的屬性的時候需要進行型別轉換,因為spring不能直接注入Date型別。之前學習spring的時候是學的spring 2.5的版本,但是今天把spring的包都換成了spring 4.2 的,發現之前的出

C-Free 5 加 TDM-GCC編輯搭建C/C++開發環境

本文所需要的安裝程式下載地址:https://download.csdn.net/download/fangfang635344731/10705407 1.安裝C-Free 5,這一步重點是安裝路徑絕對不能有空格!!!我的安裝路徑為“C:\C-Free5”。 2.安裝TDM-GCC,我用的

froala富文本編輯golang、beego脫離ueditor苦海

success Edito insert ike alert css nan ued gin 一直用百度的ueditor,可是阿裏雲這種"wo chuo"的雲上部署的beego做的服務,總是出現ueditor.all.min.js語法錯誤,清理瀏覽器緩存後又會好起來。本地調

golang開源的Gateway閘道目前支援http/https協議。

hgw 【http-reverse_proxy, http/https-gateway,hot-reload】 https://github.com/dmhao/hgw hgw是由gateway閘道器服務、manager控制服務構成的一套輕量級網關係統。目前支援http/http

Go語言環境安裝驗證go語言環境、使用文字編輯編寫一個go hello world,Go lang IDE安裝golang中新建一個go程式

1 Golang語言環境安裝包下載 https://www.golangtc.com/ 下載: go1.9.2.windows-amd64.msi 和 go1.9.2.windows-amd64.zip 2 golang語言環境安裝 本筆記使用go1.10.2.window

功能強大的PDF編輯支援轉Word、PPT、Excel等關鍵還破解了!

這款軟體的名字叫福昕高階PDF編輯器–文末有下載地址 軟體相關介紹 福昕高階PDF編輯器作為一款全能的PDF編輯器,包括PDF編輯 轉換 註釋及ORC識別等,幾乎你想要的它都有,最關鍵它還是免費的、免費

egg-ueditor基於egg的UEditor百度編輯後端實現支援圖片/檔案上傳、列表及圖片遠端抓取

egg-ueditor 基於egg的UEditor百度編輯器後端實現,支援圖片/檔案上傳、列表及圖片遠端抓取 原始碼:https://github.com/inmyjs/egg-ueditor 安裝 npm install egg-ueditor --save 使用方