@Andrea The main solution that comes to mind would be to create an RStudio Addin that uses the rstudioapi package (e.g. getActiveDocumentContext()
). The addin could then be bound to a keyboard shortcut.
Unfortunately this requires a large amount of background knowledge (because the addin has to be saved in an R package structure), so it wouldn't be possible to record the macro.