Store your shortcuts in ~/.config/.shrtcts.R
or
~/.shrtcts.R
. Each shortcut should look something like the
example below, but you can include any R code you want as the
shortcut, as long as it’s a function.
#' Say Something Nice
#'
#' A demo of cool things.
#'
#' @interactive
#' @shortcut Ctrl+Alt+P
praise::praise
Then add the following lines to your ~/.Rprofile
, which
you can find quickly with usethis::edit_r_profile()
. (Or
you can skip this step and run add_rstudio_shortcuts()
whenever you update your shortcuts.)
# ~/.Rprofile
if (interactive() && requireNamespace("shrtcts", quietly = TRUE)) {
shrtcts::add_rstudio_shortcuts()
}
You can also tell shrtcts to automatically update the keyboard shortcuts assignments.
# ~/.Rprofile
if (interactive() && requireNamespace("shrtcts", quietly = TRUE)) {
shrtcts::add_rstudio_shortcuts(set_keyboard_shortcuts = TRUE)
}
After restarting your R session, you’ll find your new shortcut Say Something Nice in your RStudio Addins menu!
If you enabled keyboard shortcut management, you’ll also be able to
run your new shortcut by pressing Ctrl + Alt +
P. But note that whenever your keyboard shortcuts update,
you’ll need to completely restart RStudio — hint: try
usethis:::restart_rstudio()
— for RStudio to pick up the
new keybindings.
If you store your .shrtcts.R
file in your home
directory, you could also just run
shrtcts::add_rstudio_shortcuts()
whenever you update the
shrtcts file instead of adding the above code to your
~/.Rprofile
.
Use the following template to organize your .shrtcts.R
.
You can write each shortcut in regular R code, annotated with roxygen2 inline documentation
comments. The comment format uses standard roxygen2 formatting, with a
few additional roxygen tags specifically for
shrtcts
Save your shortcuts R (or YAML) file as .shrtcts.R
or
.shrtcts.yml
in your home directory or in the
.config
directory in your home directory — use fs::path_home_r()
or fs::path_home()
to locate your home directory. In other words:
~/.config/.shrtcts.R
or ~/.shrtcts.yml
.
You can test that shrtcts correctly finds your shortcuts file – or confirm which file will be used by shrtcts – using locate_shortcuts_source().
Run add_rstudio_shortcuts()
to install your shortcuts.
You’ll need to restart your R session for RStudio to learn your
shortcuts.
To also update your shrtcts-related keyboard
shortcuts, set set_keyboard_shortcuts = TRUE
. This will
update the keyboard shortcuts stored in RStudio’s
addins.json
, typically stored in
~/.config/rstudio/keybindings
(>= 1.3) or
~/.R/rstudio/keybindings
(< 1.3). If this file is stored
in a non-standard location in your setup, you can provide
set_keyboard_shortcuts
with the correct path to
addins.json
. Whenever shrtcts updates the
shortcut keybindings, a complete restart of RStudio is required (hint:
use usethis:::restart_rstudio()
).
Once you’ve setup an RStudio Addin via shrtcts, there are two ways to link the shortcut’s addin to a keyboard shortcut.
You can verify and list the current shortcuts and their keyboard bindings with list_shortcuts().
shrtcts::list_shortcuts()
#> name addin shrtcts_keybinding rstudio_keybinding
#> 1 10 random numbers shortcut_01 <NA> <NA>
#> 2 New Temporary R Markdown Document shortcut_02 Ctrl+Alt+Shift+T Ctrl+Alt+Shift+T
#> 3 A Random Number Between 0 and 1 shortcut_03 <NA> <NA>
#> 4 Say Something Nice shortcut_97 Ctrl+Alt+P Ctrl+Alt+P
.shrtcts.R
You can use the @shortcut
tag to declare the shortcut in
.shrtcts.R
(or shortcut:
in the YAML
.shrtcts.yml
).
To update the keyboard shortcuts (for shrtcts only!), set
set_keyboard_shortcuts = TRUE
when calling
add_rstudio_shortcuts()
. If you use this method, shortcuts
set manually in RStudio will be overwritten, so you should choose one
method or the other.
.shrtcts.R
.shrtcts.yml
A full restart of RStudio is required whenever shrtcts udpates the shortcut keybindings. shrtcts only manages keybindings for its own addins, and it doesn’t check for conflicting key combinations, so you may want to double check the RStudio menu.
If anything goes wrong, a backup of the keybindings are saved as
addins.json.bak
in the same folder where
addins.json
was found. Use
location_addins_json()
to find this file.
shrtcts initially provided a way to specify the
shortcuts in a YAML file. This made sense because everything is YAML
these days, so why not add yet another YAML config file to the mix? But
writing R code inside YAML is, um, less than ideal. So it’s no longer
recommended, but it is still supported (for now). To convert existing
shortcuts from YAML to the roxygen2 format, use the internal
shrtcts:::migrate_yaml2r()
function.
Use the following template to organize your
.shrtcts.yaml
. Each shortcut is a YAML list item with the
following structure:
shrtcts was inspired by rsam, the RStudio Addins Manager by @yonicd. There’s a lot that rsam can do — including helping you manage your keyboard shortcuts — and shrtcts is essentially an extension of rsam’s limited liability addins. rsam provides three slots for custom addins that in turn look for specially-named functions defined in the global environment. In the addins menu, these three custom addins appear as lla1, lla2, and lla3. The upside of rsam is that you don’t have to write code in YAML (huge plus!), but the downside is that the names of the addins are fixed.
shrtcts, on the other hand, rewrites its own addin registry so that you can have customized addin names and descriptions. In both packages, the number of custom addins is limited: rsam provides 3 slots, while shrtcts gives you 100.