Emacs 24 ruby-mode indentation behavior when using iterator such as `each`
Seems to me you're using emacs-snapshot
. By default it uses a new indentation engine that's a bit buggy right now. To revert to the old one evaluate the following Emacs Lisp code:
(setq ruby-use-smie nil)
And restart ruby-mode
afterwards. I'll report the bug upstream.
Emacs ruby mode if expressions indentation
I guess you actually meant to say that Emacs aligns the if
with the end
, which is actually pretty idiomatic in Ruby (and the style enforced by tools like RuboCop). The second indentation style is popular for method class with blocks, but not for expressions like if/unless/case
.
Currently there is now way to change this behaviour. There are plans to introduce a more flexible indentation scheme in ruby-mode in the future, but that's not going to happen in the next Emacs release.
At any rate - it's not a bug, it's a feature :-)
Emacs ruby-mode, indenting wildly inside parentheses?
http://compgroups.net/comp.emacs/Ruby-mode-indentation-of-continuation-lines
(setq ruby-deep-indent-paren nil)
Or temporarily, within the current session:
M-x set-variable RET ruby-deep-indent-paren RET nil RET
Inside of a parentheses it will now indent like it does everywhere else. There is still a minor bug in the case of what I posted above. It indents 2 spaces further than I want it to, because I'm confusing it with the combination of (
and {
.
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
.
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)
Emacs ruby-mode: (not so) strange indentation for new Rails code conventions
This seems fixed in ruby HEAD
Can RuboCop output the proper indent level for each line?
I'm one of RuboCop's developers and an Emacs user/contributor as well. While RuboCop can't show you the correct indentation levels, so you can feed it to ruby-mode
(for instance), upgrading to the unreleased Emacs 24.4 will probably solve your problems. There were a ton of ruby-mode indentation (and many other) bugs in Emacs 24.3 that were fixed in 24.4. All Ruby developers that use Emacs will do themselves a huge favour by updating their Emacs.
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.
Related Topics
How to Set the Default String Encoding on Ruby 1.9
What Does the Fail Keyword Do in Ruby
Are the Date, Time, and Datetime Classes Necessary
In-Place Progress Output in the Terminal or Console
How to Find If a String Starts with Another String in Ruby
Is There a Rails Console Command (Rails 3+) to Reload Changed Code
Rails 4 How to Ignore Pending Migrations
Force Strings to Utf-8 from Any Encoding
Is There a "Main" Method in Ruby Like in C
How to List All Objects Created from a Class in Ruby
Ruby Dropped in Netbeans 7,How to Use It in Netbeans7
How to Store Site-Wide Variables in Rails 4
Tell the End of a .Each Loop in Ruby
Error While Installing Ruby Using Rvm
Execute a Rake Task from Within Migration
Fail VS. Raise in Ruby:Should We Really Believe the Style Guide