Jay Taylor's notes

back to listing index

Add goimports to Sublime Text 3

[web search]
Original source (www.calhoun.io)
Tags: golang go sublime-text-3 gofmt goimports www.calhoun.io
Clipped on: 2017-08-09

This should workin pretty much any version of Sublime Text, but I only tested it in Sublime Text 3.

Install goimports

First off, install goimports in your terminal:

go get golang.org/x/tools/cmd/goimports  

Install GoSublime

Next we need to install GoSublime in Sublime Text. Hit cmd + shift + p then type Install Package and select the install package option, then type GoSublime and select this package.

You may already have the package. An easy way to check is to hit cmd + shift + p and type Remove Package then look for it, and hit esc if you see it.

Set Up GoSublime

Next up we need to set up GoSublime to use goimports. To do this, you first want to open up your user preferences for GoSublime. You can do this via the menu (Sublime Text > Package Settings > GoSublime > Settings - User) or via the shortcut (cmd + . then cmd + 5).

NOTE: Make sure you use the user settings. Default settings can be overwritten with a package update, but user settings will be preserved.

Once this file is opened up, you need to add the line "fmt_cmd": [ "goimports" ] to the file. If your file has nothing else in it, the final version should look like this:

  "fmt_cmd": [ "goimports" ]

If you have something else in it, simply add this line to the file and be sure to add a comma on the line before it if you need to.

Test it out

Go open up a go file, add some unnecessary packages, and hit save (cmd + s). The imports should be cleaned up!

If you found this post helpful, sign up for my mailing list to get more articles like it delivered directly to your inbox.

Learn Web Development with Go!

Sign up for my mailing list to receive the first three chapters from my upcoming book, Web Development with Go, completely FREE.

You will also receive notifications when I release new articles, along with other freebies that I only share with my mailing list.

Email Address
Recaptcha requires verification
I'm not a robot
Check out my book, Web Development with Go!