Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

change emacs ruby-mode indent to 4 spaces

From a previous post I got Ruby mode working in emacs. This is working great.

Setting up .emacs file for mac ruby development

Our company uses 4 spaces for indents though instead of the default 2. I am having difficulty getting this to work.

Here is my .emacs file

(add-to-list 'load-path "~/rdoc-mode.el")

(require 'ruby-mode)

(setq indent-tabs-mode nil) ; always replace tabs with spaces

(setq-default tab-width 4) ; set tab width to 4 for all buffers

Does anyone see what I am doing wrong?

Thanks!

like image 295
Poul Avatar asked Jan 21 '10 16:01

Poul


People also ask

How do you set a tab to 4 spaces in Emacs?

Indent-relative Space out to under next indent point in previous nonblank line. If the previous nonblank line has no indent points beyond the column point starts at, `tab-to-tab-stop' is done instead. Just change the value of indent-line-function to the insert-tab function and configure tab insertion as 4 spaces.

How many spaces is a tab in Emacs?

This is because standard tabs are set to eight spaces. Tabs are special characters.


2 Answers

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"!

like image 167
sanityinc Avatar answered Sep 17 '22 22:09

sanityinc


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)
like image 20
Pär Wieslander Avatar answered Sep 18 '22 22:09

Pär Wieslander