0

Say for example I have a very long line of text in a single line in VS Code (let's pretend that the example given below is very long).

0xffffffffeeeeeeee02020202aaaaaaaa

At first I placed my cursor after the characters 0x. (the cursor is denoted by the | character in the example below)

0x|ffffffffeeeeeeee02020202aaaaaaaa

Then I want to add more cursors after every N characters from the current cursor. In this case N is equal to 8 and I want to do this twice to add two more cursor like in the example below.

0x|ffffffff|eeeeeeee|02020202aaaaaaaa

So that after I press the following sequence of keys in the keyboard, in this case those sequence of keys are ,(space)0x I should be able to get these final result.

0x, 0x|ffffffff, 0x|eeeeeeee, 0x|02020202aaaaaaaa

After I deselect the cursors I should be getting this

0x, 0xffffffff, 0xeeeeeeee, 0x02020202aaaaaaaa

Is this possible to do in VS code?

rioV8
  • 24,506
  • 3
  • 32
  • 49
0xdeadbeef
  • 500
  • 3
  • 17
  • This is called multiedit. Does this help: https://stackoverflow.com/questions/30037808/multiline-editing-in-visual-studio-code – cepheus Apr 13 '22 at 10:38

2 Answers2

2

There is a straightforward regex that can do what you want:

Find: ^0x|(.{8})(?!$)

But you have to enable the Find in Selection option and trigger the Select All Matches command yourself after entering it.

Or use a macro extension like multi-command and this keybinding to automate it:

{
  "key": "alt+p",
  "command": "extension.multiCommand.execute",
  "args": {
          "sequence": [
        {
          "command": "editor.actions.findWithArgs",
          "args": {
            "findInSelection": true,
            "isRegex": true,
            "searchString": "^0x|.{8}",
          }
        },
        "editor.action.selectAllMatches",
        "cursorRight"
      ]
  },
}

You must select up to where you want the last cursor and then trigger the macro.

  • Because of a flaky implementation, you must start with the Find in Selection option disabled in the Find Widget. I haven't found a way around that.

  • The setting Editor > Find: Seed Search String From Selection must be set to never. Otherwise your selected text will over-ride the searchString from the macro above.

put a cursor every eight characters macro



Here is the pure regex method with no extensions:

  1. Enter ^0x|(.{8})(?!$) in your Find Widget with the regex option enabled.

^0x the first part of the string you ultimately want a cursor after.

(.{8})(?!$) select each 8-character block, but not the last - that is why there is a negative lookahead for the end of the line (?!$) - so the last 8 characters are not matched. Don't worry, there will be a cursor in front of those last 8 characters as you want. (.{8}) doesn't actually need to be in a capture group, it is just clearer to see.

  1. Select all the text to match: 0xffffffffeeeeeeee. Stop the selection there - wherever you want the last cursor.

  2. Enable the Find in Selection option in the Find Widget by Alt+L.

  3. Alt+Enter to select all the find matches respecting the Find in Selection option: editor.action.selectHighlights.

Step (4) will select your matches - you should have 4 for the above string. But you don't want the matches selected you just want a cursor at the beginning of each, so do step (5):

  1. Right arrow: this cancels each selection with a cursor at the right end of each.

  2. Type.

every eight characters regex

Mark
  • 143,421
  • 24
  • 428
  • 436
  • I tried `(.{8})` regex search and `Ctrl+D` but was unable to get it working – rioV8 Apr 15 '22 at 11:04
  • @rioV8 I have posted the pure regex version. 5 straightforward steps but good for a macro. – Mark Apr 15 '22 at 21:22
  • I see I made it for the entire line and for some reason the OP didn't want the last 8 characters separated... – Mark Apr 15 '22 at 21:29
  • Fixed for partial selection. – Mark Apr 15 '22 at 21:53
  • you don't have to switch focus to the editor in step 4, focus in the Find dialog, press `Alt+L` and you select all and move focus to editor. I think they have changed the finding of regex, I think I was able to enter a regex, focus to editor, press `F3` and then `Ctrl+D` for each next occurrence of the regex. – rioV8 Apr 15 '22 at 22:25
  • It looks like you are right - for some reason there is a big difference between using the mouse to select the `Find in Selection` option and `Alt+L` ?? I was using the mouse and then `Alt+Enter` **de-selects** the selection option and you get everything in the file - I consider that a bad bug. I'll revise to simplify. Thanks. – Mark Apr 15 '22 at 22:38
1

You can use the extension Select By

It has a command to add a new cursor by keyboard

  {
    "key": "ctrl+i ctrl+alt+right", // or any other key combo
    "when": "editorTextFocus",
    "command": "selectby.addNewSelection",
    "args": {"offset": 8}
  }

Now the offset is hard coded but I will add an option to ask the user the offset.

The possibility of the context switch is already working. I have to update the README.

rioV8
  • 24,506
  • 3
  • 32
  • 49