change emacs ruby-mode indent to 4 spaces
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)
Set 4 Space Indent in Emacs in Text Mode
Do not confuse variable
tab-width
with variabletab-stop-list
.
The former is used for the display of literalTAB
characters.
The latter controls what characters are inserted when you press theTAB
character in certain modes.
-- GNU Emacs Manual
(customize-variable (quote tab-stop-list))
or add tab-stop-list entry to custom-set-variables in .emacs file:
(custom-set-variables
;; custom-set-variables was added by Custom.
;; If you edit it by hand, you could mess it up, so be careful.
;; Your init file should contain only one such instance.
;; If there is more than one, they won't work right.
'(tab-stop-list (quote (4 8 12 16 20 24 28 32 36 40 44 48 52 56 60 64 68 72 76 80 84 88 92 96 100 104 108 112 116 120))))
Another way to edit the tab behavior is with with M-x edit-tab-stops
.
See the GNU Emacs Manual on Tab Stops for more information on edit-tab-stops
.
Indent with tab instead of spaces in Emacs ruby-mode
Try adding the following to your init.el
(below the customizations you already have):
(setq-default indent-tabs-mode t)
From the documentation for indent-tabs-mode
:
Indentation can insert tabs if this is non-nil.
I don't use ruby-mode
so I don't know about possible interactions between indent-tabs-mode
and ruby-indent-tabs-mode
. It might just be enough to set indent-tabs-mode
to t
(and erase the customizations you made to ruby-indent-tabs-mode
). But when you add the snippet above to your configuration, the default behavior for Emacs will be to insert tabs for indentation.
EDIT
As can be seen here, enh-ruby-mode
defines a customizable variable called enh-ruby-indent-tabs-mode
with a default value of nil
. Later on the value of this variable is used to override the value of indent-tabs-mode
, which is why setting indent-tabs-mode
to t
has no effect on buffers with enh-ruby-mode
enabled.
So unless you enable any other modes besides ruby-mode
and enh-ruby-mode
that might be modifying the indent-tabs-mode
variable, adding
(setq enh-ruby-indent-tabs-mode t)
to your init.el
should fix your problem.
Another EDIT (working solution)
(Credits: This answer put me on the right track.)
Using
Emacs 24.3.1
ruby-mode
version 1.2 (built-in)enh-ruby-mode
version 20140406.252 (installed via M-xpackage-install
...)
I was able to make it work by adding the following to an otherwise completely empty init.el
file:
(package-initialize)
(setq-default tab-width 2)
(setq enh-ruby-indent-tabs-mode t)
(defvaralias 'enh-ruby-indent-level 'tab-width)
(defvaralias 'enh-ruby-hanging-indent-level 'tab-width)
This solution works for both the GUI and the console version of Emacs. It will probably integrate fine with your other customizations but you will need to remove the custom-set-variables
section and everything below it from the version of your init.el
you posted above.
Note also that if you do come across a situation in which Emacs inserts a space instead of a tab you can always delete it and force insertion of a tab by quoting it via C-q TAB.
Wrapping up
Turns out there is a bug in enh-ruby-mode
which causes indentation to fail for blocks starting from the second level when enh-ruby-indent-tabs-mode
is set to t
. The author/maintainer of enh-ruby-mode
has no plans of fixing it, but the bug report includes a patch that supposedly fixes the issue.
Emacs won't set indentation to 4 spaces in any language
From C-h v default-tab-width RET
This variable is obsolete since 23.2;
use `tab-width' instead.
and for tab-width
Documentation:
*Distance between tab stops (for display of tab characters), in columns.
You can customize this variable.
When I insert
(custom-set-variables
'(tab-width 4))
into ~/.emacs
and restart emacs, it is set to 4
.
How to change Emacs struct indents from 4 to 2 spaces
You should remove your configuration and start with
(custom-set-variables
'(c-basic-offset 2))
This sets all indentation at 2 spaces.
Then you can improve from there on.
You can set c-offsets-alist
for example, to customize indentation for various elements. Or c-hanging-braces-alist
to configure, where your braces should be set, on the same or at the next line. And so on.
If you have installed CC Mode info files, you can browse through it with
Ctrl-h i mCC ModeRET
CC Mode doesn't distinguish between class
and struct
, for both the syntactic element is inclass
. You can have a different indentation based on struct only with a Custom Line-Up Function
(defun my/c-lineup-inclass (langelem)
(let ((inclass (assoc 'inclass c-syntactic-context)))
(if (not inclass)
0
(save-excursion
(goto-char (c-langelem-pos inclass))
(if (looking-at "struct") 0 '+)))))
This function looks, if you are inside a class
or struct
or outside and returns an indentation level accordingly. You can then use this in your c-offsets-alist
(custom-set-variables
'(c-offsets-alist (quote ((access-label . 0)
(topmost-intro . my/c-lineup-inclass)))))
emacs how to setup tab size to 4 chars?
basic offset
means other indentations are based on it. So,
for () {
....if () { // 4 spaces
........ // 8 spaces
....}
}
to cite Gnue Emacs
This style variable holds the basic offset between indentation levels
So you won't get:
for () {
....if () { // 4 spaces
...... // 6 spaces
....}
}
But of course you could do that if you want.
And usually, it's recommended to use spaces instead of tabs:
(setq-default indent-tabs-mode nil)
Use M-x untabify
to do that for a specific buffer.
How do I set Emacs tab size to 4 chars wide for .py files?
(add-hook 'python-mode-hook
(lambda ()
(setq indent-tabs-mode t)
(setq tab-width 4)
(setq python-indent-offset 4)))
Change tab width in emacs
Both tab-width
and c-basic-offset
variables are buffer-local, which means it is effective only in the buffer you set it. Emacs only evaluates ~/.emacs
at start up, and it is only effective in that file.
To set a default value for all buffers, you need
(setq-default tab-width 4)
Emacs ruby-mode indent private
Private doesn't introduce a new scope, so indenting definitions under it is not technically correct. While there are several indentation styles for private/protected members, the only one supported by ruby-mode is the one that is semantically correct (doesn't introduce additional nesting). The "Ruby style guide" also recommends that style (not to mention a two 2 space indentation).
That all being said - there's no way to customize this aspect of ruby-mode
.
Related Topics
Using Poltergeist with a Proxy
How to Serialize a Ruby Digest::Sha1 Instance Object
Errno::Enoent No Such File or Directory Rails 4
How to Transform the Utf8 Chars to Iso8859-1
Empty Strings at the Beginning and End of Split
How Can Same Program in Ruby Accept Input from User as Well as Command Line Arguments
Ruby on Rails Workflow Engine (Like Ibm Workflow)
How to Sort an Alphanumeric Array in Ruby
How to Download File from Google Drive API with Service Account
Ruby on Rails - £ Sign Troubles
Rails Object Based Permission/Authorization Engine
How to Implement Editing/Formating Text Area in Rails