douxian9060 2016-08-05 06:54
浏览 152

Golang:使用vim(macvim)和vim-go在保存时运行`go build`

I am recently moving from sublime 3 go to mvim (vim on the mac os) and am trying to get my Golang development environment to be as similar on vim to my sublime implementation as possible.

Within my sublime setup, it runs go build anytime I save the a Go file. This provides me instant feedback on if I have unused vars or other info go build provides.

I'm attempting to move to vim, and am wondering if I can have this functionality implemented there as well. I am using vim-go but have not found a setting to implement this.

In short I want to run :GoBuild upon the saving of a Go file while using vim / vim-go. Is this Possible and how do I do so?

  • 写回答

2条回答 默认 最新

  • dtdvlazd56637 2016-08-05 07:12
    关注

    yes, use vim autocommand to run :GoBuild:

    You can specify commands to be executed automatically when reading or writing a file, when entering or leaving a buffer or window, and when exiting Vim. The usual place to put autocommands is in your .vimrc or .exrc file.

    Run the following command:

    :autocmd BufWritePre *.go :GoBuild

    Now each time you save your Go file with :w it will run :GoBuild too.
    The event type is BufWritePre, which means the event will be checked just before you write *.go file.

    BufWritePre starting to write the whole buffer to a file
    BufWritePost after writing the whole buffer to a file

    and:

    When your .vimrc file is sourced twice, the autocommands will appear twice. To avoid this, put this command in your .vimrc file, before defining autocommands:

    :autocmd! " Remove ALL autocommands for the current group.
    

    If you don't want to remove all autocommands, you can instead use a variable to ensure that Vim includes the autocommands only once:

    :if !exists("autocommands_loaded")    
    :  let autocommands_loaded = 1    
    : au ...  
    :endif
    

    like this (put this at the end of your vim startup file):

    :if !exists("autocommands_loaded")
    :  let autocommands_loaded = 1
    :  autocmd BufWritePost *.go :GoBuild
    :endif
    

    ref:
    http://vimdoc.sourceforge.net/htmldoc/autocmd.html
    http://learnvimscriptthehardway.stevelosh.com/chapters/12.html

    评论

报告相同问题?

悬赏问题

  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100