56

ess-mode is "Emacs speaks statistics." This mode is useful for editing programs for R or Splus (two separate statistics packages).

In my buffer, when ever I type _ the character is replaced with <-, which is very frustrating. Is there an emacs lisp statement to turn off this behavior?

emacs: 22.1.1 ess-mode release (unknown)

Setjmp
  • 27,279
  • 27
  • 74
  • 92
  • 1
    Duplicate - see http://stackoverflow.com/questions/1816238/how-to-turn-off-auto-replacement-in-emacs-speaks-statistics-for-r – csgillespie Jun 10 '10 at 16:13
  • 3
    If you use `underscore_case` in the mix of naming conventions in R, (see https://journal.r-project.org/archive/2012/RJ-2012-018/RJ-2012-018.pdf ) it is quite irritating. – Dave X Nov 01 '17 at 18:16

6 Answers6

65

From ESS's manual (look under "Changes/New Features in 5.2.0"):

ESS[S]: Pressing underscore ("_") once inserts " <- " (as before); pressing underscore twice inserts a literal underscore. To stop this smart behaviour, add "(ess-toggle-underscore nil)" to your .emacs after ess-site has been loaded

Michał Marczyk
  • 83,634
  • 13
  • 201
  • 212
  • 10
    Just to be explicit, `(require 'ess-site)` should precede `(ess-toggle-underscore nil)` in .emacs or .emacs.d/init.el . – jthetzel Sep 25 '15 at 13:43
5

Since the feature is useful. You can assign it to other key which is less used by you in R it will automatically unassign it from underscore. I personally assign it to ";" by adding following line in .emacs file.

(setq ess-smart-S-assign-key ";")

My version of emacs is 24.3 All-in-one installation file by Vincent Goulet.(Installed on windows 7)

hope this helps

Edit In emacs 25.2 above do not work instead add following in the .emacs file

(setq ess-smart-S-assign-key ";")
(ess-toggle-S-assign nil)
(ess-toggle-S-assign nil)
Kushdesh
  • 1,118
  • 10
  • 16
  • 1
    This stopped the problem of underscore replacement, but when I press ";" it doesn't make a "<-"...? – maia May 18 '15 at 14:26
  • It makes `<-` in my version of emacs on windows 7. Is your any different? – Kushdesh May 30 '15 at 09:08
  • 2
    Over the years, there has been [significant](https://github.com/emacs-ess/ESS/issues/584) ... [discussion](https://github.com/emacs-ess/ESS/issues/558) ... resulting in [commit ed048d3](https://github.com/emacs-ess/ESS/commit/ed048d32be45b2a22fbe4d1c192cfdd7f367864b), with name-changes and some new default behavior. Most `*-smart-*` vars/funs are deprecated/deleted, now there is `ess-insert-S-assign` and optionally `ess-cycle-assignment`. – r2evans Aug 14 '18 at 16:37
3

From http://www.r-bloggers.com/a-small-customization-of-ess/ and How to change smart assign key ("_" to "<-") binding in ESS

To assign ":" to "<-" and to stop the assignment of underscore (underbar) "_" to "<-" put the following in .emacs (yes, the repeated line is correct)

(setq ess-smart-S-assign-key ":")
(ess-toggle-S-assign nil)
(ess-toggle-S-assign nil)
(ess-toggle-underscore nil) ; leave underscore key alone!
WMash
  • 407
  • 4
  • 5
  • 1
    an explanation of why the duplicate line is necessary would be helpful. – Jacob Lee Mar 20 '19 at 16:33
  • 1
    According to the cited source: "The first call clears the default `ess-smart-S-assign' assignment and the second line re-assigns it to the customized setting." – WMash May 31 '22 at 18:56
3

A more recent version which seemed to work for me, and is a lot less verbose (you essentially keep normal underscores, but can set your own key for this smart behaviour!):

(global-set-key (kbd "C-;")  (lambda () (interactive) (insert " <- ")))
(ess-toggle-underscore nil)

Insert your shortkey choice instead of C-;.

Drummermean
  • 209
  • 3
  • 6
2

Like Michał Marczyk and this R mailing list thread suggested, add this line to ~/.emacs:

(ess-toggle-underscore nil)

Then reload it with M-x load-file and type ~/.emacs.

But if you load the file again, e.g. if you add another customization, then it toggles it back to the original state. So toggle it twice, the first one forcing it to the default:

(ess-toggle-underscore t)
(ess-toggle-underscore nil)

That being said, I like Drummermean's solution better, but it also reverts back to default if you add it to ~/.emacs and load it twice. So force a toggle to the default before:

(ess-toggle-underscore t)
(global-set-key (kbd "M--")  (lambda () (interactive) (insert " <- ")))
(ess-toggle-underscore nil)

I bound the smart assignment to Opt-[minus] like RStudio (on a Mac).

miguelmorin
  • 5,025
  • 4
  • 29
  • 64
1

As a follow-up on @mmorin answer. To set keybinding for the assignment operator the same way as in Rstudio add the following in your .emacs file

(ess-toggle-underscore t)
(ess-toggle-underscore nil)
(define-key ess-mode-map (kbd "M--") (lambda () (interactive) (just-one-space 1) (insert "<-") (just-one-space 1)))
(define-key inferior-ess-mode-map (kbd "M--") (lambda () (interactive) (just-one-space 1) (insert "<-") (just-one-space 1)))

den2042
  • 497
  • 4
  • 4