45

vim golang开发环境搭建

 5 years ago
source link: https://studygolang.com/articles/17817?amp%3Butm_medium=referral
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

vim是非常强大的编辑器,最近在写go的时候,想尝试下在vim中进行开发,在网上找了不少教程之后,还是遇到了不少的问题,就来说一下我的安装过程.

  • 安装vim包管理器
$ git clone https://github.com/VundleVim/Vundle.vim.git ~/.vim/bundle/Vundle.vim
  • 安装vim-go
$ git clone https://github.com/fatih/vim-go.git ~/.vim/bundle/vim-go
  • 安装插件

    将以下内容复制到 ~/.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 'Valloric/YouCompleteme'
" All of your Plugins must be added before the following line  call vundle#end() " required  filetype plugin indent on " required

在vim命令模式下输入PluginInstall

:PluginInstall
  • 安装vim-go下go的一些工具

    在vim命令模式下输入GoInstallBinaries安装一些vim-go插件所需要的一些工具,这些工具会被放到$GOBIN目录下

:GoInstallBinaries

安装好后,执行vim打开vim,如果没有任何提示错误的信息,那么恭喜,vim下go的开发环境就已经搭建好了.

可惜有时候并不是那么一帆风顺的,下面就讲讲我所遇到的问题及解决办法

排错

  • YCM提示vim版本过低
#卸载老版本vim
$ sudo apt remove vim-tiny vim-common vim-gui-common vim-nox
#安装新版本
$ git clone https://github.com/vim/vim.git
$ cd vim
$ ./configure --with-features=huge \
            --enable-multibyte \
        --enable-rubyinterp=yes \
        --enable-pythoninterp=yes \
        --with-python-config-dir=/usr/lib/python2.7/config \
        --enable-python3interp=yes \
        --with-python3-config-dir=/usr/lib/python3.5/config \
        --enable-perlinterp=yes \
        --enable-luainterp=yes \
            --enable-gui=gtk2 \
            --enable-cscope \
       --prefix=/usr/local

make VIMRUNTIMEDIR=/usr/local/share/vim/vim81

with-python-config-dir为/usr/lib/python*/下以config开头的目录,一般还有一些系统相关的信息.Ubuntu下with-python-config-dir 或者with-python3-config-dir只需要配置一个就可以了,如果两个都配置,可能会导致失败的.

然后运行vim时,提示错误信息为:

YouCompleteMe unavailable: requires Vim compiled with Python (2.7.1+ or 3.4+) support.
$ vim --version | grep python
+comments          +libcall           +python/dyn        +visualextra
+conceal           +linebreak         +python3/dyn       +viminfo

$ python --version
Python 2.7.12

明明现在已经有python的依赖的,而且版本也是对的,最终检查了以后,发现这个vim是我从github上下载的源码压缩包解压出来的,然后用git拉取源码后重新编译安装了以后,这个问题就解决了.

YCM server shutdown

重新安装了以后,运行vim提示的信息为:

The ycmd server SHUT DOWN (restart with :YcmRestartServer)

然后按照提示重启也没什么用,运行

:YcmToggleLogs ycmd_51731_stderr_8r5f09tg.log

发现并没有任何的日志信息.然后确定了一下YCM也是最新的代码,最后通过网上查找资料,在YCM的主目录下运行

$ git submodule update --init --recursive

等待一些第三方的包下载完成后,需要重新安装YCM,这个时候需要cmake,如果没有需要先安装,然后运行install.py 或者 install.sh脚本完成安装

#如果没有cmake才需要执行下面第一个命令
$ sudo apt install cmake
$ ./install.sh

等到安装完成之后,重新打开vim就没有任何的错误信息了,至此,vim下的go开发环境就已经搭建好了,好好享受吧~~.

其实遇到问题并不可怕,可怕的是没有任何错误信息,只要就错误信息,我们通过查看错误信息就可以解决了.


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK