Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Select code block or paragraph in VS Code/R

I would like to select an R code block via a shortcut.

At the moment I am using CTRL+L to select the current line and CTRL+ALT+UP/DOWN to expand the selection. This, however, is cumbersome.

Is there any way to tell VS Code to select everything in a paragraph?

Example:

library(dplyr)

starwars %>% 
  filter(species == "Droid")

starwars %>% 
  |mutate(name, bmi = mass / ((height / 100)  ^ 2)) %>% # <- The cursor is where "|" is for example
  select(name:mass, bmi)

This is what should be selected in this example:

starwars %>% 
  mutate(name, bmi = mass / ((height / 100)  ^ 2)) %>%
  select(name:mass, bmi)
like image 682
persephone Avatar asked Sep 20 '25 23:09

persephone


1 Answers

This could be done with the aid of an extension. See, e.g., the Select By extension in which you can specify the starting and ending regex within the keybinding. Keybinding:

{
  "key": "alt+q",               // whatever you want
  "command": "selectby.regex",
  "args": {
    "flags": "m",
    "backward": "^\\w",        // since your block starts flush left apparently
    "forward": "\n^$",         // stop at first empty line
    "forwardInclude": false,
    "backwardInclude": true
  }
}

Here is one that I wrote: Jump and Select. Use this keybinding:

{
  "key": "alt+q",                          // whatever keybinding you want 
  "command": "jump-and-select.jumpBackwardSelect",
  "args": {
    "text": "^\\w",
    "putCursorBackward": "beforeCharacter",
    "restrictSearch": "document"
  }
}

This should select from the cursor back to the first blank line (given your well-structured code examples).

jump and select backward from cursor


To select the block from anywhere, you also need a macro extension like multi-command and this keybinding:

{
  "key": "alt+q",
  "command": "extension.multiCommand.execute",
  "args": {
    // "interval": 200,
    "sequence": [
      {
        "command": "jump-and-select.jumpBackward",
        "args": {
          "text": "^\\w",
          "putCursorBackward": "beforeCharacter",
        }
      },
      {
        "command": "jump-and-select.jumpForwardSelect",
        "args": {
          "text": "^[^\\w]$\n?",
          "putCursorBackward": "afterCharacter",
        }
      }
    ]
  },
  "when": "editorFocus"
},

select entire block: r language


like image 183
Mark Avatar answered Sep 22 '25 13:09

Mark