前言
之前提到,我从使用RStudio编辑器迁移到了使用VScode编辑器,也是最近几年,VScode才有了相对成熟的R语言支持,但是,有一些方便的快捷键(或者说我用习惯了)没有被初始化设置,所以,需要我手动添加😑
RStudio 常用的快捷键
PS:当然,这里说的常用是我常用的(服务器 Rstudio server),并不涵盖所有
- Alt + - ,
<-
赋值 - Ctrl + Shift + m,
%>%
管道符 - Ctrl + Shift + c,注释选中行
- Ctrl + Shift + F10,重新启动 R session
- Ctrl + 1 和 Ctrl + 2,在 R Script和 R Console之间转换
- Ctrl + s,保存当前 R Script
- Ctrl + Alt + Shift + N,新建空白 R Script
……
VScode 中设置 R 语言快捷键
像什么切换啊,新建啊都内置好了(比如:Ctrl + `,Ctrl + n),但是赋值啊、管道符啊、等等都没有,所以添加一下。
- 点击设置,然后选择键盘快捷方式, 在新出现的界面右上角,点击图标:打开键盘快捷方式(JSON)
- 或者 Ctrl + Shift + p 输入 keyboard,就会看见打开键盘快捷方式(Open Keyboard Shortcuts(JSON)),点击就可以打开
- 将设置写入JSON文件:
[
//赋值
{
"key": "alt+-",
"command": "type",
"args": {
"text": " <- "
},
"when": "editorTextFocus && editorLangId == 'r'"
},
{
"key": "alt+=",
"command": "type",
"args": {
"text": " -> "
}
},
//特殊作用的
{
"key": "alt+5",
"command": "type",
"args": {
"text": " %in% "
},
"when": "editorTextFocus && editorLangId == 'r'"
},
//管道符
{
"key": "ctrl+shift+m",
"command": "editor.action.insertSnippet",
"when": "editorTextFocus && editorLangId == 'r'",
"args": {
"snippet": " %>% "
}
},
{
"key": "ctrl+shift+,",
"command": "editor.action.insertSnippet",
"when": "editorTextFocus && editorLangId == 'r'",
"args": {
"snippet": " %<>% "
}
}
]
后记
因为在VScode中使用R语言,有个好用的,类似于Ipython的工具,叫radian
,所以,也贴上一些radian
的设置,直接放在家目录的.radian_profile
配置文件中就OK:
# allows user defined shortcuts, these keys should be escaped when send through the terminal.
# In the following example, `esc` + `-` sends `<-` and `esc` + `m` sends `%>%`.
# Note that in some terminals, you could mark `alt` as `escape` so you could use `alt` + `-` instead.
options(radian.escape_key_map = list(
list(key = "-", value = " <- "),
list(key = "m", value = " %>% ")
))