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?

- 97,041
- 11
- 181
- 245

- 4,814
- 1
- 32
- 54
-
1You can define your own shortcut using addins: example here http://rstudio.github.io/rstudioaddins/ – user20650 Apr 16 '16 at 15:59
4 Answers
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.

- 59,189
- 14
- 150
- 185
-
1_surely_ rstudio can come up with something better than this. eg, in sublime, one can put `{ "keys": ["cmd+shift+r"], "command": "insert", "args": {"characters": "#' "} }` or similar in the rstudio equivalent `~/.R/rstudio/keybindings/editor_bindings.json` (although the multiple cursors makes this next to worthless).. speaking of which, @RStudio, can we get multiple cursors? – rawr Apr 16 '16 at 19:48
-
3Rstudio 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
-
@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 01:26
-
-
Works like a charm. Thanks @csgillespie for such wonderful addsin. Much appreciate. – MYaseen208 Apr 17 '16 at 08:14
-
-
didn't know about addins. Worth staying up to date with Rstudio, and great tool though !! (if I had one little comment, I would just say that your package adds an extra space each time you comment/uncomment) – ClementWalter Apr 22 '16 at 13:08
-
@clemlaflemme Would you raise an issue on the github page and I'll try and fix. – csgillespie Apr 22 '16 at 13:13
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.

- 1,549
- 13
- 28
-
I couldn't remember this shortcut and found your answer through Google. Thanks! – Andrew Brēza Jun 08 '17 at 13:43
-
Rstudio find/replace
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
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"

- 2,637
- 1
- 18
- 25
-
Thanks for this answer! Quick and easy without having to install the addin. – trangdata Aug 09 '22 at 23:37
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

- 11
- 1