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

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