0

I know the topic of multi cursors-editing in visual studio code is duplicate but what I want is the way (by keyboard) to select word occurrences in just one line in visual studio code, because the other option: ctrl + F2 it selects all the occurrences in all the file, and not the mouse way by holding alt and click.

let say I have this:

const FETCH_USERS_REQUEST = "FETCH_USERS_REQUEST"; 
const FETCH_USERS_REQUEST = "FETCH_USERS_REQUEST"; 

when the cursor is on REQUEST word for the second line I want to do two cursors in the second line after the two occurrences of REQUEST.

Ismail
  • 65
  • 1
  • 8
  • you can select the first occurrence of the text you want, and then with Ctrl+D add more occurrences, if you do to many use Ctrl+U – rioV8 Aug 08 '21 at 04:48

1 Answers1

1

You have 2 alternatives:

  1. Enable the Find in Selection option in the Find Widget after entering your find query.

  2. An extension that I wrote does this fairly well, see Find and Transform.

With this simple keybinding:

{
  "key": "alt+y",             // whatever keybinding you want
  "command": "findInCurrentFile",
  "args": {
    "restrictFind": "line"   // find all on current line only
      // with multiple cursors you can have as many current lines as you wish
  }
}

It does a find in the current file. Since there is no actual find query, like REQUEST, designated in the args. It will find the current word at the cursor on the line. Different languages define what a "word" is differently. For javascript, for example, FETCH_USERS_REQUEST is the current word even if the cursor is on Request only.

You can manage this by actually selecting, double-clicking on Request or Ctrl+D, and then trigger the above keybinding. Then the extesnion will search for whatever is selected, if there is a non-empty selection.

The extension is designed to select those find matches, not put the cursor after them, but you can simply right-arrow to dismiss the selection and the cursor will be where you want.

[I have to update the extension, the current version v0.9.7 in the Marketplace won't do this - but here is a demo of it already working. It should be updated tomorrow at the latest, look for v0.9.8.]

find word on line

Mark
  • 143,421
  • 24
  • 428
  • 436