fatih / vim-go

Go development plugin for Vim

Home Page:https://www.patreon.com/bhcleek

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Run goimports on save?

mperham opened this issue · comments

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?

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 :).