2

I am trying to define keymapping about %>% for ess R mode.
Code 1 is my init.el based on How to implement the piping operator %>% in ESS mode?. After evaluation of the code, I get the key binding Code 2. I don't know why such a situation happen and RET does not work well. How do I fix it?

Code 1

(defun then_R_operator ()
  (interactive)
  (just-one-space 1)
  (insert "%>%"))

(define-key ess-mode-map (kbd "C-%") 'then_R_operator)
(define-key inferior-ess-mode-map (kbd "C-%") 'then_R_operator)

Code 2

key             binding
---             -------
RET     then_R_operator
C-%     then_R_operator

Update

I also try a different method as follows. But, I got the same result.

(define-key ess-mode-map (kbd "C-M") "%>%")
(define-key inferior-ess-mode-map (kbd "C-M") "%>%")
Drew
  • 75,699
  • 9
  • 109
  • 225
hrkshr
  • 113
  • 8
  • Are you using the graphical Emacs, or are you running it in a terminal? It could be that your terminal doesn't support `C-%`, and it's getting treated as RET instead – Tyler May 03 '21 at 13:53
  • I am using the graphical Emacs. – hrkshr May 03 '21 at 14:26
  • You can't bind `C-M`, it sends the same symbol as `RET`. Your original version works as expected for me with `C-%`. Maybe you had `C-M` bound somewhere as well, that would have caused the problem. – Tyler May 03 '21 at 14:42
  • Yes! ```C-M``` is Rstudio's keybinding. I didn't think this cause the problem. – hrkshr May 03 '21 at 15:04

2 Answers2

0

I found an easy solution from https://stackoverflow.com/questions/59251402/how-to-set-up-custom-automatic-character-replacement-in-emacs-ess.

(define-key ess-mode-map (kbd "C-%") "%>%")
(define-key inferior-ess-mode-map (kbd "C-%") "%>%")

Note C-M causes a problem.

hrkshr
  • 113
  • 8
0

Another way to define the key binding is with use-package:

(use-package ess
  :bind (:map ess-r-mode-map
         ("C-c >" . " %>% ")
         :map inferior-ess-r-mode-map
         ("C-c >" . " %>% ")))
aparkerlue
  • 317
  • 2
  • 8