How to stop emacs from replacing underbar with <- in ess-mode

REmacsEss

R Problem Overview


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)

R Solutions


Solution 1 - R

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

Solution 2 - R

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)

Solution 3 - R

From http://www.r-bloggers.com/a-small-customization-of-ess/ and https://stackoverflow.com/questions/25404278/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!

Solution 4 - R

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-;.

Solution 5 - R

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).

Solution 6 - R

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)))

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionSetjmpView Question on Stackoverflow
Solution 1 - RMichał MarczykView Answer on Stackoverflow
Solution 2 - RKushdeshView Answer on Stackoverflow
Solution 3 - RWMashView Answer on Stackoverflow
Solution 4 - RDrummermeanView Answer on Stackoverflow
Solution 5 - RmiguelmorinView Answer on Stackoverflow
Solution 6 - Rden2042View Answer on Stackoverflow