28

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

3 Answers 3

60

The FileType event doesn't fire on buffer writes; BufWritePre is the correct one, but you need to provide a file pattern, e.g. *.go:

autocmd BufWritePre *.go Fmt

The only downside is that this duplicates the detection of the go filetype. You could delegate this by hooking into the FileType event, and then define the formatting autocmd for each Go buffer by using the special <buffer> pattern:

autocmd FileType go autocmd BufWritePre <buffer> Fmt

This has the downside that if the filetype ever gets set multiple times, you'll run the formatting multiple times, too. That could be solved via a custom :augroup, but now it becomes really complex. Or, if you're really sure that this is the only BufWritePre autocmd for Go buffers, you could use :autocmd! BufWritePre ... (with a !).

Sign up to request clarification or add additional context in comments.

7 Comments

Wow, incredibly extensive answer, thanks a lot! I ended up using the second option - I don't plan to reset the filetype at all. Thanks!
You might want to add silent if you need to normal output from the Fmt command (I kept seeing X lines filtered): autocmd FileType go autocmd BufWritePre <buffer> silent Fmt
The scenario is coding in a boarding gate. What if code has an error and I do need to save the file even with an error? In my case, it did not let me save the file. Is there a way to force saving in such cases?
PS! from Go 1.4 (where misc/vim was removed), you have to install github.com/vim-jp/vim-go-extra for @IngoKarkat's instructions to work. Alternatively, browse for other options at the Go GitHub wiki.
|
2

If you use folds, gofmt messes these up (it opens closed folds, closes open ones). To keep folds as they where use the following autocommand

autocmd FileType go autocmd BufWritePre <buffer> execute "normal! mz:mkview\<esc>:Fmt\<esc>:loadview\<esc>`z"

It uses the z register to mark the cursor position because :mkview and :loadview (wich save and restores the folds) move the cursor for some reason.

Comments

2

For those not using the plugin, this should work:

autocmd FileType go autocmd BufWritePre <buffer> execute "normal! mz:mkview\<esc>:%!gofmt-safe\<esc>:loadview\<esc>`z"

Add the following script to your PATH, this is needed otherwise gofmt will nuke the file if there are any syntax errors. This can be dangerous if you type :x for instance.

gofmt-safe

#!/usr/bin/env bash

orig=$(mktemp)
fmt=$(mktemp)

cat > "$orig"

<"$orig" gofmt "$@" > "$fmt" 2>/dev/null

if [ $? -eq 0 ]; then
    cat "$fmt"
else
    cat "$orig"
fi

rm -f "$orig" "$fmt"

If you want live dangerously without this script, you can replace %!gofmt-safe with %!gofmt in the autocmd line. This has the benefit of showing you syntax errors on :w. Just make sure not to type :x or :wq.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.