35

I've been experimenting quite a bit with the increasingly popular %>% operator from the magrittr package.

I've used it enough that I've set a keyboard shortcut to save me typing:

shift+command+. instead of space, shift+5, shift+., shift+5, space.

snapshot

This is great in SublimeTetxt2 but Rstudio does not allow services it does not work if I'm working within Rstudio projects.

So my question is:

Can you define text-inserting key-bindings or shortcuts within Rstudio?

This would be exactly synonymous with the alt+- binding for the assignment <- operator that is oxygen to the otter.

If someone could point me to where the shortcuts are coded in the github repo so I could "hack" my own, I suppose this would be a useful start.

https://github.com/rstudio/rstudio

Uwe
  • 41,420
  • 11
  • 90
  • 134
npjc
  • 4,134
  • 1
  • 22
  • 34
  • Have you posted a request in the RStudio forums? – Carl Witthoft May 29 '14 at 11:34
  • 1
    @CarlWitthoft done indeed. That being said I had hoped that there be a simple official method or fix/hack/workaround that I could adapt without having to submit FR. I would personally be very interested to find out what the folks at Rstudio think about making the UNIX-style syntax "more-accessible" to R users. – npjc May 29 '14 at 16:38
  • 4
    Haven't found a way, but currently I'm using AutoKey(https://code.google.com/p/autokey/) on Linux to bind Alt+. to %>%. Works great and you can make it so that the shortcut is only "activated" in RStudio. – Rasmus Bååth Jun 16 '14 at 12:55
  • Nice idea. I use Emacs and created a key-chord (two keys pressed quickly) so that >> is auto-changed into the unwieldy %>%. – Kevin Wright Sep 12 '14 at 19:37
  • The latest preview version (> 0.99.660, Aug 2015) of RStudio allows you to modify many of the keyboard shortcuts. Just go to Tools -> Modify Keyboard Shortcuts. The pipe operator is not in that list btw. – Peter Diakumis Aug 18 '15 at 00:24

5 Answers5

54

Just noticed that RStudio v0.98.1079 has shift+command+M mapped for the %>% operator. You can check out the other keyboard shortcuts within RStudio by clicking at the Help tab or by entering alt+shift+K.

Peter Diakumis
  • 3,942
  • 2
  • 28
  • 26
  • i seem to have disabled this shortcut key (unsure how) - do you you know how to turn this back on? – d8aninja Mar 28 '15 at 19:07
  • 5
    @D8Amonk I'm not sure how you've managed to do that. Perhaps try re-installing the newest release of RStudio? And the keyboard shortcuts I mention are for a Mac - if you've got Windows it will be `shift` + `control` + `M`. – Peter Diakumis Mar 29 '15 at 02:36
2

I think the answer to your question is "Not currently, but it's on RStudio's to-do list" (see: this and this).

If you want to hack it yourself, it looks like this is where you'd do it (GitHub: rstudio / src / gwt / src / org / rstudio / core / rebind / command / ShortcutsEmitter.java).

Also, it looks like shortcuts are managed here (GitHub: rstudio / src / gwt / src / org / rstudio / core / client / command).

rsoren
  • 4,036
  • 3
  • 26
  • 37
2

For stuff like that you can use autohotkey. it is an external windows program but can do pretty much anything including hotkeys that type stuff and acronyms that are replaced by stuff you want

OganM
  • 2,543
  • 16
  • 33
0

R studio Preview (v0.99.671) now supports keybuindings customization. It also has a bunch of other nice improvements, give it a try:

https://www.rstudio.com/products/rstudio/download/preview/

Ufos
  • 3,083
  • 2
  • 32
  • 36
0

For people now, the best way to do something like this in RStudio if it's not already built in is to add a "snippet":

https://appsilon.com/rstudio-shortcuts-and-tips/#custom-snippets

It's in your global options and can be used to save pieces of code you often repeat to quickly insert it.

enter image description here

giraffehere
  • 1,118
  • 7
  • 18