1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

VSCode: カーソル行を対象にRSpecを実行する

Last updated at Posted at 2025-09-16

やりたいこと

Rubyのファイルを編集中に指定のキーを押したとき、カーソル行を対象にRSpecを実行したい。
つまりターミナル内で下記コマンドを実行したい:

$ bundle exec rspec -f d ファイル名:行番号

手順

1. VSCode拡張Copy Relative Path and Line Numbersをインストール

この拡張は
Ctrl+l (MacOS)
Alt+l (Windows/Linux)
というキーバインドを提供し、ファイル名(相対パス):行番号という形式でカーソル行の行番号をクリップボードにコピーしてくれる。

2. RSpec実行のキーバインド設定

keybindings.jsonに記述:

  {
    "key": "ctrl+r",
    "command": "runCommands",
    "args": {
      "commands": [
        "copy-relative-path-and-line-numbers.both",
        {
          "command": "workbench.action.terminal.sendSequence",
          "args": {
            "text": "bundle exec rspec -f d "
          },
        },
        "workbench.action.terminal.paste",
        {
          "command": "workbench.action.terminal.sendSequence",
          "args": {
            "text": "\u000d"  // Enter
          },
        }
      ]
    },
    "when": "editorLangId == 'ruby' && editorTextFocus",
  },

これで、Rubyのファイルを編集中にCtrl+rを押すと、ターミナルでrspecを実行するようになる。

他の方法

vscode-run-rspec-fileという拡張にも同じような機能があるようだ(試してない)。

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?