change emacs ruby-mode indent to 4 spaces

蹲街弑〆低调 提交于 2019-12-01 16:02:42
Pär Wieslander

The tab-width setting only controls the width of a tab character, i.e. how many spaces a tab character is equivalent to when displayed in your buffer. It does not affect the number of spaces (or tabs) used for indenting your code.

For Ruby code, the indentation is controlled by the ruby-indent-level variable:

(setq ruby-indent-level 4)

The other posters have provided the correct answer, so I'll mention here how to figure out the answer to this kind of question.

First of all, since you correctly assumed that the indent width would be configurable, the first thing to try is:

M-x customize-group RET ruby-mode RET

And sure enough, one of the customization options there is "Ruby Indent Level". You can set it and save the changes. Done!

Alternatively, you can look at ruby-mode itself:

M-x find-library RET ruby-mode RET

Then search (with C-s) for 'indent'. There you'll find a variable definition:

(defcustom ruby-indent-level 2 ...)

When you find a variable like that, you can set it in your .emacs (or ~/.emacs.d/init.el) with setq:

(setq ruby-indent-level 4)

You could also discover that variable using apropos:

M-x apropos RET indent ruby RET

That's why emacs is described as "self-documenting"!

There's a way to do it without touching .emacs. You can put a special comment block at the end of every Ruby file that sets "file variables" specific to that file. Any emacs or xemacs editing that file will use the mode, tab settings, & etc in that comment block.

As an example, here is the "file variables" block we use for Ruby development:

# Local Variables:
# mode: ruby
# tab-width: 2
# ruby-indent-level: 2
# indent-tabs-mode: nil
# End:
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!