无法在vim中自动格式化go(golang)代码

4

我遇到了以下命令的问题:

autocmd FileType go autocmd BufWritePre <buffer> Fmt

它应该会在我.vimrc文件的结尾自动格式化我的代码。

这是我的 .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()
" alternatively, pass a path where Vundle should install plugins
"call vundle#begin('~/some/path/here')

" let Vundle manage Vundle, required
Plugin 'gmarik/Vundle.vim'

" Keep Plugin commands between vundle#begin/end.
" plugin on GitHub repo
Plugin 'tpope/vim-fugitive'
" plugin from http://vim-scripts.org/vim/scripts.html
Plugin 'L9'
" Git plugin not hosted on GitHub
Plugin 'git://git.wincent.com/command-t.git'
" git repos on your local machine (i.e. when working on your own plugin)
Plugin 'file:///home/gmarik/path/to/plugin'
" The sparkup vim script is in a subdirectory of this repo called vim.
" Pass the path to set the runtimepath properly.
Plugin 'rstacruz/sparkup', {'rtp': 'vim/'}
" Avoid a name conflict with L9
Plugin 'user/L9', {'name': 'newL9'}
Plugin 'commentary.vim'
Plugin 'go.vim'

" All of your Plugins must be added before the following line
call vundle#end()            " required
filetype plugin indent on    " required

syntax on
filetype plugin on

filetype indent on

"autocmd FileType go compiler go
autocmd FileType go autocmd BufWritePre <buffer> Fmt

然而,vim给我抛出了一个错误信息:
Error Detected while processing BufWrite Auto commands for "" E492: Not an editor command: Fmt
我不知道出了什么问题,尤其是因为它之前是可以工作的。

@OneOfOne,那跟 $GOROOT/misc/vim 中已经包含的插件和 readme.txt 文件中的说明有什么不同呢? - Charlie Parker
还有,以下插件怎么样?https://github.com/nsf/gocode? - Charlie Parker
这些插件提供了 :Fmt 命令吗?如果有,那么就不会有问题;如果没有,那么就会有问题。(nsf/gocode 似乎没有 :Fmt 命令) - FDinoff
faith/vim-go 在保存时运行 gofmt,我99%确定默认插件不会这样做,此外它还可以内部处理 gocode。 - OneOfOne
@elithrar 是的,我有 gofmt。我刚刚对我的文件进行了格式化,输出了正确的格式。 - Charlie Parker
显示剩余2条评论
1个回答

3

你可能正在指的是由这个定义的:Fmt命令。

看起来该文件类型插件未被调用。您可以使用:scriptnames命令进行检查;它需要包含ftplugin/go/fmt.vim。如果没有出现,请检查您的'runtimepath'选项是否正确。

或者,您可以使用fatih/vim-go;它显然有一个配置可以自动格式化Go源代码,因此您不需要自己定义:autocmd


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接