2012-06-10 14:12
浏览 322


I'm using vim with the vim-golang plugin. This plugin comes with a function called :Fmt that "reformats" the source code using gofmt, a command-line executable.

I want to invoke the :Fmt function each time that I save the file, so it is continuously re-formatted. I think this should be done with a autocmd directive. But I have two doubts:

  1. I could not find a way to execute the function. I tried writting Fmt and :Fmt at the end of the autocmd line, but it didn't seem to work. I think I miss something, like "call".
  2. I want this to happen only when saving a file of filetype 'go'. I don't know how to combine those two conditions - I can activate variables depending on the file type, and I can do small stuff, like removing trailing spaces, whenever a file is saved, but separatedly.

So this is what I have so far:

" I can set variables for go like this
autocmd FileType go setlocal noexpandtab shiftwidth=4 tabstop=4 softtabstop=4 nolist

" I can clean trailing spaces(conserving cursor position) on save like this
autocmd BufWritePre * kz|:%s/\s\+$//e|'z

" None of these worked:
autocmd BufWritePre,FileType go Fmt
autocmd BufWritePre,FileType go :Fmt

图片转代码服务由CSDN问答提供 功能建议

我正在将vim与 vim-golang 插件。 该插件带有一个名为:Fmt 的函数,可“重新格式化” 使用 gofmt (命令行可执行文件)的源代码。

我想分别调用:Fmt函数 我保存文件的时间,因此它会不断地重新格式化。 我认为应该使用 autocmd 指令来完成。 但是我有两个疑问:

  1. 我找不到执行该功能的方法。 我尝试在autocmd行的末尾写Fmt和:Fmt,但是似乎没有用。 我想我想念诸如“通话”之类的东西。
  2. 我希望仅在保存文件类型为“ go”的文件时才会发生这种情况。 我不知道如何结合这两个条件-我可以根据文件类型激活变量,并且我可以做一些小事情,例如在保存文件时分开删除尾随空格。 < / ol>


    autocmd FileType go setlocal noexpandtab shiftwidth =  4 tabstop = 4 softtabstop = 4 nolist 
    autocmd BufWritePre * kz |:%s / \ s \ + $ // e |'z 
    autocmd BufWritePre,FileType进入Fmt 
    autocmd BufWritePre,FileType进入:Fmt 
  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 邀请回答

2条回答 默认 最新

相关推荐 更多相似问题