lu2idreams · 2022-10-20 22:18
Hey everyone! I recently started using Emacs+ESS for writing R code, and I am really liking it so far, Emacs is impressively extensible & flexible. There is however one thing I am missing: keybindings to insert some special snippets/characters I use really frequently. Specifically, I want to bind Control-Shift-m to insert the R pipe operator „%>%“, and Alt-i to insert the assignment arrow „<-“, like in RStudio, as not having these bindings sort of disrupts my workflow. What would be the best way to do this in Lisp?
11fdriver · 2022-10-20 23:22
As long as you are happy to replace the existing keybindings with these, then please do! Emacs is all about how you can change your editor to work best for you.
There are probably ways to make this more general, but this is hopefully the simplest and most illustrative:
(defun my-insert-R-pipe ()
"Insert '%>%' at point, moving point forward."
(interactive)
(insert "%>%"))
(defun my-insert-R-assignment ()
"Insert '<-' at point, moving point forward."
(interactive)
(insert "<-"))
(define-key ess-mode-map (kbd "C-S-m") #'my-insert-R-pipe)
(define-key ess-mode-map (kbd "M-i") #'my-insert-R-assignment)
Edit:
I should also mention that Emacs includes several ways of inserting different forms, interactively or otherwise.
`auto-insert` & `auto-insert-mode` spits templates into newly-created files according to file-type.
Tempo and Skeleton are templating syntaxes/macros that allow for interactive insertion of text from a command. See the docs for `tempo-define-template` and `skeleton-insert`, and maybe the Emacs manual entry on 'Autotyping'.
`abbrev-mode` is Emacs' text-replacement engine. It's a touch basic, but you can always use an abbrev to call a skeleton/function instead.
globalcandyamnesia · 2022-10-21 00:16
I don't want to get too fancy, but you could add `(just-one-space)` before and after the `insert` to normalize spacing.
11fdriver · 2022-10-21 00:29
Good idea!
[deleted] · 2022-10-21 02:42
[deleted]
FreeAd7233 · 2022-10-21 03:55
Like other says, directly mapping a key to insert those characters is a way.
alternatively, consider using a snippets, look for yasnippets or tempel (they are two popular snippet plugin), and write your own snippet is easy. I set “pip” to be the snippet trigger key for %>%, and “eq” for the snippet trigger key of <-. And you can just complete snippets with auto completion pop up menu like company and corfu(corfu only supports tempel).
cerka · 2022-10-21 05:18
I second [yasnippet](https://github.com/joaotavora/yasnippet), it is extremely handy for all sorts of boilerplate (like LaTeX preambles, environments). There are many contributed snippets in the [yasnippet-snippets](https://github.com/AndreaCrotti/yasnippet-snippets/tree/master/snippets) repo that you can look at as examples.
lu2idreams · 2022-10-21 06:30
Thank you very much for your comprehensive answer! Playing around with this, I found out that ess already defines a function for inserting the assignment operator, which also ensures it is surrounded by whitespace (which is very handy), so this can simply be bound to a key. Leaving my code here for anyone who may also wish to do this:
(require 'ess-mode)
(define-key ess-mode-map (kbd "C-S-m") "%>%")
(define-key inferior-ess-mode-map (kbd "C-S-m") "%>%")
(define-key ess-mode-map (kbd "M--") #'ess-insert-assign)
(define-key inferior-ess-mode-map (kbd "M--" #'ess-insert-assign)
Also noting that the RStudio key binding for inserting the `<-` assignment operator is obviously Alt+-, not sure how I got that one messed up. ESS also provides language-specific modes, as per [their documentation](https://ess.r-project.org/Manual/readme.html), which may be interesting as well for anyone coming across this.
Less elegant than your solution, but also works :D Also, `auto-insert` etc you mentioned sound like interesting options for templates, perhaps for R Markdown; I will definitely look into it! Thank you
mickeyp · 2022-10-21 07:37
Another way is learning how to write a keyboard macro:
https://www.masteringemacs.org/article/keyboard-macros-are-misunderstood
You can do this, easily, and much more, without having to touch much (or any) elisp.
Ok-Spinach-8808 · 2022-10-23 20:53
i second that motion