0xdeadbeef
0xdeadbeef

Reputation: 581

How to place a cursor after every N characters in VS Code?

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?

Upvotes: 1

Views: 2723

Answers (2)

Mark
Mark

Reputation: 181339

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

Upvotes: 2

rioV8
rioV8

Reputation: 28673

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.

Upvotes: 1

Related Questions