57

I am using the Emacs-Speaks-Statistics (ESS) mode for Emacs. When editing R code, any comment lines (those starting with #) automatically get tabbed to the far right when I create a new line above it. How should I change my .emacs.el file to fix this?

For example, I have:

# Comment

Now, after putting my cursor at the beginning of the line and pressing Enter, I get:

                                # Comment

Thanks for any hints.

Dason
  • 60,663
  • 9
  • 131
  • 148
Christopher DuBois
  • 42,350
  • 23
  • 71
  • 93

4 Answers4

70

Use '###' if you don't want the comments indented. According to the manual,

By default, comments beginning with ‘###’ are aligned to the beginning of the line. Comments beginning with ‘##’ are aligned to the current level of indentation for the block containing the comment. Finally, comments beginning with ‘#’ are aligned to a column on the right (the 40th column by default, but this value is controlled by the variable comment-column,) or just after the expression on the line containing the comment if it extends beyond the indentation column.

huaiyuan
  • 26,129
  • 5
  • 57
  • 63
  • 2
    I am glad I looked before asking this exact same question. I marked up this answer because I think it is really a better solution than the answer above. I have long thought this was a bug / problem with ESS mode, but after tweaking my syntax, my problems have all gone away. – Choens Feb 03 '12 at 19:20
  • 5
    This convention may seem odd if you do not use Emacs much. However, this convention comes from lisp and emacs-lisp, where the number of semi-colons denotes the type of comment, and hence how it should be indented. – Stephen Eglen Aug 15 '14 at 16:34
34

Either

(setq ess-fancy-comments nil)

if you never want to indent single-# comments, or

(add-hook 'ess-mode-hook 
          (lambda () 
            (local-set-key (kbd "RET") 'newline)))

if you want to change the behavior of Enter so it doesn't indent.

Jouni K. Seppänen
  • 43,139
  • 5
  • 71
  • 100
  • 4
    Rather I think you should use "#" for end-of-line comments, and these are nicely indented to the same column on purpose --> nice code "listing". For the other comments, really do get in to the habit of using "##" (much more than "###"): These indent as other "statements" within that block of code – Martin Mächler Jul 20 '10 at 08:26
  • 2
    I'm still in favour of deciding how many #s I should use. One is generally sufficient so this answer is greatly appreciated as my comment is not moved to a location I don't want it to be... – user989762 Jan 29 '16 at 13:35
5

Setting ess-indent-with-fancy-comments to nil will remove the weird single-# indentation, but it must be set either buffer-locally in a hook (as in Rob's answer), OR before ESS is loaded:

(setq ess-indent-with-fancy-comments nil)
(require 'ess)

Other ways to make sure it is set before ESS is loaded, is to set it in M-x configure, or to set it in the :init section of use-package.

What's going on is that ESS defines styles at initialization in ess-style-alist, and then applies the default style in every buffer. So to make sure these styles respect ess-indent-with-fancy-comment, you must make sure to set it before the styles are defined.

jackkamm
  • 541
  • 1
  • 4
  • 11
  • After my upgrading to ESS 18.10.3, the hooking method does not work for me anymore. `setq` before `require ess` works. – Yin Zhu Feb 27 '20 at 01:29
3

Jouni's answer didn't work for me. But I found an approach here that does: https://stat.ethz.ch/pipermail/ess-help/2016-May/010970.html

   (defun my-ess-settings ()
     (setq ess-indent-with-fancy-comments nil))
   (add-hook 'ess-mode-hook #'my-ess-settings)
Rob Donnelly
  • 2,256
  • 2
  • 20
  • 29