Emacs golang-mode indent setting

8,173 views
Skip to first unread message

Tong Sun

unread,
Jan 19, 2013, 11:36:03 PM1/19/13
to golan...@googlegroups.com
Hi, 

I'm wondering how to set Emacs golang-mode to use 2 as indent level and use only space (no tab) for indent?
I.e., same as "gofmt -tabs=false -tabwidth=2"?

Thanks

Patrick Mylund Nielsen

unread,
Jan 20, 2013, 12:09:35 AM1/20/13
to Tong Sun, golang-nuts
Please consider configuring emacs to show tabs as two spaces, i.e. (setq default-tab-width 2) in your ~/.emacs, then use the default style (go fmt.) Especially if you are planning to ever share the code.



--
 
 

Tong Sun

unread,
Jan 20, 2013, 10:35:54 AM1/20/13
to golang-nuts
On Sat, Jan 19, 2013 at 11:36 PM, Tong Sun <sunto...@gmail.com> wrote:

I'm wondering how to set Emacs golang-mode to use 2 as indent level and use only space (no tab) for indent?
I.e., same as "gofmt -tabs=false -tabwidth=2"?

Made some progress:

(setq-default 
 tab-width 2
 standard-indent 2
 indent-tabs-mode nil) ; makes sure tabs are not used.

Now the problem is how to hook it up into ghe go-mode, instead of affecting all of my other modes. 

Thanks

Dustin Sallings

unread,
Jan 20, 2013, 4:31:50 PM1/20/13
to golan...@googlegroups.com
Tong Sun <sunto...@gmail.com> writes:

> (setq-default 
>  tab-width 2
>  standard-indent 2
>  indent-tabs-mode nil) ; makes sure tabs are not used.
>
> Now the problem is how to hook it up into ghe go-mode, instead of
> affecting all of my other modes. 

While still echoing everyone else's suggestion to use the same
formatting everyone else uses, I'll give you a bit of my .emacs you can
poison to your heart's content :)

(defun my-go-mode-hook ()
(add-hook 'before-save-hook 'gofmt-before-save)
(setq tab-width 8 indent-tabs-mode 1))
(add-hook 'go-mode-hook 'my-go-mode-hook)

--
dustin

Tong Sun

unread,
Jan 20, 2013, 9:11:23 PM1/20/13
to Dustin Sallings, golang-nuts
On Sun, Jan 20, 2013 at 4:31 PM, Dustin Sallings <dsal...@gmail.com> wrote:
  While still echoing everyone else's suggestion to use the same
formatting everyone else uses, I'll give you a bit of my .emacs ...

Thanks Dustin, work great. Don't worry about my formatting, I think as long as everyone is using gofmt, we all will be fine. 


ma...@rightscale.com

unread,
Feb 13, 2014, 5:27:57 PM2/13/14
to golan...@googlegroups.com
This seems to work:

(add-hook 'go-mode-hook
  (lambda ()
    (setq-default)
    (setq tab-width 2)
    (setq standard-indent 2)
    (setq indent-tabs-mode nil)))

gihan karunarathne

unread,
Feb 19, 2015, 10:55:44 PM2/19/15
to golan...@googlegroups.com, ma...@rightscale.com
Great. It is formatting with indent size 2, but it uses tabs instead spaces (on Emacs 24.4). How to avoid it ? (means replace tabs with spaces)
My configuration:
  (add-hook 'go-mode-hook 
  (lambda ()
    (add-hook 'before-save-hook 'gofmt-before-save)
    (setq-default) 
    (setq tab-width 2) 
    (setq standard-indent 2) 
    (setq indent-tabs-mode nil)))

Thank you in advance!

Tong Sun

unread,
Feb 19, 2015, 11:10:25 PM2/19/15
to gihan karunarathne, golang-nuts, ma...@rightscale.com

--
You received this message because you are subscribed to a topic in the Google Groups "golang-nuts" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/golang-nuts/c176nKcyoDQ/unsubscribe.
To unsubscribe from this group and all its topics, send an email to golang-nuts...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages