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

huangapple go评论79阅读模式
英文:

Cannot auto format go (golang) code in vim

问题

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

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 "<buffer=1>"

E492: Not an editor command: Fmt

我不知道出了什么问题,特别是因为之前它是可以工作的。

英文:

I was having a problem with the following command:

autocmd FileType go autocmd BufWritePre &lt;buffer&gt; Fmt

its suppose to format my code code automatically by putting it at the end of my .vimrc file.

This is how my .vimrc file lookgs like:

set nocompatible              &quot; be iMproved, required
filetype off                  &quot; required

&quot; set the runtime path to include Vundle and initialize
set rtp+=~/.vim/bundle/Vundle.vim
call vundle#begin()
&quot; alternatively, pass a path where Vundle should install plugins
&quot;call vundle#begin(&#39;~/some/path/here&#39;)

&quot; let Vundle manage Vundle, required
Plugin &#39;gmarik/Vundle.vim&#39;

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

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

syntax on
filetype plugin on

filetype indent on

&quot;autocmd FileType go compiler go
autocmd FileType go autocmd BufWritePre &lt;buffer&gt; Fmt

However, vim does throw me an error saying:

Error Detected while processing BufWrite Auto commands for &quot;&lt;buffer=1&gt;&quot;

E492: Not an editor command: Fmt

I have no idea whats wrong, specially because it was working earlier.

答案1

得分: 3

您可能在提到由此定义的:Fmt命令,它是由this提供的。

看起来这个文件类型插件没有被加载。您可以使用:scriptnames命令进行检查;它应该包含ftplugin/go/fmt.vim。如果没有包含,那么可能是您的'runtimepath'选项有问题。

另外,您可以使用fatih/vim-go插件;它似乎有一个自动格式化Go源代码的配置,因此您不需要自己定义:autocmd命令。

英文:

You're probably referring to the :Fmt command defined by this.

It looks like that filetype plugin isn't sourced. You can check with the :scriptnames command; it needs to contain ftplugin/go/fmt.vim. If it isn't, something's wrong with your &#39;runtimepath&#39; option.

Alternatively, you could use fatih/vim-go; it apparently has a configuration that automatically formats the Go source code, so you don't need to define that :autocmd on your own.

huangapple
  • 本文由 发表于 2014年7月23日 04:57:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/24897703.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定