4

In Sublime Text 3, with plugins GoSublime and GoImports installed.

I'm having trouble having goimports run on my file automatically everytime I save.

Here's what I tried :

My GoSublime settings are set to :

{
    "env": {"GOPATH": "/home/nicolas/.go", "PATH": "$GOPATH/bin:$PATH" },
    //"fmt_cmd": ["goimports"]
}

When I save with these settings, the code gets formatted but imports are not added. Nothing surprising so far.

If I hit ctrl+shift+P then GoImports, imports get added as expected.

Now my problem is, when I uncomment that previous line to have these settings :

{
    "env": {"GOPATH": "/home/nicolas/.go", "PATH": "$GOPATH/bin:$PATH" },
    "fmt_cmd": ["goimports"]
}

Not only does it not add the imports, but no other formatting gets done anymore.

Any idea where I went wrong ?

I'm using Linux (Ubuntu).

John S Perayil
  • 6,001
  • 1
  • 32
  • 47
Nicolas Marshall
  • 4,186
  • 9
  • 36
  • 54
  • `goimports` probably isn't on your `$PATH`, or isn't executable. – joshlf Apr 21 '16 at 22:22
  • Can you verify that you have done everything listed in the [following link ?](http://michaelwhatcott.com/gosublime-goimports/) – John S Perayil Apr 22 '16 at 11:07
  • 1
    That was it. The thing is, I had installed GoImports through sublime using `Package control > Install plugin > GoImports`, and not through the `$ go get ... ` command. Which of course did put it in the sublime packages folder, not in $GOPATH/bin, which is on my $PATH. Gonna self-answer my question right away. – Nicolas Marshall Apr 22 '16 at 11:23

3 Answers3

10
  1. Make sure $GOPATH/bin is in your $PATH (Windows: %GOPATH%\bin goes in your %PATH%).
  2. Run go get -u golang.org/x/tools/cmd/goimports (you may have to install mercurial).
  3. Install Sublime Text and GoSublime (or make sure you’ve got the latest update if it’s already installed).
  4. Open the GoSublime user config/preference file (Mac: ⌘. ⌘5 Windows: Ctrl+. Ctrl+5). Make sure you keep the command button down for the whole shortcut sequence.
  5. Make it look like this: {"fmt_cmd": ["goimports"]}

Source

Shubham Chadokar
  • 2,520
  • 1
  • 24
  • 45
John S Perayil
  • 6,001
  • 1
  • 32
  • 47
3

So, where I went wrong :

I installed GoImports from inside Sublime using Package control > Install package > GoImports. That installed it in the Sublime Packages folder, which was not on my $PATH

What I should have done instead :

Install it using the command go get -u golang.org/x/tools/cmd/goimports. This will install it in your $GOPATH (and $GOPATH/bin should be on your $PATH).

Shubham Chadokar
  • 2,520
  • 1
  • 24
  • 45
Nicolas Marshall
  • 4,186
  • 9
  • 36
  • 54
0

ctrl+. , ctrl+x open margo.go file. Then uncomment one of golang.GoFmt & golang.GoImports. restart sublime text and try again.

// golang.GoFmt,
// or
// golang.GoImports,

I using ubuntu + sublime text 3207 + Go-1.10 + Gosublime + Goimports-release-1.10. And it works for after I uncomment golang.GoImports.

Y.W
  • 1