1

when I select a certain word in vscode and press ctrl+shift+l all occurrences of that word will be selected throughout the entire file, is there a way to achieve the same thing but only select the occurrences on the currently selected line?

HenriDev
  • 587
  • 7
  • 13
  • 'Replace in selection' could probably work, first select whole line then do a replacement as here: https://stackoverflow.com/questions/44335619/find-and-replace-in-visual-studio-code-in-a-selection – Renat Apr 18 '23 at 11:55
  • @HenriDev you could just `alt+click` in order to add cursors to a line. – Kostas Nitaf Apr 18 '23 at 11:56
  • @KostasNitaf if I have a very long line it could be that the word is there 50+ times, I don't want to `Alt+Double-Click` 50 times and possibly skip one – rioV8 Apr 18 '23 at 15:45

3 Answers3

1
  • Select the word you want
  • Copy the word: Ctrl+C
  • Select the whole line: Ctrl+L
  • Start Find dialog: Ctrl+F
  • Select button: Find in Selection Alt+L
  • Paste Word: Ctrl+V
  • Select All in Selection: Alt+Enter

If you find all the commands for these shortcuts you can combine them with multi-command extension into one key binding

rioV8
  • 24,506
  • 3
  • 32
  • 49
1

This is very easy with an extension I wrote, Find and Transform. Create a keybinding like:

  {
    "key": "alt+q",           // whatever keybinding you want
    "command": "findInCurrentFile",
    "args": {
      "description": "Select all matches on current line",  // whatever description you want
      "restrictFind": "line"
    }
  }

Then just put the cursor into the word and trigger your keybinding.

And if you want to replace those matches with something just put

select all matches in line

There are other options than within the current line, like the whole document, just the next or previous match, just the next match on the line, etc.

And if you want to replace those matches with some text just put an additional arg into the keybinding:

"replace": "my replacement text",
Mark
  • 143,421
  • 24
  • 428
  • 436
0

You can select using CTRL+SHIFT+L in the same way. First, you select the entire line with CTRL+L and then press CTRL+SHIFT+L to select all occurrences identical to the selected line. But attention to one detail, CTRL+L also selects any blank spaces on that line.

  • that's not what i'm looking for, i want to select a single word in a line and then select all occurrences of that word in the same line. – HenriDev Apr 18 '23 at 12:37
  • 1
    CTRL+D you select one by one, starting from the next word to the right closest to the one you selected.. is that it? – Shirley Oliveira Apr 18 '23 at 12:47
  • oh that's a start already thanks ! – HenriDev Apr 18 '23 at 12:52
  • still not 100% what i was looking for but this is more flexible – HenriDev Apr 18 '23 at 12:53
  • 1
    I don't think there a way of selecting all occurences of the same word in one line and just in that line, I even tried to search for it on Google with no success. For what I see, the closest way to achieve what you want is really with CTRL+D. If you find it, please tell me. – Shirley Oliveira Apr 18 '23 at 13:38