Golang-Vim環境搭建-OSX

來源:互聯網
上載者:User
這是一個建立於 的文章,其中的資訊可能已經有所發展或是發生改變。

接觸Golang之後使用過許多的編輯器比如liteidea,sublime2,idea其中我最鐘愛的就是idea了後來偶然有一次使用到了Vagrant搭建虛擬開發環境於是乎不得不用vim進行開發,為了快速開發自然要安裝許多便捷的外掛程式.當然安裝這些外掛程式不得不說把我噁心到了,雖然說有一部分是我自己的原因.好吧我就先上一張大家先看下vim這個編輯器之神的模樣.



有木有相當的狂炫酷霸吊炸天呢哈哈 開始vim的外掛程式安裝吧

首先vim的外掛程式安裝有兩種形式,一直直接自己安裝到~/.vim/plugin目錄下 另外一種是使用外掛程式管理器Vundle(還有其他的外掛程式管理器)

我先介紹下要安裝的種類

1 gmarik/Vundle.vim 這個就是外掛程式管理器

2 'Valloric/YouCompleteMe' 代碼自動完成的外掛程式

3 goimports 會自動刪除匯入import的包

4 godef 可以跳轉到函數定義處

5 gocode 這個就是go代碼自動完成的外掛程式裝了YouCompleteMe之後就不需要再點擊ctrl x才出現 而是敲代碼時自動出現

6 gotags 這個可以方便你查看源碼 也就是右邊的那一部分

7 NerdTree 這個可以查看目錄結構也是很方便的


~/.vimrc是一個關於vim的設定檔(如果你沒有自己在~/目錄下mkdir ~/.vimrc檔案)我會在文章最後貼上我的.vimrc的完整配置

GOBIN目錄一定要確保正確必須在你的PATH環境變數當中如果你設定了多個GOPATH那麼你一定要確保你所設定的GOBIN目錄中有安裝外掛程式的可執行檔

vundle安裝(git的安裝自行Google)

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

在.vimrc中添加一下內容則完成了 vundle對vim的外掛程式管理部分 以後需要什麼外掛程式通過配置該檔案就可以了

filetype offfiletype plugin indent offset rtp+=$VIM/vimfiles/bundle/vundle/"rc後的參數表示Vundle自動下載安裝外掛程式的位置,為空白放在~/.vim/下call vundle#rc('$VIM/vimfiles/bundle/')Bundle 'gmarik/vundle'filetype plugin indent onsyntax on
goimports安裝
go get code.google.com/p/go.tools/cmd/goimports

go install code.google.com/p/go.tools/cmd/goimports

github上也有goimports但是上面有一段介紹This project has moved to the official go.tools repo 所以就不適用github的地址

在.vimrc中添加 

Bundle 'cespare/vim-golang' 

let g:gofmt_command = "goimports"  

autocmd BufWritePre *.go :Fmt

重新開啟vim,命令模式下執行:BunduleInstall

使用方法: 在命令模式下 輸入:Fmt  goimports會在後台被自動調用,對當前編輯的檔案排版,並在檔案中插入/刪除相應的import語句

godef安裝

go get code.google.com/p/rog-go/exp/cmd/godef
go install code.google.com/p/rog-go/exp/cmd/godef
.vimrc中添加
Bundle 'dgryski/vim-godef'
重新開啟vim,命令模式下執行:BunduleInstall

使用方法開啟一個go檔案 游標指定到相應的函數 在命令模式下輸入gd自動跳轉到函數定義的位置

YouCompleteMe與gocode的安裝安裝

YCM的安裝過程相當蛋疼一步步來吧

1 vim的版本必須是7.4以上的因為我的是osx10.9內建的vim是7.3的所以必須更新 

首先從http://www.vim.org/download.php 下載一個最新版的vim下載下來之後

如果直接make編譯一定是有問題很多問題 具體我就不一一描述了

按照下面的過程來編譯吧解壓源碼進入src目錄修改src/os_unix.c檔案

在  #if !defined(__APPLE__) && !defined(__TANDEM)  # define select select_declared_wrong  #endif前添加 #if defined(__APPLE__) # include <AvailabilityMacros.h>#endif
返回src make clean 然後 ./configure --with-features=huge --enable-pythoninterp --enable-rubyinterp 再make 

這時候make出來的vim就是可用的了否則無法用於YCM

修改~/.bash_profile檔案添加一句話

alias vim="/Applications/Developer/tools/vim74/src/vim"

source一下就可以了 vim --version查看vim版本如果是7.4則沒問題了

在.vimrc中添加 Bundle 'Valloric/YouCompleteMe' 

然後:BundleInstall

然後在~/.vim/bundle/YouCompleteMe/目錄下執行./install.sh

YCM安裝好了 接下來安裝gocode

go get github.com/nsf/gocode
go install github.com/nsf/gocode
在.vimrc裡添加:Bundle 'Blackrush/vim-gocode'

還要記得去github.com/nsf/gocode/vim/ 下面執行 ./update.sh
重新開啟vim,命令模式下執行:BunduleInstall

ctags和gotags安裝

go get github.com/jstemmer/gotags
go install github.com/jstemmer/gotags

在~/.vimrc中添加以下內容

" go tagsBundle 'majutsushi/tagbar'nmap <F8> :TagbarToggle<CR>let g:tagbar_type_go = {    \ 'ctagstype' : 'go',    \ 'kinds'     : [        \ 'p:package',        \ 'i:imports:1',        \ 'c:constants',        \ 'v:variables',        \ 't:types',        \ 'n:interfaces',        \ 'w:fields',        \ 'e:embedded',        \ 'm:methods',        \ 'r:constructor',        \ 'f:functions'    \ ],    \ 'sro' : '.',    \ 'kind2scope' : {        \ 't' : 'ctype',        \ 'n' : 'ntype'    \ },    \ 'scope2kind' : {        \ 'ctype' : 't',        \ 'ntype' : 'n'    \ },    \ 'ctagsbin'  : 'gotags',    \ 'ctagsargs' : '-sort -silent'    \ }

重新開啟vim,命令模式下執行:BunduleInstall

NerdTree安裝

直接在.vimrc加入以下內容就可以了

" NerdTree use <C-n>Bundle 'scrooloose/nerdtree'""""""""""""""""""""""""""""""" scrooloose/nerdtree setting""""""""""""""""""""""""""""""let NERDTreeWinPos='left'let NERDTreeWinSize=31let NERDTreeChDirMode=1map <C-n> :NERDTreeToggle<CR>autocmd bufenter * if (winnr("$") == 1 && exists("b:NERDTreeType") && b:NERDTreeType == "primary") | q | endif

下面貼出我的./vimrc的配置 僅供參考

" UI: colorscheme desertset rulerset numberset background=darkset t_Co=256set cursorlineset backspace=indent,eol,startset fdm=indentset completeopt=longest,menu" Golang" gofmt *.go files after savingfiletype offfiletype plugin indent offset rtp+=$GOROOT/misc/vimfiletype plugin indent onfiletype plugin onsyntax onfiletype indent on" Bundleset nocompatiblelet mapleader=","set rtp+=~/.vim/bundle/vundle/call vundle#rc()Bundle 'gmarik/vundle'Bundle 'Valloric/YouCompleteMe'Bundle 'cespare/vim-golang'Bundle 'Blackrush/vim-gocode'Bundle 'dgryski/vim-godef'let g:godef_split=2" no backupset nobackupset noswapfileset nowb" encodingset encoding=utf8" auto, smart indentset aiset si" highlight searchset hlsearch" open file in the place last modifiedautocmd BufReadPost *\ if line("'\"")>0&&line("'\"")<=line("$") |\exe "normal g'\"" |\ endif" go tagsBundle 'majutsushi/tagbar'nmap <F8> :TagbarToggle<CR>let g:tagbar_type_go = {    \ 'ctagstype' : 'go',    \ 'kinds'     : [        \ 'p:package',        \ 'i:imports:1',        \ 'c:constants',        \ 'v:variables',        \ 't:types',        \ 'n:interfaces',        \ 'w:fields',        \ 'e:embedded',        \ 'm:methods',        \ 'r:constructor',        \ 'f:functions'    \ ],    \ 'sro' : '.',    \ 'kind2scope' : {        \ 't' : 'ctype',        \ 'n' : 'ntype'    \ },    \ 'scope2kind' : {        \ 'ctype' : 't',        \ 'ntype' : 'n'    \ },    \ 'ctagsbin'  : 'gotags',    \ 'ctagsargs' : '-sort -silent'    \ }autocmd BufWritePre *.go :Fmtlet g:gofmt_command = "goimports"" NerdTree use <C-n>Bundle 'scrooloose/nerdtree'""""""""""""""""""""""""""""""" scrooloose/nerdtree setting""""""""""""""""""""""""""""""let NERDTreeWinPos='left'let NERDTreeWinSize=31let NERDTreeChDirMode=1map <C-n> :NERDTreeToggle<CR>autocmd bufenter * if (winnr("$") == 1 && exists("b:NERDTreeType") &&b:NERDTreeType == "primary") | q | endif


相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.