..

Zed R 语言环境配置:面向数据分析与统计建模(而非 R 包开发)的 tasks.json 与 keymap.json 设置

·

The default task templates in ocsmit/zed-r focus on R package development (devtools::check, testthat). Based on that foundation, here is an adapted, clean configuration tailored for data analysis, statistics, and interactive scripting (tested on macOS with arf, radian, or base R).

Key Features

  • RStudio-style execution (Cmd + Enter): Sends current line (or selection) to the active R console, advances cursor to the next line, and leaves the terminal open.
  • Prevents terminal auto-hiding: Remaps ctrl-`` to terminal_panel::ToggleFocus` so sending code toggles focus without collapsing/hiding the bottom dock.
  • CRAN package installation: Quick-install from highlighted text (Cmd + Shift + B) or via a native prompt dialog (Cmd + Shift + E).
  • Batch execution (Cmd + Shift + Enter): Runs the current file via a standalone Rscript task.

配置文件

1. ~/.config/zed/tasks.json

[
  {
    "label": "R Terminal",
    "command": "radian --no-save",
    "cwd": "$ZED_WORKTREE_ROOT",
    "use_new_terminal": true,
    "reveal": "always",
    "hide": "on_success"
  },
  {
    "label": "Run current R script",
    "command": "Rscript \"$ZED_FILE\"",
    "cwd": "$ZED_WORKTREE_ROOT",
    "use_new_terminal": false,
    "allow_concurrent_runs": false,
    "reveal": "always",
    "show_summary": true,
    "show_output": true
  },
  {
    "label": "Source current file",
    "command": "Rscript -e \"source('$ZED_FILE', echo = TRUE)\"",
    "cwd": "$ZED_WORKTREE_ROOT",
    "use_new_terminal": false,
    "allow_concurrent_runs": false,
    "reveal": "always",
    "show_summary": true,
    "show_output": true
  },
  {
    "label": "Install selected R package",
    "command": "if [ -z \"$ZED_SELECTED_TEXT\" ]; then echo '⚠️ 请先在编辑器中选中要安装的 R 包名(例如选中 ggplot2)'; exit 1; fi; Rscript -e \"install.packages('$ZED_SELECTED_TEXT')\"",
    "cwd": "$ZED_WORKTREE_ROOT",
    "use_new_terminal": false,
    "allow_concurrent_runs": false,
    "reveal": "always",
    "show_summary": true,
    "show_output": true,
    "show_error": true
  },
  {
    "label": "Install R package (prompt)",
    "command": "pkg=$(osascript -e 'try\ntext returned of (display dialog \"请输入要安装的 R 包名:\" default answer \"\" with title \"安装 R 包\")\non error\nreturn \"\"\nend try'); if [ -n \"$pkg\" ]; then Rscript -e \"install.packages('$pkg')\"; fi",
    "cwd": "$ZED_WORKTREE_ROOT",
    "use_new_terminal": false,
    "allow_concurrent_runs": false,
    "reveal": "always",
    "show_summary": true,
    "show_output": true,
    "show_error": true
  },
  {
    "label": "Document local R package",
    "command": "Rscript -e \"devtools::document(roclets = c('rd', 'collate', 'namespace'))\"",
    "cwd": "$ZED_WORKTREE_ROOT",
    "use_new_terminal": false,
    "allow_concurrent_runs": false
  }
]

2. ~/.config/zed/keymap.json

[
  {
    "context": "Editor",
    "bindings": {
      "cmd-shift-f10": "repl::Restart",
      "cmd-\\": "assistant::InlineAssist",
      "ctrl-shift-m": ["workspace::SendKeystrokes", "space |> space"],
      "alt--": ["workspace::SendKeystrokes", "space < - space"],
      "cmd-alt-i": ["workspace::SendKeystrokes", "\n# space %%\n"],
      // 运行当前行(或选中块)发送到已打开的 R 终端,终端常驻,光标自动下移一行
      "cmd-enter": [
        "workspace::SendKeystrokes",
        "cmd-c ctrl-` cmd-v enter ctrl-` down"
      ],
      "cmd-alt-enter": [
        "workspace::SendKeystrokes",
        "cmd-c ctrl-` cmd-v enter ctrl-` down"
      ],
      // 批处理执行整份 R 脚本
      "cmd-shift-enter": [
        "task::Spawn",
        {
          "task_name": "Run current R script",
          "reveal_target": "dock"
        }
      ],
      "cmd-shift-d": [
        "task::Spawn",
        {
          "task_name": "Document local R package",
          "reveal_target": "center"
        }
      ]
    }
  },
  {
    "context": "Workspace",
    "bindings": {
      // 核心按键修正:切换焦点而非折叠面板,确保终端常显
      "ctrl-`": "terminal_panel::ToggleFocus",
      // 打开/唤起底部的交互终端 (arf / R)
      "cmd-shift-m": [
        "task::Spawn",
        {
          "task_name": "R Terminal",
          "reveal_target": "dock"
        }
      ],
      "cmd-shift-r": [
        "task::Spawn",
        {
          "task_name": "Run current R script",
          "reveal_target": "dock"
        }
      ],
      "cmd-shift-s": [
        "task::Spawn",
        {
          "task_name": "Source current file",
          "reveal_target": "dock"
        }
      ],
      // 划词安装选中的第三方包
      "cmd-shift-b": [
        "task::Spawn",
        {
          "task_name": "Install selected R package",
          "reveal_target": "dock"
        }
      ]
    }
  }
]