centos7 Golang vim IDE

匿名 (未验证) 提交于 2019-12-03 00:29:01
接触Golang之后使用过许多的编辑器比如lite,sublime3,goland,lite更适合在windows环境使用,对于非destop版本的Linux就不太适用了,在linux下还是直接上神器vim了,为了快速开发自然要安装许多便捷的插件

首先vim的插件安装有两种形式,一直直接自己安装到~/.vim/plugin目录下 另外一种是使用插件管理器Vundle(还有其他的插件管理器)
//hello.go
package main
import "fmt"
func main() {
}
mkdir ~/.vim/bundle
~/.vimrc附录为完整的.vimrc文件
filetype off
filetype plugin indent off
set rtp+=$VIM/vimfiles/bundle/vundle/
"rc后的参数表示Vundle自动下载安装插件的位置,为空放在~/.vim/下
call vundle#rc('$VIM/vimfiles/bundle/')
Bundle 'gmarik/vundle'
filetype plugin indent on
syntax on

~/.vimrcvundle#beginvundle#end


Plugin 'fatih/vim-go', { 'do': ':GoUpdateBinaries' }

“Processing 'fatih/vim-go'”Done!”。
这时,我们可以看到.vim/bundle下多了一个vim-go文件夹:
$ ls .vim/bundle/
goimports 会自动删除导入import的包
godef 可以跳转到函数定义处
gocode 这个就是go代码自动完成的插件装了YouCompleteMe之后就不需要再点击ctrl x才出现 而是敲代码时自动出现
gotags 这个可以方便你查看源码
:GoInstallBinaries$GOBIN$GOPATH/bin
:GoInstallBinaries
vim-go: gogetdoc not found. Installing github.com/zmb3/gogetdoc to folder /root/Applications/Go/bin
^@https fetch failed: Get https://golang.org/x/tools/go/buildutil?go-get=1: dial tcp 216.239.37.1:443: i/o timeout^@package golang.org/x/
tools/go/buildutil: unrecognized import path "golang.org/x/tools/go/buildutil" (https fetch: Get https://golang.org/x/tools/go/buildutil?
go-get=1: dial tcp 216.239.37.1:443: i/o timeout)^@Fetching https://golang.org/x/tools/go/loader?go-get=1^@https fetch failed: Get https:
//golang.org/x/tools/go/loader?go-get=1: dial tcp 216.239.37.1:443: i/o timeout^@package golang.org/x/tools/go/loader: unrecognized impor
t path "golang.org/x/tools/go/loader" (https fetch: Get https://golang.org/x/tools/go/loader?go-get=1: dial tcp 216.239.37.1:443: i/o tim
eout)^@
vim-go: guru not found. Installing golang.org/x/tools/cmd/guru to folder /root/Applications/Go/bin
ng.org/x/tools/cmd/guru?go-get=1: dial tcp 216.239.37.1:443: i/o timeout^@package golang.org/x/tools/cmd/guru: unrecognized import path "
golang.org/x/tools/cmd/guru" (https fetch: Get https://golang.org/x/tools/cmd/guru?go-get=1: dial tcp 216.239.37.1:443: i/o timeout)^@
vim-go: golint not found. Installing github.com/golang/lint/golint to folder /root/Applications/Go/bin
ch failed: Get https://golang.org/x/lint?go-get=1: dial tcp 216.239.37.1:443: i/o timeout^@package golang.org/x/lint: unrecognized import
path "golang.org/x/lint" (https fetch: Get https://golang.org/x/lint?go-get=1: dial tcp 216.239.37.1:443: i/o timeout)^@
vim-go: errcheck not found. Installing github.com/kisielk/errcheck to folder /root/Applications/Go/bin
t=1^@https fetch failed: Get https://golang.org/x/tools/go/loader?go-get=1: dial tcp 216.239.37.1:443: i/o timeout^@package golang.org/x/
tools/go/loader: unrecognized import path "golang.org/x/tools/go/loader" (https fetch: Get https://golang.org/x/tools/go/loader?go-get=1:
dial tcp 216.239.37.1:443: i/o timeout)^@github.com/kisielk/gotool (download)^@
vim-go: impl not found. Installing github.com/josharian/impl to folder /root/Applications/Go/bin
ttps fetch failed: Get https://golang.org/x/tools/imports?go-get=1: dial tcp 216.239.37.1:443: i/o timeout^@package golang.org/x/tools/im
ports: unrecognized import path "golang.org/x/tools/imports" (https fetch: Get https://golang.org/x/tools/imports?go-get=1: dial tcp 216.
239.37.1:443: i/o timeout)^@
vim-go: goimports not found. Installing golang.org/x/tools/cmd/goimports to folder /root/Applications/Go/bin
#goimports
go get -u golang.org/x/tools/cmd/goimports

先在你的$GOPATH目录(如果不懂GOPATH,请先弄懂)下,创建目录src
$ mkdir src
复制github.com.nsf.gocode.tar.gzsrc目录下,解压缩,这时候目录应该看起来是这个样子:
.└―― src
└―― github.com
└―― nsf
└―― gocode
├―― LICENSE
├―― README.md
├―― _gccgo
└―― package.go
├―― _goremote
...
接下来安装这个包:
这时候目录下会生成一个pkg目录,里面有编译好的 .a 文件,就可以在 go 文件里使用这个包了。
安装后,$GOBIN下的新增Binaries如下:
[root@iz6r97y3yfzu8dz src]# ls $GOBIN -l
total 147132
-rwxr-xr-x 1 root root 15017165 Jun 15 22:48 dlv
-rwxr-xr-x 1 root root 11167455 Jun 16 23:52 gocode
-rwxr-xr-x 1 root root 15742059 Jun 16 23:51 godoc
-rwxr-xr-x 1 root root 12120307 Jun 13 08:08 gotour
hello.go
- 新起一行输入fmt.,然后ctrl+x, ctrl+o,Vim 会弹出补齐提示下拉框,不过并非实时跟随的那种补齐,这个补齐是由gocode提供的。
YCM(Your Complete Me)
Plugin 'Valloric/YouCompleteMe'
:PluginInstall
[root@iz6r97y3yfzu8dz test]# vim ~/.vimrc
UltiSnips requires py >= 2.7 or py3
YouCompleteMe unavailable: requires Vim compiled with Python (2.7.1+ or 3.4+) support.
请按 ENTER 或其它命令继续

[root@iz6r97y3yfzu8dz test]#vim --version |grep python
HanDevServer:/opt/itest # /opt/iapps/vim/bin/vim --version |grep python
+cryptv +linebreak -python +viminfo
-cscope +lispindent -python3 +vreplace

如果发现是这样的,说明没有加入Python支持,需要重新编译安装vim,加入--enable-pythoninterp=yes参数。如果想开启Python3支持,则--enable-python3interp=yes,所以最终的编译选项是:
HanDevServer:~handaoliang/src/vim74 # ./configure --prefix=/opt/iapps/vim --enable-multibyte --enable-pythoninterp=yes

#如果vim版本不符合就需要卸载旧版本,然后下载最新版本vim
git clone https://github.com/vim/vim.git
# make一遍的时候,第二遍make时会出现cached,所以第二次make的时候还会使用第一次的执行结果,所以想修改就必须清除make操作之后,两次make
cd vim
make uninstall //卸载
make clean
rm -rf src/auto/config.cache

./configure --with-features=huge \
--enable-multibyte \
--enable-rubyinterp=yes \
--enable-pythoninterp=yes \
--with-python-config-dir=/usr/lib64/python2.7/config \
--enable-perlinterp=yes \
--enable-luainterp=yes \
--enable-cscope \
--prefix=/usr/local

make && make install
sudo apt-get install build-essential cmake python-dev
cd ~/.vim/bundle/YouCompleteMe
./install.sh
YouCompleteMe

# yum install build-essential cmake python-dev
$cd ~/.vim/bundle/
$ git clone https://github.com/Valloric/YouCompleteMe.git
$ cd YouCompleteMe/
$ git submodule update --init --recursive
$ cd ~/.vim/bundle/YouCompleteMe
$ ./install.py --gocode-completer --clang-completer


Plugin 'SirVer/ultisnips'
func name(params) type {
}
" 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>"

1. Download and install Tim Pope's Pathogen.
2. Next, move or clone the vim-colors-solarized directory so that it is a subdirectory of the .vim/bundle directory.
a. Clone:

b. Move:
In the parent directory of vim-colors-solarized:

Modify .vimrc

After either Option 1 or Option 2 above, put the following two lines in your .vimrc:
syntax enable
set background=dark
colorscheme solarized
or, for the light background mode of Solarized:
syntax enable
set background=light
colorscheme solarized
I like to have a different background in GUI and terminal modes, so I can use the following if-then. However, I find vim's background autodetection to be pretty good and, at least with MacVim, I can leave this background value assignment out entirely and get the same results.
if has('gui_running')
set background=light
else
set background=dark
endif

4.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
set t_Co=256
set background=dark

5.安装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>

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!