Last Updated: March 28, 2016
·
675
· ronelliott

Override gofmt tab indentation

Overview:

gofmt now forces tab indentation, which can be annoying to those desiring space indentation. to override this behavior in a one-liner use this:

cat /path/to/my/file.go | /usr/local/bin/gofmt | expand -t4 > /path/to/my/file.go.fmt && mv /path/to/my/file.go.fmt /path/to/my/file.go

or for fish-shell:

cat /path/to/my/file.go | /usr/local/bin/gofmt | expand -t4 > /path/to/my/file.go.fmt ;and mv /path/to/my/file.go.fmt /path/to/my/file.go

Fish function:

function gofmt
    cat $argv[1] | /usr/local/bin/gofmt | expand -t4 > $argv[1].fmt ;and mv $argv[1].fmt $argv[1]
end

Notes:

  • Due to the renaming, this can only process one file at a time.
  • This will expand tabs to 4 spaces, to expand to more/less than 4 change the -t argument on the expand command.