Home > Software engineering >  VS Code/R - how to paste highlighted text in editor to arguments in keyboard shortcut
VS Code/R - how to paste highlighted text in editor to arguments in keyboard shortcut

Time:10-15

I am regularly using the skimr::skim(name_of_a_dataframe) command in r when working interactively. To make the work a bit more efficient, I would like to assign the command to a keyboard shortcut which applies skimr::skim() to the text highlighted in the editor, i.e. the name of the dataframe. Essentially, the behavior should be similar to the keybinding F2 which opens the dataframe.

Here's what I came up with so far:

{
  "key": "F7",
  "description": "run skimr::skim() on df",
  "command": "r.runCommand",
  "when": "editorTextFocus",
  "args": "skimr::skim(XXXX)"
}

The problem I am facing is this: How can I insert the text which I highlighted/selected in the editor paste into the args section of the keybinding (XXXX).

There is a related question on SO, but the solution is eventually a snippet, something I do not need. Many thanks.

CodePudding user response:

Use the command r.runCommandWithSelectionOrWord and represent the selection or word under the cursor with $$:

{
  "key": "F7",
  "description": "run skimr::skim() on df",
  "command": "r.runCommandWithSelectionOrWord",
  "when": "editorTextFocus",
  "args": "skimr::skim($$)"
}

Info about this and related commands is on the vscode-R Wiki here: https://github.com/REditorSupport/vscode-R/wiki/Keyboard-shortcuts#creating-keybindings-for-r-commands

  • Related