Vim-go: Run goimports on save?

Created on 15 Oct 2014  路  7Comments  路  Source: fatih/vim-go

I love running gofmt on every save but it complains about unused imports all the time. Can you run goimports also to clean up unused imports?

Most helpful comment

This is not anymore default, but you can easily enable it by adding this to your .vimrc:

let g:go_fmt_command = "goimports"

I might change this again in the future because a lot of people is asking for this :) However goimports is slow on large code bases which is against my intention which is providing a fast and simple go editing experience in Go.

All 7 comments

This is not anymore default, but you can easily enable it by adding this to your .vimrc:

let g:go_fmt_command = "goimports"

I might change this again in the future because a lot of people is asking for this :) However goimports is slow on large code bases which is against my intention which is providing a fast and simple go editing experience in Go.

The problem with your suggestion is that my code is no longer formatted. I don't want to run one or the other, I want to run them both.

No, goimports is doing formatting too! Did you try it? Because goimports is basically gofmt + automatic importing.

I did try it. It sorted my imports differently that gofmt so I thought maybe it wasn't formatting. I'll try it for the next day and see if I like it better than gofmt. Specifically it seems to put external packages below stdlib packages.

With goimports:

import (
    "errors"
    "strconv"
    "strings"

    "github.com/mperham/inspeqtor/util"
)

With gofmt:

import (
    "errors"
    "github.com/mperham/inspeqtor/util"
    "strconv"
    "strings"
)

Yeah, this is the only difference. This come up a lot in golang-nuts, here is one of the recent discussion:

https://groups.google.com/forum/#!topic/golang-nuts/Ir0-hKNkEkQ

That's ridiculously stupid.

I like this behavior. Please keep it :).

Was this page helpful?
0 / 5 - 0 ratings

Related issues

andrejvanderzee picture andrejvanderzee  路  3Comments

derekchiang picture derekchiang  路  3Comments

joeblubaugh picture joeblubaugh  路  3Comments

danielmanesku picture danielmanesku  路  4Comments

mnarrell picture mnarrell  路  3Comments