从 Rstudio 编辑启动外部文本编辑器 window

Launching external text editors from the Rstudio editing window

虽然 RStudio 自带的原生文本编辑器可用,但其 Vim 映射仍然不理想。那么,RStudio中有没有可以调用外部文本编辑器的function/command呢?理想的方式是触发 "external editor" 的键盘快捷键,这是通过设置指定的。

有一个过时的请求如下:

你可以自己写。使用 rstudioapi::getSourceEditorContext()$path 获取源代码编辑器中当前文档的名称。此页面:https://rstudio.github.io/rstudioaddins/ 描述了如何将一些 R 代码附加到密钥。

例如,您可以将此功能放入您的加载项中:

editInTextedit <- function() 
  system(paste("open -e", rstudioapi::getSourceEditorContext()$path))

并且(在 Mac 上)它将 运行 textedit 编辑当前文件。或者,如果您使用的是内置终端的足够新版本的 RStudio,您可以使用

editInVim <- function()
  rstudioapi::terminalExecute(paste("vim", rstudioapi::getSourceEditorContext()$path))

我发现 vim 我到达那里的效果不是很好(例如,它不理解箭头键),但你可能运气更好。

编辑添加:加载项存储在 R 包中。要把上面两个功能做成插件,新建一个工程,选择新建一个R包。给它起一个不会与其他 R 包冲突的名称,例如"AddInEditors"。将这两个函数定义放入 R 子目录中的 .R 文件中,并创建包含

的文件 inst/rstudio/addins.dcf
Name: Run Textedit
Description: Runs Textedit on the current source file.
Binding: editInTextedit
Interactive: true

Name: Run Vim
Description: Runs Vim on the current source file.
Binding: editInVim
Interactive: true

然后运行Build | Install and restart在包上,插件应该是可用的。

这并没有将它们附加到热键上;有关这方面的说明,请参阅之前的参考资料。如果没有热键,您可以在顶部菜单栏的 Addins 菜单中找到它们,或者通过 Tools | Addins | Browse addins....