ClementWalter
ClementWalter

Reputation: 5272

Keyboard shortcut for inserting roxygen #' comment start

This question might be over-answered but I could not find one. Basically I am using RStudio and the keyboard shortcut cmd + shift + c for inserting comments. Is there an other combination to insert directly the roxygen tags #' ? Or a way to modify RStudio to tell it to add the ' when I press cmd + shift + c?

Upvotes: 30

Views: 6655

Answers (4)

GoGonzo
GoGonzo

Reputation: 2867

Rstudio find/replace

enter image description here

Select text to comment out, tick regex option and specify:

  • find: ^(.+)
  • replace: #' \1

Above means to find all characters (.+) following beginning of the line ^ and replace them by the #' and the first captured group \1.

vim find/replace

enter image description here I find this option the easiest as I use Rstudio in vim mode. To replace text one only need to:

  • select text
  • go to the "command-line mode" by using : key
  • enter s/^/#' and hit enter.

s/ stands for "substitute", ^ stands for beginning of the line and #' is the text we are inserting.

This is not a default Rstudio option. Make sure you have Keybindings set to "vim" in RStudio "Global Options"

Upvotes: 6

Laura
Laura

Reputation: 11

The absolute simplest answer is in the comments on the addins answer above and deserves its own billing (with attribution):

Rstudio does have column selection, to get multiple cursors alt + mouse to select, or ctrl + alt and the arrow keys, then keys move forward and back by words/lines work as expected. – Peter Apr 16 '16 at 23:55

[ETA: On Mac, ctrl + option + arrow keys or option + mouse.]

@Peter nice. in that case you could just make a chunk of cursors at the beginning of the line and and type in #' . I doubt roxygen comments are used enough to warrant a dedicated keyboard shortcut, but it wouldn't hurt to have I guess – rawr Apr 17 '16 at 1:26

Upvotes: 1

Shorpy
Shorpy

Reputation: 1579

This isn't exactly what you're looking for. But you can add an ROxygen2 skeleton for a function by placing your cursor inside the function then pressing ctr+alt+shift+R. Then if you hit enter in the ROxygen2 codeblock it will automatically add the backtick. So an alternate workflow, edit the function, then insert the skeleton and do the documentation that way.

Upvotes: 20

csgillespie
csgillespie

Reputation: 60462

You could use an RStudio addin, you'll need a fairly recent version of RStudio. I've just created an RStudio addin that comments/uncomments using roxygen2 tags, i.e. works just like code commenting, but with #'. The addin is hosted on github.

Just install and attach a convenient keyboard shortcut.


If you are interested in other available addins, see the addinmanager addin.

Upvotes: 28

Related Questions