3

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") "%>%")
4
  • 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
    Commented May 3, 2021 at 13:53
  • I am using the graphical Emacs.
    – hrkshr
    Commented May 3, 2021 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
    Commented May 3, 2021 at 14:42
  • Yes! C-M is Rstudio's keybinding. I didn't think this cause the problem.
    – hrkshr
    Commented May 3, 2021 at 15:04

2 Answers 2

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.

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.