Linux 下 golang 的 vim 環(huán)境配置

鏈接:blog.csdn.net/pei2215015/article/details/79802351
轉(zhuǎn)載:Go開發(fā)大全
vim 做 golang 開發(fā)環(huán)境,多么炫酷!還沒嘗試過用 vim 做開發(fā)環(huán)境的同學(xué)可以跟著這篇文檔把環(huán)境搭建起來了!

準(zhǔn)備條件,是已經(jīng)安裝好 golang 環(huán)境,還需要把 golang 的環(huán)境變量配置好,這里配置如下
[root@localhost?bundle]#?vim?/etc/profile
在這個(gè)文件最后加上,GOPATH、GOBIN、GOROOT、PATH?的環(huán)境變量
export?GOPATH="/root/go"
export?GOBIN="$GOPATH/bin"
export?GOROOT="/usr/local/go"
export?PATH="$PATH:/usr/local/go/bin"
export?PATH="$PATH:/root/go/bin"
[root@localhost?bundle]#?source?/etc/profile
安裝 git
[root@localhost?~]#?yum?install?-y?git
安裝 Vundle.vim
mkdir?~/.vim/bundle
git?clone?https://github.com/gmarik/Vundle.vim.git?~/.vim/bundle/Vundle.vim
安裝 vim-go
git?clone?https://github.com/fatih/vim-go.git?~/.vim/bundle/vim-go
修改~/.vimrc,如果沒有就創(chuàng)建。vimrc 文件
[root@localhost?bundle]#?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()
"?let?Vundle?manage?Vundle,?required
Plugin?'gmarik/Vundle.vim'
Plugin?'fatih/vim-go'
"?All?of?your?Plugins?must?be?added?before?the?following?line
call?vundle#end()????????????"?required
filetype?plugin?indent?on????"?required
let?g:go_version_warning?=?0
如果覺得高亮的地方太少,可在~/.vimrc?下多添加如下配置
let?g:go_highlight_functions?=?1
let?g:go_highlight_methods?=?1
let?g:go_highlight_structs?=?1
let?g:go_highlight_operators?=?1
let?g:go_highlight_build_constraints?=?1
創(chuàng)建一個(gè)測(cè)試文件 test.go,將發(fā)現(xiàn)可以高亮了
[root@localhost?bundle]#?vim?./test.go
下載 nerdtree 和 nerdtree-git-plugin
[root@localhost?bundle]#?git?clone?https://github.com/scrooloose/nerdtree.git?~/.vim/bundle/nerdtree
[root@localhost?bundle]#?git?clone?https://github.com/Xuyuanp/nerdtree-git-plugin.git?~/.vim/bundle/nerdtree-git-plugin
修改~/.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()
"?let?Vundle?manage?Vundle,?required
Plugin?'gmarik/Vundle.vim'
Plugin?'fatih/vim-go'
Plugin?'git://github.com/scrooloose/nerdtree.git'
Plugin?'git://github.com/Xuyuanp/nerdtree-git-plugin.git'
"?All?of?your?Plugins?must?be?added?before?the?following?line
call?vundle#end()????????????"?required
filetype?plugin?indent?on????"?required
let?g:go_version_warning?=?0
"?NERDTree?config
map???:NERDTreeToggle????"快捷鍵?F7,可以收放目錄樹的視圖
autocmd?bufenter?*?if?(winnr("$")?==?1?&&?exists("b:NERDTreeType")?&&b:NERDTreeType?==?"primary")?|?q?|?endif
autocmd?vimenter?*?NERDTree
nerdtree 的使用,這里不講解,大家可使用?命令來自己了解,如下
[root@localhost?bundle]#?vim?~/.vimrc
注意:使用 nerdtree 的命令,需要光標(biāo)在目錄樹的窗口下才可以,如上圖,此時(shí)直接敲?即可
安裝 neocomplete
這個(gè)智能的提示功能需要很多限制條件,想要安裝成功請(qǐng)先閱讀這個(gè)網(wǎng)址:
https://blog.csdn.net/pei2215015/article/details/79813522
根據(jù)官網(wǎng) https://github.com/shougo/neosnippet.vim 的提示安裝,修改~/.vimrc
[root@localhost?bundle]#?vim?~/.vimrc
在如上位置添加 3 個(gè)插件 Plugin
在~/.vimrc 文件的最后添加
let?g:neocomplete#enable_at_startup?=?1
:wq?保存退出
打開 vim,執(zhí)行:PluginInstall
[root@localhost?.vim]#?vim
:PluginInstall
出現(xiàn) Done 即安裝成功
安裝 ctags、gotags 和 tarbar
[root@localhost?bundle]#?yum?install?-y?ctags
如上面第 9 個(gè)步驟一樣,在~/.vimrc 中添加插件
Plugin?'jstemmer/gotags'
Plugin?'majutsushi/tagbar'
保存之后,打開 vim,執(zhí)行:PluginInstall,如下
[root@localhost?.vim]#?vim
:PluginInstall
出現(xiàn) Done 即安裝成功
在~/.vimrc文件最后加入以下內(nèi)容:
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'
\?}
安裝 mark.vim
地址:https://www.vim.org/scripts/script.php?script_id=2666,我這里演示中下載的是
直接使用 vim 打開
[root@localhost?bundle]#?vim?./mark-3.0.0.vmb.gz?
在 vim 的命令模式下輸入
:so?%
即安裝成功
在~/.vimrc 中添加
nmap?ml?MarkSet?#高亮或反高亮一個(gè)單詞??
nmap?md?MarkClear
nmap?mn?MarkSearchAnyNext?#跳轉(zhuǎn)到任一下一個(gè)高亮單詞??
nmap?mp?MarkSearchAnyPrev
nmap?mf?MarkSearchCurrentNext?#跳轉(zhuǎn)到當(dāng)前高亮的下一個(gè)單詞??
nmap?mb?MarkSearchCurrentPrev
安裝 goimports
[root@localhost?src]#?cd?/root/go/src
[root@localhost?src]#?mkdir?golang.org
[root@localhost?golang.org]#?mkdir?x
[root@localhost?golang.org]#?cd?./x
[root@localhost?x]#?git?clone?https://github.com/golang/tools.git
[root@localhost?x]#?cd?./tools/cmd/goimports/
[root@localhost?goimports]#?go?install
使用方法:在 vim 的命令模式下輸入:GoImports
注意:在 tools/cmd 下還有很多命令工具,可以使用同樣的方法安裝
安裝 golint,對(duì) go 代碼進(jìn)行 lint 檢查
[root@localhost?golang.org]#?cd?/root/go/src/golang.org/x/
[root@localhost?x]#?git?clone?https://github.com/golang/lint
[root@localhost?x]#?cd?./lint/
[root@localhost?golint]#?go?install
使用方法:在 vim 的命令模式下輸入:GoLint
讓 vim 支持 gocode
[root@localhost?vim]#?cd?/root/go/src/github.com/gocode/vim
[root@localhost?vim]#?./update.sh
如果還需要安裝其他的第三方庫(kù),請(qǐng)按照下面地址去學(xué)習(xí)
https://www.golangtc.com/download/package
這里就不一一演示了,所有操作和上面的都是一樣的了
最后的~/.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()?????????????????????????????????????????????????????????????????????????????????????????????
????????????????????????????????????????????????????????????????????????????????????????????????????????????????
"?let?Vundle?manage?Vundle,?required????????????????????????????????????????????????????????????????????????????
Plugin?'gmarik/Vundle.vim'??????????????????????????????????????????????????????????????????????????????????????
Plugin?'fatih/vim-go'???????????????????????????????????????????????????????????????????????????????????????????
Plugin?'git://github.com/scrooloose/nerdtree.git'???????????????????????????????????????????????????????????????
Plugin?'git://github.com/Xuyuanp/nerdtree-git-plugin.git'???????????????????????????????????????????????????????
Plugin?'Shougo/neocomplete'?????????????????????????????????????????????????????????????????????????????????????
Plugin?'Shougo/neosnippet'??????????????????????????????????????????????????????????????????????????????????????
Plugin?'Shougo/neosnippet-snippets'?????????????????????????????????????????????????????????????????????????????
Plugin?'jstemmer/gotags'????????????????????????????????????????????????????????????????????????????????????????
Plugin?'majutsushi/tagbar'??????????????????????????????????????????????????????????????????????????????????????
?????????????????????????????????????????????????????????????????????????????????????????????????????????????????
"?All?of?your?Plugins?must?be?added?before?the?following?line???????????????????????????????????????????????????
call?vundle#end()????????????"?required?????????????????????????????????????????????????????????????????????????
???????????????????????????????????????????????????????????????????????????????????????????????????????????????
filetype?plugin?indent?on????"?required
let?g:go_version_warning?=?0
let?g:go_highlight_functions?=?1
let?g:go_highlight_methods?=?1
let?g:neocomplete#enable_at_startup?=?1
"?NERDTree?config
map???:NERDTreeToggle
autocmd?bufenter?*?if?(winnr("$")?==?1?&&?exists("b:NERDTreeType")?&&?b:NERDTreeType?==?"primary")?|?q?|?endif
autocmd?vimenter?*?NERDTree
"?mark?config
nmap?ml?MarkSet
nmap?md?MarkClear
nmap?mn?MarkSearchAnyNex
nmap?mp?MarkSearchAnyPrev
nmap?mf?MarkSearchCurrentNext
nmap?mb?MarkSearchCurrentPrev
nmap??:TagbarToggle
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'
\?}
有時(shí)間再補(bǔ)充下各個(gè)插件是怎么使用的吧。
安裝 ack,這里是新補(bǔ)充的
[root@localhost?kingshard]#?yum?install?-y?ack
[root@localhost?bundle]#?git?clone?https://github.com/mileszs/ack.vim.git?~/.vim/bundle/ack
在~/.vimrc中添加插件位置添加 Ack 和在文件最后添加快捷鍵 F4
????Plugin?'mileszs/ack'
????map??:Ack
光標(biāo)停留在某函數(shù)或變量名上,按 F4 回車,即可全局搜索調(diào)用處
總結(jié):可能會(huì)經(jīng)常使用的快捷鍵
打開目錄窗口:F7
打開符號(hào)窗口:F8
各窗口間的相互跳轉(zhuǎn):ctr+w+w
跳轉(zhuǎn)到函數(shù)定義處:gd
向前:tab 鍵
向后:ctr+o
高亮光標(biāo)所在的單詞:ml
取消光標(biāo)所在單詞的高亮:md
下一個(gè)高亮的單詞:mf
上一個(gè)高亮的單詞:mp
推薦閱讀:
“臉書” 回應(yīng)全網(wǎng) 宕機(jī) 7小時(shí):誤發(fā)一條指令!網(wǎng)友:這程序員不得祭天?
5T技術(shù)資源大放送!包括但不限于:C/C++,Linux,Python,Java,PHP,人工智能,單片機(jī),樹莓派,等等。在公眾號(hào)內(nèi)回復(fù)「1024」,即可免費(fèi)獲取!!

