99

I use IntelliJ with the Golang plugin. Is it possible to configure the IDE to execute gofmt on file save?

David Miguel
  • 12,154
  • 3
  • 66
  • 68
Myles McDonnell
  • 12,943
  • 17
  • 66
  • 116
  • 3
    I'd recommend using https://godoc.org/golang.org/x/tools/cmd/goimports instead of the standard gofmt. Goimports not only runs gofmt, but it also fixes your imports. – David Brophy Nov 21 '15 at 14:03

5 Answers5

149

Of course you can.

  1. install File Watchers plugin
  2. open "Preferences->Tools->File Watchers" & Add a new watcher
  3. set File Type: Go, Program: the abosolute path of gofmt, Arguments: -w $FilePath$
  4. click OK

enter image description here

yee
  • 1,975
  • 1
  • 15
  • 14
  • 6
    This always reformats every file in my repo, even Godeps/*. So I hardcoded my path instead. Any way to use $FilePath$ without recursively formatting every file in the repo every time? Seems like FilePath is having a domino effect for me . – jayunit100 Nov 28 '15 at 15:10
  • 1
    @jayunit100 You can configure the *Scope* param 1. add a **local scope** 2. name it & set **Pattern** to: file[$proj]:*/&&!file[$proj]:Godeps//* ![snapshot](http://i.imgur.com/OLxLu55.png?1) – yee Nov 30 '15 at 04:04
  • 1
    This doesnt allow me to edit my files. When i try to add a new line to code some more the program brings me back to the previous line and deletes my new line. how do i stop that? note: using phpstorm – bia.migueis Dec 08 '15 at 23:09
  • 6
    never mind, i figured it out. "immediate file sync" will make it so fmt runs as soon as you make any changes. just unselect it and it will only run on file save (or window unfocus) – bia.migueis Dec 08 '15 at 23:30
  • @bia.migueis Yeah, you're right. Immediate file sync must be omitted to fmt only on save. – Zippo Jan 19 '16 at 14:45
  • 3
    Heads up, this answer is somewhat out of date now: the File Watchers plugin now has a pre-built "go fmt" watcher you can add without needing to do much work at all. – Aaron Nov 30 '18 at 18:05
62

Gogland EAP 16 deprecated the On Save actions and replaced them with the File Watchers plugin.

If you have previously configured On Save actions, the IDE will prompt you to install the plugin and configure everything automatically. If you want to set it up from scratch yourself, check if you have File Watchers plugin and if not install it.

Enable gofmt:

  1. Go to Settings | Tools | File Watchers.
  2. Click the + button and select go fmt.
  3. The default values are good.
  4. In Advance Options select:
    • Auto-save edited files to trigger the watcher
    • Trigger the watcher on external changes
  5. Click ok!

Config

You can do the same for goimports and gometalinter.

David Miguel
  • 12,154
  • 3
  • 66
  • 68
  • 9
    Actually its better to setup only goimports instead of go fmt because it also performs fmt, as specified in the goimports docs: "in addition to fixing imports, goimports also formats your code in the same style as gofmt so it can be used as a replacement for your editor's gofmt-on-save hook" – dux2 May 23 '18 at 09:27
  • 2
    This is the correct answer for Goland as of Oct 2018 – George Aristy Oct 12 '18 at 15:14
54

If you are using Gogland, you have an option On Save under Preferences

enter image description here

Arun Gopalpuri
  • 2,340
  • 26
  • 27
  • 1
    gogland isn't the same as the go plugin. – spotter Mar 01 '17 at 02:00
  • It's almost the same just like python plugin vs pycharm. The only possible difference is lag on updates. Other then this - it provides you with exactly the same features set. – Alex Pavlenko Apr 28 '17 at 05:37
  • 27
    On Save mechanism is deprecated and will be removed in future. Use File Watchers plugin instead – Zangetsu Nov 06 '17 at 18:41
  • 3
    But even in latest GoLand, I do not see this option. I though when right click on filename, under "Go", I can fmt file. but I do not see the option to enable it during autosave. – curiousengineer Aug 02 '18 at 15:38
3

September 2021 Update.

If you are using GoLand with the 2021.2 version and higher (July 2021), there are a few options to enable gofmt on save besides File Watchers.

  • Enable Run gofmt on code reformat under Preferences/Settings | Editor | Code Style | Go | Other.Run gofmt on code reformat option location
  • Enable Reformat code under Preferences/Settings | Tools | Actions on Save.enter image description here
  • Press Command + S on macOS or Ctrl + S on Windows/Linux to save your changes.
s0xzwasd
  • 2,895
  • 3
  • 17
  • 26
  • 1
    The "Action on Save" menu item is part of "File Watchers" plugin (https://plugins.jetbrains.com/plugin/7177-file-watchers). Installing the plugin into IDEA Ultimate helped a lot with running go-fmt / go-imports automatically. – ya_pulser Oct 26 '21 at 16:27
2

Updated:

For new versions of the Goland:

  1. open File->Settings->Tools->File Watchers
  2. click on + button and choose go fmt
  3. press Ok

Done :)

Amin Shojaei
  • 5,451
  • 2
  • 38
  • 46