Modifying RStudio Keyboard Shortcuts in .RProfile

I am wondering if there is a way to customize RStudio keyboard shortcuts (i.e., the ones you see in RStudio by default when you type Alt + Shift + k) in an .RProfile. I know you can do this manually by going to "Tools --> Modify Keyboard Shortcuts" in the IDE, but I am looking to do this automatically upon startup.

If it helps, here is an analogy. To change your choice of CRAN mirror, you can go to "Tools --> Global Options --> Packages" and change the mirror manually. However, you can also do this in a .RProfile script as follows:

local({
     r <- getOption("repos")
     r["CRAN"] <- "https://cran.rstudio.com/"
     options(repos=r)
})

I have not yet seen an online resource that addresses my question, so I thought I would ask it here. I know my choice of keyboard shortcuts would be preserved from one R session to another, but I am looking to be as efficient as possible (e.g., maybe I end up switching computers in the future - I could just run my .RProfile on a new machine with my preferred keyboard combinations instead of having to manually change them all again).

In case it helps, here's the bit of documentation about how keybindings are saved:

Saving and Loading

The RStudio keybindings are saved as JSON files in the directory ~/.R/rstudio/keybindings/ — you can find the bindings for the editor and RStudio itself at:

~/.R/rstudio/keybindings/rstudio_commands.json
~/.R/rstudio/keybindings/editor_commands.json

From:

2 Likes

I apologize for taking longer than a month to respond - thank you for getting me started on this topic. However, I cannot find the directory you provided. I am using Windows 10, and I currently have R 3.5.1 and RStudio 1.1.453 installed. I have enabled hidden files/folders, and I have searched for "keybindings", "rstudio_commands", "editor_commands", and ".json" in my R and RStudio folders - nothing comes up.

Am I supposed to create the two .json files? I am guessing they are supposed to exist already, but I just thought I would ask.

On Windows, these files are located in the directory e.g.

> normalizePath("~/.R/rstudio/keybindings")
[1] "C:\\Users\\kevin\\Documents\\.R\\rstudio\\keybindings"

Note that only customized keybindings exist here (we don't write out the default keybindings) and hence only exist if you've already modified keyboard shortcuts in an existing IDE session.

2 Likes