Hi,
It is possible to pass options to the formatter like so:
gofmt -tabs=false -tabwidth=4 main.go
Via the go-vim setting:
let g:go_fmt_command = "gofmt -tabs=false -tabwidth=4"
However now I'm wondering how would it be possible to pass these settings when using goimports?
I'm trying to get my code formatted with spaces instead of tabs, 4 spaces in width.
Well just pass it to goimports binary path!
let g:go_goimports_bin = "goimports -tabs=false -tabwidth=4"
can you try it?
I have 2 issues with that approach.
1.) It doesn't work. I get this on save.
2.) It prompts me with goimports installation on every vim start, even though its already installed.
% vim main.go
Installing code.google.com/p/go.tools/cmd/goimports
Press ENTER or type command to continue
My suggestion was based on that you had a goimports installed somewhere else. We need to seperate binary path and the options. I've opened a new issue where we are gonna introduce a new parameter. Let us talk from there now. Thanks for reporting.
I see the commit 13a71b0785371b1114aff945736dffc7b8630678 adds new option, but I'm wondering how can that option be used to achieve go-vim usage of goimports combined with custom formatting settings (-tabs=false -tabwidth=4)?
That's up to the go fmt command, if you use gofmt just specifiy in your vimrc:
let g:go_fmt_options = "-tabs=false -tabwidth=4"
@fatih Well, naturally that's the first thing I tried before posting here, and it didn't work for me.
Is there any additional setting that I'm missing?
I went ahead, and deleted every occurrence of goimports from my system, and had the go-vim plugin re-install it.
sudo find / -name 'goimports'
The behavior is pretty much the same as with the one described 9 days ago. When I hit :w, I get some echo of code on bottom, while in buffer the code is nor formatted, nor do imports run. link
Here is my current vimrc
goimports doesn't have those options. Only gofmt has it. You need to change it to g:go_fmt_command = 'gofmt'.
Well than, that means if I want to do goimports there is no way for me to format my code with proper spacing (other than running :%retab on BufWritePost)?
Yes, unfortunately this is a limitation of goimports rather than vim-go. You can just type goimports in the shell and see what it supports.
I was aware of this limitation, I thought that you would do some magic with new update (eg. run goimports first, and than gofmt over it).
The goimports tool is really stupid in this sense, they should merge it with gofmt and provide it as a flag.
Most helpful comment
That's up to the go fmt command, if you use
gofmtjust specifiy in your vimrc: