indentation in go-vim

4,221 views
Skip to first unread message

Reinhard

unread,
Dec 23, 2011, 3:46:38 PM12/23/11
to golang-nuts
The vim plugins in Go work fine, but I personally prefer 4 spaces
indent per level instead of a tab every 8th column. Of course, I could
configure vim with ":set ts=4" but problems occur e.g. when copy-
pasting part of the listing to
some text processor.

It is quite easy to change the default:

- in vim/ftplugin/go/fmt.vim, line 20: change "%!gofmt" to "%!gofmt -
tabindent=false -tabwidth=4"
- in vim/syntax/go.vim, line 32: insert "set sw=4"

That´s all; it would be nice to make it configurable in one central
place - what are your opinions?

There is one problem left I could not solve. The "classic" C comment /
* ... */ over several lines are only indented and completed right for
the cindent option which is switched off deliberately. I am afraid
that a solution in vim script language would not be so easy (the
feature is hardcoded in vim), but I haven´t tried it yet. Would it not
be nice to
have this feature in gofmt? (Though writing with automatic indent of
"*" and inserting a space after is more
comfortable).

Reinhard

Aram Hăvărneanu

unread,
Dec 23, 2011, 4:00:44 PM12/23/11
to Reinhard, golang-nuts
Reinhard wrote:
> It is quite easy to change the default:

Please don't. You are free to use in your editor whatever settings you desire,
but please don't change gofmt default behavior. In fact, I'm not sure why
gofmt even has configurable options. Its point is to fix formatting
once and for all.

--
Aram Hăvărneanu

Reinhard

unread,
Dec 23, 2011, 4:21:17 PM12/23/11
to golang-nuts
> Please don't. You are free to use in your editor whatever settings you desire,
> but please don't change gofmt default behavior. In fact, I'm not sure why
> gofmt even has configurable options. Its point is to fix formatting
> once and for all.

Of course. This was not my intention. I asked to make it configurable
in the vim plugin. And as far as I see, gofmt treats this kind of
comment but incomplete:
/*
*aa
*bb
*/

is transformed to
/*
*aa
*bb
*/

Usual is

/*
* aa
* bb
*/

So I ask for an additional feature ...

Reinhard

David Symonds

unread,
Dec 23, 2011, 6:21:38 PM12/23/11
to Reinhard, golang-nuts
On Sat, Dec 24, 2011 at 7:46 AM, Reinhard <zwi...@gmx.de> wrote:

> The vim plugins in Go work fine, but I personally prefer 4 spaces
> indent per level instead of a tab every 8th column. Of course, I could
> configure vim with ":set ts=4" but problems occur e.g. when copy-
> pasting part of the listing to
> some text processor.

The solution there is simply to run gofmt on the buffer after pasting.
If you're using the full set of vim plugins in the tree, :Fmt will do
that.

> It is quite easy to change the default:
>
> - in vim/ftplugin/go/fmt.vim, line 20: change "%!gofmt" to "%!gofmt -
> tabindent=false -tabwidth=4"
> - in vim/syntax/go.vim, line 32: insert "set sw=4"
>
> That´s all; it would be nice to make it configurable in one central
> place - what are your opinions?

Nope, not going to happen. You're free to change your own tabwidth,
but we're not going to make it easy for people to not use tabs.
There's one Go style.

> There is one problem left I could not solve. The "classic" C comment /
> * ... */ over several lines are only indented and completed right for
> the cindent option which is switched off deliberately. I am afraid
> that a solution in vim script language would not be so easy (the
> feature is hardcoded in vim), but I haven´t tried it yet. Would it not
> be nice to
> have this feature in gofmt? (Though writing with automatic indent of
> "*" and inserting a space after is more
> comfortable).

It honestly doesn't come up much. I don't think I've seen much Go code
at all that uses a line of stars for the /* */ style of comments; it's
either
/*
Some text
over lots of
lines.
*/
or
// Some text
// over lots of
// lines.
both of which are handled fine by vim.


Dave.

WillRubin

unread,
Dec 23, 2011, 8:43:28 PM12/23/11
to golang-nuts
> Nope, not going to happen. You're free to change your own tabwidth,
> but we're not going to make it easy for people to not use tabs.
> There's one Go style.

I'm all for pride of authorship but I find this type of comment off-
putting. I've seen a sort of "do it the Go way or don't do it"
attitude a few times in the forums and it always makes me cringe a
bit.

Tabs especially have been a contentious subject for at least the time
I've been programming. It's a real issue for those of us who have
large fonts and poorer eyesight. Tabs don't work consistently across
all editors and screens! Spaces do for me.

> In fact, I'm not sure why
> gofmt even has configurable options. Its point is to fix formatting
> once and for all.

Trying to force a subjective issue like this by removing switches has
nothing to do with the Go language. Please leave the tabs vs spaces
and tab size in gofmt. It's a useful tool and not having these options
makes it less so.

David Symonds

unread,
Dec 23, 2011, 10:45:25 PM12/23/11
to WillRubin, golang-nuts
On Sat, Dec 24, 2011 at 12:43 PM, WillRubin <endo...@gmail.com> wrote:

>> Nope, not going to happen. You're free to change your own tabwidth,
>> but we're not going to make it easy for people to not use tabs.
>> There's one Go style.
>
> I'm all for pride of authorship but I find this type of comment off-
> putting. I've seen a sort of "do it the Go way or don't do it"
> attitude a few times in the forums and it always makes me cringe a
> bit.
>
> Tabs especially have been a contentious subject for at least the time
> I've been programming. It's a real issue for those of us who have
> large fonts and poorer eyesight. Tabs don't work consistently across
> all editors and screens! Spaces do for me.

This is the whole point. There's been so much historic to-and-fro over
something that matters much less than people think. Pushing one
automated code layout style avoids people quibbling over such small
matters so we can spend time on more important things.

Good editors allow you to set how many spaces a tab character will be
rendered as. You're perfectly free to set your editor up to have a tab
width of two spaces, and then it's visually indistinguishable from
indenting with two space characters. If your editor doesn't make that
configurable, you should get a better editor.


Dave.

Jesse McNelis

unread,
Dec 23, 2011, 11:58:14 PM12/23/11
to golan...@googlegroups.com
On 24/12/11 12:43, WillRubin wrote:
> Trying to force a subjective issue like this by removing switches has
> nothing to do with the Go language. Please leave the tabs vs spaces
> and tab size in gofmt. It's a useful tool and not having these options
> makes it less so.

Search the mailing list and you'll see this discussion has been done to
death. Search the Internet and notice that every mailing list for every
project ever has this discussion and it's completely pointless.

For Go a decision was made on a style. It may not be exactly what you
like, but everyone else uses it and a consistent style across everyone
using the language increases readability far more than any one's
individual stylistic preferences.

If your screen or editor can't even handle tabs (stuck in the 70's?)
it's very doubtful it could handle utf-8 and thus isn't useful for
editing Go code.

I'd be happy if the gofmt style was part of the language spec and the
compilers actually enforced it.

- jessta

Mark Summerfield

unread,
Dec 24, 2011, 4:15:57 AM12/24/11
to Reinhard, golang-nuts
Hi Reinhard,

On Fri, 23 Dec 2011 12:46:38 -0800 (PST)
Reinhard <zwi...@gmx.de> wrote:
> The vim plugins in Go work fine, but I personally prefer 4 spaces
> indent per level instead of a tab every 8th column. Of course, I could
> configure vim with ":set ts=4" but problems occur e.g. when copy-
> pasting part of the listing to
> some text processor.
>
> It is quite easy to change the default:
>
> - in vim/ftplugin/go/fmt.vim, line 20: change "%!gofmt" to "%!gofmt -
> tabindent=false -tabwidth=4"
> - in vim/syntax/go.vim, line 32: insert "set sw=4"
>
> That´s all; it would be nice to make it configurable in one central
> place - what are your opinions?

I don't like the tabs either. The way I handle them is using a tiny
shell script in a file called 'gf':

#!/bin/bash
gofmt -s=1 -tabwidth=4 -tabs=false $*

I then reformat within vim using :'<,'>!gf or :%gf which aren't too
much to type.


--
Mark Summerfield, Qtrac Ltd, www.qtrac.eu
C++, Python, Qt, PyQt - training and consultancy
"Programming in Go" - ISBN 0321774639
http://www.qtrac.eu/gobook.html

Reinhard

unread,
Dec 24, 2011, 4:37:42 AM12/24/11
to golang-nuts
It is reasonable what Dave writes, and I will simply set ts=4 in
syntax/go.vim. But one line should be added anyway in this file (and
after possibly setting ts):

let &sw=&ts

By defaultIn .vimrc, shiftwidth is set to 1, so this leads to trouble
when inserting new code. .vimrc has nothing to do with Go. will, if
you are the author, could you do this?
But Will is also right, at least a bit. For instance, printing (and
copy-paste) requires now extra actions. There are reasons that an up-
to-date editor like vim writes under :help tabstop:

Note: Setting 'tabstop' to any other value than 8 can make your file
appear wrong in many places (e.g., when printing it).

And for Python, the option -t considers tab indenting as error. So at
least we should respect that there are people with different opinions.

Nevertheless, Merry Xmas :-)

Reinhard

Reinhard Wobst

unread,
Dec 24, 2011, 5:06:06 AM12/24/11
to golang-nuts
Sorry, some mistypings:

> By defaultIn .vimrc, shiftwidth is set to 1, so this leads to trouble

It is so in *my* .vimrc, since I write a lot of texts with it, not in
general.

> And for Python, the option -t considers tab indenting as error. So at

It issues a warning resp. error if tabs and spaces are mixed, but the
default Python style is 4 spaces per indent, nevertheless.

- Reinhard

Kyle Lemons

unread,
Dec 27, 2011, 4:18:25 PM12/27/11
to Mark Summerfield, Reinhard, golang-nuts
I don't like the tabs either. The way I handle them is using a tiny
shell script in a file called 'gf':

   #!/bin/bash
   gofmt -s=1 -tabwidth=4 -tabs=false $*

I fail to see how this preferable to

au BufRead,BufNewFile *.go set noet ts=4 sw=4

in your vimrc.  It's what I have, and it works brilliantly.  When my coworker who prefers ts=2 sw=2 opens the file, it looks the way he likes it.

Reinhard Wobst

unread,
Dec 27, 2011, 4:53:34 PM12/27/11
to golang-nuts


On 27 Dez., 22:18, Kyle Lemons <kev...@google.com> wrote:
> I fail to see how this preferable to
>
> au BufRead,BufNewFile *.go set noet ts=4 sw=4
>
> in your vimrc.  It's what I have, and it works brilliantly.  When my

This is in principle the better way. However, it is based on the fact
that autocommands are stacked, and .vimrc is executed before all
plugins which then use sw.
I personally prefer a local setting in e.g. .vim/after/syntax or .vim//
ftdetect/ since then it is clearer what happens (in my case, I am
alone on my computer).

Reinhard

Kyle Lemons

unread,
Dec 27, 2011, 6:14:39 PM12/27/11
to Reinhard Wobst, golang-nuts
This is in principle the better way. However, it is based on the fact
that autocommands are stacked, and .vimrc is executed before all
plugins which then use sw.
I personally prefer a local setting in e.g. .vim/after/syntax or .vim//
ftdetect/ since then it is clearer what happens (in my case, I am
alone on my computer).

Hmm.  I've always labored under the impression that it's pretty clear that an autocommand that runs when you load a *.go file is pretty much the last thing that runs, and thus overrides pretty much everything except other autocommands. 

Reinhard Wobst

unread,
Dec 28, 2011, 8:08:54 AM12/28/11
to golang-nuts
> Hmm.  I've always labored under the impression that it's pretty clear that
> an autocommand that runs when you load a *.go file is pretty much the last
> thing that runs, and thus overrides pretty much everything except other
> autocommands.

:help startup

is your friend :-) So .vimrc is executed before plugins - maybe I
overlooked something, but now I am in a hurry for leaving toward
holidays, cu next year ...

Reinhard

Kyle Lemons

unread,
Dec 28, 2011, 2:47:14 PM12/28/11
to Reinhard Wobst, golang-nuts
The order of file loading on startup is actually irrelevant to my point.  Autocommands that run on bufread/bufnewfile are run in their order of definition after the plugin settings are applied to a file, and thus override them.  I have yet to run into a ftplugin that tries to subvert you by registering autocommands, since they have much better and more sensible ways of doing such things. 

linzuxi...@gmail.com

unread,
Feb 14, 2014, 12:58:40 AM2/14/14
to golan...@googlegroups.com
In new version , I can find : %!gofmt  , line 20

- in vim/ftplugin/go/fmt.vim, line 20: change "%!gofmt" to "%!gofmt - tabindent=false -tabwidth=4" 


Caleb Spare

unread,
Feb 14, 2014, 3:34:02 AM2/14/14
to linzuxi...@gmail.com, golang-nuts
I recommend using vim's tabstop setting rather than changing tabwidth in gofmt. Standard gofmt style is to use tabs, not spaces, and future versions of gofmt will no longer have the -tabindent and -tabwidth flags.


--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

zuxiong lin

unread,
Feb 14, 2014, 4:20:13 AM2/14/14
to Caleb Spare, golang-nuts
my ~/.vimrc 
set smartindent
set tabstop=4
set expandtab
set backspace=2
set backspace=indent,eol,start
set nobackup

filetype off
filetype plugin indent off
set runtimepath+=/usr/local/Cellar/go/misc/vim
filetype plugin indent on
syntax on



But it does not work for go code when I code the new line.



" future versions of gofmt will no longer have the -tabindent and -tabwidth flags
I think it is great since our screen is big enough and go code is short on modern.



--
林足雄

QQ:         534439207 / fjjiaboming
Email:    linzuxi...@gmail.com
Mobile:     18688162114
Weibo:   fjjiaboming
Reply all
Reply to author
Forward
0 new messages